1 / 22

Lecture10 Exception Handling

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

camdyn
Télécharger la présentation

Lecture10 Exception Handling

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lecture10Exception 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 ClassNotFoundException IOException ArithmeticException Exception AWTException NullPointerException RuntimeException IndexOutOfBoundsException Object Throwable Several more classes Several more classes LinkageError VirtualMachineError Error AWTError 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 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

  8. Exception Handling • The exception handling in Java consists of claiming exception, throwing exception, and catching and processing exceptions method1() clai m exception method2() throws Exception { { if (an error occurs) try { { catch exception throw exception throw new Exception(); invoke method2; } } } catch (Exception ex) { Process 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 method1 method2 { { { ... ... ... try try try { { { ... ... ... invoke method1; invoke method2; invoke method3; statement1; statement2; statement3; } } } catch (Exception1 ex1) catch (Exception2 ex2) catch (Exception3 ex3) { { { Process ex1; Process ex2 ; 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; }

More Related