Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 12 Exception Handling and Text IO

Similar presentations


Presentation on theme: "Chapter 12 Exception Handling and Text IO"— Presentation transcript:

1 Chapter 12 Exception Handling and Text IO
When a program runs into a runtime error, the program terminates abnormally. Btw, a runtime error is also called an exception How can you handle the runtime error so that the program can continue to run or terminate gracefully? Exception Handling Exceptions are thrown from a method. The caller of the method can catch and handle the exception So an exception is an object that represents an error or condition Dr. Clincy Lecture

2 Example - Exception Handling
Let’s demonstrate exception handling and see how an exception object is created and thrown. Program below divides two integers and display their quotient If 0 is entered for the second number, the following runtime error will occur: Exception in thread “main” java.lang.ArithmeticExpection: / by zero at Quotient.main (Quotient.java:13) Dr. Clincy Lecture

3 Example - Exception Handling
A simple fix to the error would be to use an if statement to test the second number in making sure it is not 0. Let’s re-write this fix using a method instead……. Dr. Clincy Lecture

4 Example - Exception Handling
Let’s re-write the fix using a method instead……. Method for computing the quotient Terminates if 2nd number is 0 The problem with this is, you should not let a method terminate a program The “caller” or “user of the method” should decide whether to terminate Dr. Clincy Lecture

5 Exception Handling – Throw, Catch and Try
The “caller” or “user of the method” should decide whether to terminate So, how can a method notify its caller that an exception has occurred ? Java enables a method to throw an exception that can be caught and handled by the caller. How is this done ?: Throw statement: execution of the throw statement creates an exception object from the exception class – exception class being java.lang.ArithmeticException. The Throw statement “throws” the exception from one place to another. Catch block: when the exception is thrown, it is caught by the Catch block of code. The Catch block of code handles the exception Try block: the Try block of code contains the exception handling method – the Try block of code is also the code that would be implemented if no exception is realized Dr. Clincy Lecture

6 Example - Exception Handling
Let’s re-write the code using exception handling Exception object is created from constructor and thrown if there is a runtime error Try Block: this block of code in the main invokes the exception handling method Catch Block: this block of code is executed if an exception occurs Declares the type of exception that the handler can handle and must be the same name of a class that inherits from the throwable class. The handler can refer to the exception with variable name, ex. Instead of this, the following could have been used: ex.getMessage() – would return “Divisor cannot be zero” Unlike a regular method, after the Catch block is executed, control DO NOT return to the Throw statement, the next statement is executed

7 Template for try-throw-catch
Code to run; A statement or a method that may throw an exception More code to run; } catch(ExceptionType ex) { Code to process the exception; Next statement to execute after the catch block Exception Handling Advantages Now you see the advantages of using exception handling. It enables a method to throw an exception to its caller. Without this capability, a method must handle the exception or terminate the program. Dr. Clincy Lecture

8 Cover Lab 6 Dr. Clincy Lecture


Download ppt "Chapter 12 Exception Handling and Text IO"

Similar presentations


Ads by Google