Presentation is loading. Please wait.

Presentation is loading. Please wait.

ECE122 L23: Exceptions December 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 24 Exceptions.

Similar presentations


Presentation on theme: "ECE122 L23: Exceptions December 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 24 Exceptions."— Presentation transcript:

1 ECE122 L23: Exceptions December 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 24 Exceptions

2 ECE122 L23: Exceptions December 6, 2007 Overview °Problem: Can we detect run-time errors and take corrective action? °Try-catch Test for a variety of different program situations °Java provides a series of test cases °Allows for graceful exit of program or treatment of special cases

3 ECE122 L23: Exceptions December 6, 2007 Errors °Errors do occur in programming. Problems opening a file, dividing by zero, accessing an out-of-bounds array element, hardware errors, and many more °The question becomes: What do we do when an error occurs? How is the error handled? Who handles it? Should the program terminate? Can the program recover from the error? Should it? °Java uses exceptions to provide the error-handling capabilities for its programs.

4 ECE122 L23: Exceptions December 6, 2007 Exceptions °An exception is an event that occurs during the execution of a program that disrupts the normal flow of instructions. Represented as an object in Java °Throwing an exception An error occurs within a method. An exception object is created and handed off to the runtime system. The runtime system must find the code to handle the error. °Catching an exception The system searches for code to handle the thrown exception. It can be in the same method or in some method in the call stack.

5 ECE122 L23: Exceptions December 6, 2007 Exceptions °An exception is an object that describes an unusual or erroneous situation °Exceptions are thrown by a program May be caught and handled by another part of the program °A program can be separated into a normal execution flow and an exception execution flow °An error is also represented as an object in Java Usually represents a unrecoverable situation and should not be caught

6 ECE122 L23: Exceptions December 6, 2007 Handling Exceptions °General form: try { statement(s); } catch (ExceptionType name) { statement(s); } finally { statement(s); }

7 ECE122 L23: Exceptions December 6, 2007 Exception Handling °Java has a predefined set of exceptions and errors that can occur during execution °A program can deal with an exception in one of three ways: ignore it handle it where it occurs handle it an another place in the program °The manner in which an exception is processed is an important design consideration

8 ECE122 L23: Exceptions December 6, 2007 Exception Handling °If exception is ignored by the program The program will terminate abnormally and produce an appropriate message °The message includes a call stack trace that: indicates the line on which the exception occurred shows the method call trail that lead to the attempted execution of the offending line

9 ECE122 L23: Exceptions December 6, 2007 Exception Class Hierarchy °Java has a predefined set of exceptions and errors that can occur during execution. Exception (derived from Throwable) ClassNotFoundException ArithmeticException IndexOutOfBoundsException NullPointerException IOExceptionRunTimeException This is just a small part of the Exception hierarchy NumberFormatException

10 ECE122 L23: Exceptions December 6, 2007 The try Statement °Handling an exception in a program The line that throws the exception is executed within a try block °A try block is followed by one or more catch clauses °Each catch clause has an associated exception type and is called an exception handler °When exception occurs: Processing continues at the first catch clause that matches the exception type

11 ECE122 L23: Exceptions December 6, 2007 Handling Exceptions °Three statements help define how exceptions are handled: try- identifies a block of statements within which an exception might be thrown catch - must be associated with a try statement and identifies a block of statements that can handle a particular type of exception. -The statements are executed if an exception of a particular type occurs within the try block. -A try statement can have multiple catch statements associated with it. finally - must be associated with a try statement - identifies a block of statements that are executed regardless of whether or not an error occurs within the try block. -Even if the try and catch block have a return statement in them, finally will still run.

12 ECE122 L23: Exceptions December 6, 2007 The finally Clause °A try statement can have an optional clause following the catch clauses Designated by the reserved word finally °The statements in the finally clause always are executed °If no exception is generated: The statements in the finally clause are executed after the statements in the try block complete °If an exception is generated: The statements in the finally clause are executed after the statements in the appropriate catch clause complete

13 ECE122 L23: Exceptions December 6, 2007 Summary °Exceptions form an important part of Java °Graceful error checking and handling allow for better designed code °Try-catch evaluates a number of possibilities °Often error messages are the result


Download ppt "ECE122 L23: Exceptions December 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 24 Exceptions."

Similar presentations


Ads by Google