Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture10 Exception Handling Jaeki Song. Introduction Categories of errors –Compilation error The rules of language have not been followed –Runtime error.

Similar presentations


Presentation on theme: "Lecture10 Exception Handling Jaeki Song. Introduction Categories of errors –Compilation error The rules of language have not been followed –Runtime error."— Presentation transcript:

1 Lecture10 Exception Handling Jaeki Song

2 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

3 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

4 Java and Exception LinkageError Error AWTError AWTException Throwable ClassNotFoundException VirtualMachineError IOException Exception RuntimeException Object ArithmeticException NullPointerException IndexOutOfBoundsException Several more classes

5 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

6 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

7 Common Exceptions ExceptionPurpose ArithmetricExceptionError 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 OutOfMemoryExceptionNot enough memory to create an object

8 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; } } method2() throws Exception { if (an error occurs) { throw new Exception(); } } catch exception throw exception claim exception

9 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

10 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

11 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

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

13 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

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

15 Example Example 1 Example 2

16 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

17 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

18 The finally Clause try { statements;} catch(TheException e) { handling e; } finally { finalStatements; }

19 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 calling methods.

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

21 Customized Exception Handling Step2: Throws exception when certain conditions happens public void deposit (double amount) throws NegativeException { statement; }

22 Customized Exception Handling Step3: Provide the try and catch clauses for the thrown exception try { statement; } catch { statement; }


Download ppt "Lecture10 Exception Handling Jaeki Song. Introduction Categories of errors –Compilation error The rules of language have not been followed –Runtime error."

Similar presentations


Ads by Google