Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Lecture10 Exception Handling Jaeki Song Introduction • Categories of errors – Compilation error • The rules of language have not been followed – Runtime error • The program is running if the environment detects an operation that is impossible to carry out – Logic error • A program doesn’t perform the way it was intended to Exception • Runtime errors causes exceptions – Events that occur during the execution of a program and disrupt the normal flow of control • A program that does not provide the code to handle exceptions may terminate abnormally, causing serious problem • E.g. – The user may enter invalid input – The program may attempt to open file that doesn’t exist – The program may attempt to access an out-of-bound array element Java and Exception ClassNotFoundException IOException ArithmeticException Exception AWTException NullPointerException RuntimeException IndexOutOfBoundsException Object Throwable Several more classes Several more classes LinkageError VirtualMachineError Error AWTError Several more classes Java and Exception • A java exception is an instance of a class derived from Throwable – Errors related to graphic are included in the java.awt package – Numeric exceptions are included in the java.lang package Exception Classes • The Exception class describes errors caused by your program and external circumstances – ClassNotFoundException • If you attempt to use a class that does not exist – RuntimeException • Describes programming errors, such as bad casting, accessing an out-of-bound array, and numeric errors Common Exceptions Exception Purpose ArithmetricException Error caused by a calculation, such as division by zero NumberFormatException Problem converting a string to a number; occurs when the text field is blank or contains a fraction when an integer is required IllegalArgumentException Unable to format the value passed to one of the format methods FileNotFoundException File does not exist in path specified IOException Failure of an input or output operation such as reading from a file OutOfMemoryException Not enough memory to create an object Exception Handling • The exception handling in Java consists of claiming exception, throwing exception, and catching and processing exceptions method1() { try { invoke method2; } catch (Exception ex) { Process exception; } catch exception } method2() throws Exception { if (an error occurs) { throw new Exception(); } } claim exception throw exception Claiming Exceptions • In general, every method states the types of exceptions it might encounter – Simply tells the compiler what might go wrong when the method is executing • To claim an exception in a method, you use the throws keyword public void myMethod( ) throws IOException public void myMethod( ) throws IOException, OtherExceptions Throwing Exceptions • When a statement causes errors, the method containing the statement creates an exception object and passes it to the system – The exception object contains information about exception, including its type when the error occurred • In the method that has claimed the exception, the following is the syntax to throw an exception: TheException ex = new TheException( ); throw ex Catching Exceptions • After a method throws an exception, the Java runtime systems begins the process of finding the code to handle the error – The code that handles the error is called the exception handler – The handler must match the type of exception thrown • If no handler is found, the program terminates Catching Exceptions • When calling a method that explicitly claims an exception, you must use the try-catch block try { statement; } //statements that may throw exceptions catch (Exception1 ex) { handler for exception1;} catch (Exception2 ex) {handler for exception2;} Catching Exceptions • If no exception arise during the execution of the try clause, the catch clauses are skipped • If one of the statement inside the try block throws an exception, Java skips the remaining statements and starts to search for a handler for the exception Catching Exceptions main method { ... try { ... invoke method1; statement1; } catch (Exception1 ex1) { Process ex1; } method1 { ... try { ... invoke method2; statement2; } catch (Exception2 ex2) { Process ex2; } method2 { ... try { ... invoke method3; statement3; } catch (Exception3 ex3) { Process ex3; } } } } Example • Example 1 • Example 2 The Exception Object • If an exception of a subclass of Exception occurs in a graphics program, Java prints the error message – The exception object contains valuable information about the exception The Exception Object • public String getMessage() – Returns the detailed message of the Throwable object • public String toString() – Returns a short description of the Throwable object • public void printStackTrace( ) – Prints the Throwable object and its trace information The finally Clause try { statements;} catch(TheException e) { handling e; } finally { finalStatements; } Cautions When Using Exceptions • Exception handling separates error-handling code from normal programming tasks, thus making programs easier to read and to modify. Be aware, however, that exception handling usually requires more time and resources because it requires instantiating a new exception object, rolling back the call stack, and propagating the errors to the Customized Exception Handling • Procedures – Step 1: Create own exception class by extending Exception and creating its own constructors • Your exception class can inherit from the Exception class public class NegativeAmountException extends Exception { public NegativeAmountException() { super (“Negative amount “); statement; } } Customized Exception Handling • Step2: Throws exception when certain conditions happens public void deposit (double amount) throws NegativeException { statement; } Customized Exception Handling • Step3: Provide the try and catch clauses for the thrown exception try { statement; } catch { statement; }