Presentation is loading. Please wait.

Presentation is loading. Please wait.

Exception: Java Error Handling Yingcai Xiao. Error Handling In-line if(error) System.out.println(“Error occurred here.”); Block-based if(error) { ErrorCode.

Similar presentations


Presentation on theme: "Exception: Java Error Handling Yingcai Xiao. Error Handling In-line if(error) System.out.println(“Error occurred here.”); Block-based if(error) { ErrorCode."— Presentation transcript:

1 Exception: Java Error Handling Yingcai Xiao

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

3 Error Handling Function-based if(error) ErrorHandlingFunction(ErrorCode); 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.

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

5 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.

6 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.

7 Try and Catch try { // Code that might generate exceptions // Don’t create objects here if they are to be used elsewhere. } catch(Type1 id1) { // Handle exceptions of Type1 } catch(Type2 id2) { // Handle exceptions of Type2 } catch(Type3 id3) { // Handle exceptions of Type3 }

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

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

10 Some Methods in the Exception Class Exception is a child of throwable, which in turn is a child of Object. String getMessage( ) String getLocalizedMessage( ) String toString( ) void printStackTrace( ) void printStackTrace(PrintStream) void printStackTrace(PrintWriter) Class getClass( )

11 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; }

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


Download ppt "Exception: Java Error Handling Yingcai Xiao. Error Handling In-line if(error) System.out.println(“Error occurred here.”); Block-based if(error) { ErrorCode."

Similar presentations


Ads by Google