ECE 122 Engineering Problem Solving with Java Lecture 24 Exceptions
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
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.
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.
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
Handling Exceptions General form: try { statement(s); } catch (ExceptionType name) { statement(s); } finally { statement(s); }
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
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
Exception Class Hierarchy Java has a predefined set of exceptions and errors that can occur during execution. Exception (derived from Throwable) RunTimeException ClassNotFoundException IOException ArithmeticException NullPointerException IndexOutOfBoundsException This is just a small part of the Exception hierarchy NumberFormatException
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
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.
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
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