Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 12 Exception Handling

Similar presentations


Presentation on theme: "Chapter 12 Exception Handling"— Presentation transcript:

1 Chapter 12 Exception Handling
You have learned that: a runtime error is also called an exception You have learned that: an exception is an actual object that represents an error or condition. You have learned that: Exceptions are thrown from a method. You have learned that: the caller of the method can catch and handle the exception. You had the opportunity to create a custom exception handler. What’s new ?: there are “canned” or “pre-existing” exceptions What’s new ?: those “pre-existing” exceptions are objects, and objects are defined using classes What’s new ?: the root class for those “pre-existing” exceptions is java.lang.Throwable Dr. Clincy Lecture

2 Exception Types There are many predefined exception classes in the Java API Classified into three major types: (1) System Errors, (2) Exceptions, and (3) Runtime Exceptions . Btw, all are “runtime” errors Dr. Clincy Lecture

3 System Errors System errors are thrown by the JVM and are represented in the Error class. The Error class describes internal system errors. Such errors rarely occur. If one does, there is little you can do beyond notifying the user and trying to terminate the program gracefully. Two classes are dependent on one another and one of them is no longer compatible after a compilation The JVM is broken or has run out of the needed resources to operate Dr. Clincy Lecture

4 Exceptions Exception describes errors caused by your program and external circumstances. These errors can be caught and handled by your program. Attempt to use a class that doesn’t exist Invalid input, reading past end of file, and opening a nonexistent file Dr. Clincy Lecture

5 Exceptions continuing …
Exception describes errors caused by your program and external circumstances. These errors can be caught and handled by your program. Dividing integer by zero. Programming type errors Attempting to access an object through a null reference variable Index to an array out of bound Method passing an argument that is illegal or inappropriate Dr. Clincy Lecture

6 Checked Exceptions vs. Unchecked Exceptions
RuntimeException, Error and their subclasses are known as unchecked exceptions – meaning that the compiler does NOT force the programmer to deal with the exception. All other exceptions are known as checked exceptions, meaning that the compiler forces the programmer to check and deal with the exceptions. In most cases, unchecked exceptions reflect programming logic errors that are not recoverable. Unchecked exceptions can occur anywhere in the program. To avoid cumbersome overuse of try-catch blocks, Java does not mandate you to write code to catch unchecked exceptions. Dr. Clincy Lecture

7 Declaring, Throwing, and Catching Exceptions
We already know that Java’s exception-handling model is based on three operations: (1) declaring an exception, (2) throwing an exception, and (3) catching an exception Declaring Exceptions Every method must state the types of checked exceptions it might throw. This is known as declaring exceptions. Should be declared as follows: public void myMethod() throws IOException public void myMethod() throws Exception1, …,ExceptionN Just one exception Multiple exceptions NOTE: if a method does not declare exceptions in the superclass, you cannot override it to declare exceptions in the subclass. Dr. Clincy Lecture

8 Throwing Exceptions Example
When the program detects an error, the program can create an instance of an appropriate exception type and throw it. This is known as throwing an exception. Should be declared as follows: throw new TheException(); Throwing Exceptions Example /** Set a new radius */ public void setRadius(double newRadius)throws IllegalArgumentException { if (newRadius >= 0) radius = newRadius; else throw new IllegalArgumentException("Radius cannot be negative"); } Declaring the exception at the beginning of the Method If error is detected – this code creates an instance of the exception and throws it. Dr. Clincy Lecture

9 Catching Multiple Exceptions
You know how to declare multiple exceptions … and you also know how to throw multiple exceptions. Multiple exceptions can be caught and handled in a try-catch bock. For example: try { statements; // Statements that may throw exceptions } catch (Exception1 exVar1) { handler for exception1; catch (Exception2 exVar2) { handler for exception2; ... catch (ExceptionN exVar3) { handler for exceptionN; NOTE: if no exceptions arise during execution of the try block, the catch blocks are skipped. Dr. Clincy Lecture

10 Getting Info from Exceptions
Exception object contains valuable info about the exception. The following methods may be used: Great for debugging runtime errors Provides step-by-step access to trace stack info printed by printStackTrace() Dr. Clincy Lecture

11 The finally Clause try { statements; } catch(TheException ex) {
Sometimes you will want some code to be executed regardless of whether an exception occurs or not. The finally clause can be used to accomplish this. try { statements; } catch(TheException ex) { handling ex; finally { finalStatements; Dr. Clincy Lecture

12 Let’s cover Lab 7 now Dr. Clincy Lecture


Download ppt "Chapter 12 Exception Handling"

Similar presentations


Ads by Google