JAVA Exception Handling27
JAVA Exception Handling27
Not all errors can be caught at compile time! Help -- run-time error! What next ? First ideas:
n n
Good guess but some errors call for corrective action, not just Better: tell someone (not necessarily the user)!
OOP: Exception Handling 1
The idea comes from hardware. Modern language support (Java, Python, Lisp, Ada, C++).
One error in a method can be handled very differently in the clients, this is not a good approach. Repeating handling of the same error. Can be extremely hard to debug.
Where did the error occur (class, method, line number) What type of error A good error message Dump of runtime stack? (too much information?)
A run-time phenomenon.
Exception handling is part of the language. Exceptions are objects. Exceptions are structured in a class hierarchy. It is not possible to ignore an exceptions (very nice feature).
n
A method defines which exception may occur, the client must anticipate these exceptions, otherwise compile-time error.
more powerful, more flexible than using return keywords try, catch, throw, throws, finally.
detect error, probably with an if create a new exception and throw it or let JVM detect error, create and throw an exception
public static void main (String args[]) throws exception1, exception2, exception3 { . . . }
Specify
n n
If a method chooses not to catch, then specify which exceptions are thrown. Exceptions are part of a method's public interface.
Checked/Unchecked Exceptions
An exception is either checked or unchecked
n
10
Unchecked
11
Superclass for all exceptions Two methods for filling in and printing the stack Serious internal errors (should not occur in running programs). Are normally not handled. (report and terminate) Programs should not throw Error The catch or specify principle does not apply, because they are so severe. Examples
u u u
Error
n n n n n
12
Not a good name (all exceptions are at run-time)! Commonly seen run-time error The catch or specify principle does not apply, because they are so ubiquitous. Examples
u
IOException
n n n
Related to input and output The catch or specify principle does apply Examples
u u
13
14
15
16
18
} [source: java.sun.com]
19
Exception Propagation
If it is not appropriate to handle the exception where it occurs,
it can be handled at a higher level. Exceptions propagate up through the method calling hierarchy until they are caught and handled or until they reach the outermost level. A try block that contains a call to a method in which an exception is thrown can be used to catch that exception.
20
Rethrowing an Exception
void method1 throws IOException { throw new IOException("Error in method1"); } void method2 throws IOException { try{ method1(); } catch (IOException e) { System.out.printly ("Handle partly here"); throw e; // 1 st method // throw e.fillInStackTrace; // 2nd method // throw new IOException ("new one"); // 3th method } } public static void main (String args[]){ // catch if just specify error to console try { method2(); } catch (IOException e){ System.out.printly ("Handle rest here"); } OOP: Exception Handling }
22
The most important thing for new exceptions. Tends to be long an descriptive (ArrayIndexOutOfBoundsException)
All classes that inherits from Exception has 'Exception' postfixed to their name. All classes that inherits from Error has 'Error' postfixed to their name.
23
25
Guidelines
Do not use exceptions for normal control flow! Do use exceptions to indicate abnormal conditions! Handle the error (fully or partially) if you have enough
information in the current context. Otherwise, propagate! Handle group of statements
n
Use exceptions in constructors! Do something with the exceptions your code catches! Clean up using finally.
26
Summary
The manner in which an exception is processed is an important
design consideration.
Advantages of Exceptions
n n
Separates error handling from "regular" code. Propagation of errors up the call stack.
u
27