Exception Java Error Handling Yingcai Xiao Error Handling

  • Slides: 12
Download presentation
Exception: Java Error Handling Yingcai Xiao

Exception: Java Error Handling Yingcai Xiao

Error Handling In-line if(error) System. out. println(“Error occurred here. ”); Block-based if(error) { Error.

Error Handling In-line if(error) System. out. println(“Error occurred here. ”); Block-based if(error) { Error. Code = Error. Type. One; goto Error. Handling. Block; } … Error. Handling. Block: switch(Error. Code){ case Error. Type. One: … break; … }

Error Handling Function-based if(error) Error. Handling. Function(Error. Code); Event-based & Object-oriented An event is

Error Handling Function-based if(error) Error. Handling. Function(Error. Code); Event-based & Object-oriented An event is generated when an error occurs. Event handling functions are error handling functions registered to handle “error” events. In Java, an error event is called an exception. Error handling functions are called exception handlers. An object that describes an error is called an exception object. An exception is an unexpected condition preventing the normal execution of the code. Note: some errors are expected conditions. For example, the user gives an out-of-range input.

Exception Based Error Handling 1. 2. 3. 4. 5. 6. 7. 8. 9. Create

Exception Based Error Handling 1. 2. 3. 4. 5. 6. 7. 8. 9. Create an exception object when an error condition occurs. “throw” the exception object. Current path of execution is stopped. (done by the Java Interpreter) Execution “jumps” to a registered exception handler (the nearest one and go no further). (done by the Java Interpreter) The event handler handles the error and determines the path of continuation. e. g. : My. Object o = new My. Object(); if(o == null) throw new Null. Pointer. Exception();

Exception Based Error Handling Note: • Exceptions can be “throw”ed anywhere in the code.

Exception Based Error Handling Note: • Exceptions can be “throw”ed anywhere in the code. • Exception handlers can be defined anywhere as needed. • Low level library methods may throw exceptions but expecting the methods’ users to handle the exceptions. • Standard Java exceptions can be found at java. sun. com. • When overriding a method, you can throw only the exceptions that have been specified in the base-class version of the method.

Try and Catch The try block: a guarded code region that may produce exceptions

Try and Catch The try block: a guarded code region that may produce exceptions The method hosting the try block will exit upon the throwing of an exception, unless the exception is caught by a “catch” block right after the try block.

Try and Catch try { // Code that might generate exceptions // Don’t create

Try and Catch try { // Code that might generate exceptions // Don’t create objects here if they are to be used elsewhere. } catch(Type 1 id 1) { // Handle exceptions of Type 1 } catch(Type 2 id 2) { // Handle exceptions of Type 2 } catch(Type 3 id 3) { // Handle exceptions of Type 3 }

Creating a New Exception Class // define a new exception class by subclassing //

Creating a New Exception Class // define a new exception class by subclassing // from the “Exception” class Simple. Exception extends Exception {} public class Simple. Exception. Demo { // Any exceptions to be thrown out should be // listed in the function header public void f() throws Simple. Exception { System. out. println( "Throwing Simple. Exception from f()"); throw new Simple. Exception (); }

Using a New Exception Class public static void main(String[] args) { Simple. Exception. Demo

Using a New Exception Class public static void main(String[] args) { Simple. Exception. Demo sed = new Simple. Exception. Demo(); try { sed. f(); // exceptions thrown out from f } catch(Simple. Exception e) { System. err. println("Caught it!"); } } } ///: ~

Some Methods in the Exception Class Exception is a child of throwable, which in

Some Methods in the Exception Class Exception is a child of throwable, which in turn is a child of Object. String get. Message( ) String get. Localized. Message( ) String to. String( ) void print. Stack. Trace(Print. Stream) void print. Stack. Trace(Print. Writer) Class get. Class( )

Rethrowing an Exception Rethrowing an exception causes the exception to go to the exception

Rethrowing an Exception Rethrowing an exception causes the exception to go to the exception handlers in the next-higher context. catch(Exception e) { System. err. println("An exception was thrown"); throw e; }

Performing cleanup with finally try { // The guarded region: Dangerous activities // that

Performing cleanup with finally try { // The guarded region: Dangerous activities // that might throw A, B, or C } catch(A a 1) { // Handler for situation A } catch(B b 1) { // Handler for situation B } catch(C c 1) { // Handler for situation C } finally { // Activities that happen every time // regardless exceptions are thrown or not }