20090306-Exceptions

20090306-Exceptions - Exceptions and Exception Handling...

Info iconThis preview shows pages 1–8. Sign up to view the full content.

View Full Document Right Arrow Icon
Exceptions and Exception Handling Carl Alphonce CSE116
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Intermediate Java Topic overview Exceptions and exception handling What are exceptions? When should you use exceptions? Details How are exceptions generated (thrown)? How are exceptions handled (caught)? Runtime exceptions vs. other exceptions. Collections What is a collection? java.util.Collection interface Iterators java.util.Iterator interface Iterator pattern Decoupling of data structure and iteration logic
Background image of page 2
Intermediate Java What are exceptions? Exceptions are a mechanism for handling “exceptional” situations which can occur at runtime. Many languages provide exceptions. Terminology: code where something unexpected happens “throws” an exception code which handles the exceptional situation “catches” the exception – this code is called an exception handler
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Intermediate Java When are exceptions appropriate? Exceptions are appropriate to use to signal to a caller a problematic situation which cannot be handled locally. Example: Consider a file reading component which is used both by an interactive UI and a batch processing component. If the file reading component has a problem (e.g. “disk is full”), can it decide locally how to respond? No. It is up to the client to decide how to react. The UI may notify its human user. The batch processor may log the problem, and skip processing of this file.
Background image of page 4
Intermediate Java When are exceptions not appropriate? It is not appropriate to use the exception mechanism when an exceptional situation can be handled locally. It is not appropriate to use the exception mechanism in dealing with situations which are not exceptional. If a particular situation is expected, it should be explicitly checked for. For example, if a user supplies the name of a file to be read, it is better to check for existence of the file rather than to attempt to read and rely on a thrown exception to give notice if the file doesn’t exist.
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Intermediate Java How are exceptions generated? An exception is an object An exception must derive from the java.lang.Exception class Detour into inheritance and typing…
Background image of page 6
Intermediate Java Types and subtypes Every class in Java defines a type. Every interface in Java defines a type.
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 8
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 32

20090306-Exceptions - Exceptions and Exception Handling...

This preview shows document pages 1 - 8. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online