Exception Error handling Exception n n 4 An

  • Slides: 9
Download presentation
Exception Error handling

Exception Error handling

Exception n n 4 An unusual occurrence during program execution that requires immediate handling

Exception n n 4 An unusual occurrence during program execution that requires immediate handling Errors are the most common type of exception Java enables centralization of exception handling code in a location that is most logical When an exception is reported (or thrown) execution is automatically transferred the closest exception handler, no matter what method was executing when the exception was thrown.

Try/catch n n 4 Try establishes a block of code that is to have

Try/catch n n 4 Try establishes a block of code that is to have its exceptions and abnormal exits (through break, continue, return, or exception propagation) handled Catch—a try block may be followed by zero or more catch clauses that specify code to handle various types of exceptions – Catch clauses have unusual syntax: each is declared an argument, much like a method argument. This argument must be of type throwable or a subclass – Do not need a catch clause for every possible

Try…catch n n n n n try statement 1 [catch(Exception. Class 1 e 1)

Try…catch n n n n n try statement 1 [catch(Exception. Class 1 e 1) statement 2] [catch(Exception. Class 2 e 2) statement 3] [finally statementn] The statement following try is executed unconditionally as part of the normal flow of execution 4

Try…catch n n n 3 During execution of this statement (or block), an exception

Try…catch n n n 3 During execution of this statement (or block), an exception may be raised (even indirectly, i. e. , statement may call a method that may raise an exception) If the exception is raised and not handled by a more tightly nested block or method, statement 1 terminates execution and the interpreter checks if one of the catch blbocks can handle the exception Every exception class implements the throwable interface

Exception n 3 Statements inside the catch block are executed when the respective exception

Exception n 3 Statements inside the catch block are executed when the respective exception type is matched Inside the catch block, the code may decline to handle the exception by executing a throw statement. Here, the exception is passed along and the interpreter must search for another handler Exceptions not handled by any code eventually reach the Java default exception handler, which prints a message and terminates execution

Finally n n n 3 Code in the finally block is executed unconditionally after

Finally n n n 3 Code in the finally block is executed unconditionally after execution leaves the try block The finally block is guaranteed to be executed, whether the try block terminated normally or exited early because of a n exception or break statement The finally block is a logical place to put code that cleans up

Exception n n n No Exception try { exception raised } catch(…) { }

Exception n n n No Exception try { exception raised } catch(…) { } n n n try { } catch(…) { }

Console. java n n n n public static int read. Int() { String tmp.

Console. java n n n n public static int read. Int() { String tmp. X = read. String(). trim(); try { return Integer. parse. Int(tmp. X); } catch(Number. Format. Exception ne) { System. err. println("Not an integer. Quitting. . . "); System. exit(-1); return -1; } }