Presentation is loading. Please wait.

Presentation is loading. Please wait.

Exception Error handling. Exception 4 n An unusual occurrence during program execution that requires immediate handling n Errors are the most common type.

Similar presentations


Presentation on theme: "Exception Error handling. Exception 4 n An unusual occurrence during program execution that requires immediate handling n Errors are the most common type."— Presentation transcript:

1 Exception Error handling

2 Exception 4 n An unusual occurrence during program execution that requires immediate handling n Errors are the most common type of exception n Java enables centralization of exception handling code in a location that is most logical n 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.

3 Try/catch 4 n Try establishes a block of code that is to have its exceptions and abnormal exits (through break, continue,return, or exception propagation) handled n 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 exception

4 Try…catch 4 n try n statement1 n [catch(ExceptionClass1 e1) n statement2] n [catch(ExceptionClass2 e2) n statement3] n [finally n statementn] n The statement following try is executed unconditionally as part of the normal flow of execution

5 Try…catch 3 n 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) n If the exception is raised and not handled by a more tightly nested block or method, statement1 terminates execution and the interpreter checks if one of the catch blbocks can handle the exception n Every exception class implements the throwable interface

6 Exception 3 n Statements inside the catch block are executed when the respective exception type is matched n 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 n Exceptions not handled by any code eventually reach the Java default exception handler, which prints a message and terminates execution

7 Finally 3 n Code in the finally block is executed unconditionally after execution leaves the try block n 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 n The finally block is a logical place to put code that cleans up

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

9 Console.java n public static int readInt() n { String tmpX = readString().trim(); n try n { return Integer.parseInt(tmpX); } n catch(NumberFormatException ne) n { System.err.println("Not an integer. Quitting..."); n System.exit(-1); n return -1; } }


Download ppt "Exception Error handling. Exception 4 n An unusual occurrence during program execution that requires immediate handling n Errors are the most common type."

Similar presentations


Ads by Google