- Home
- Objectives

- XyzWs Study Guides
- Study Guides
- Study Notes
- Resources

- Mock Exams
SCJP Study Guide:
Flow Control
Printer-friendly version |
Mail this to a friend
Exception Handling
Errors! Everyone tries to avoid them, but it's an unfortunate fact: Errors occur in software programs. However, if you handle errors properly, you'll greatly improve programs' readability, reliability and maintainability. The Java programming language uses exceptions for error handling.
The term exception is shorthand for the phrase "exceptional event."
Definition: An exception is an event, which occurs during the execution of a program, that disrupts the normal flow of the program's instructions.
In Java, errors are handled by throwing and catching exceptions. Throwing an exception when an error occurs is a way of signaling that something has gone wrong. Catching an exception is a way of dealing with the exception.
Three statements play a part in handling exceptions:
- The try statement identifies a block of statements within which an exception might be thrown.
-
The catch statement must be associated with a
trystatement 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 thetryblock. -
The finally statement must be associated with a
trystatement and identifies a block of statements that are executed regardless of whether or not an error occurs within thetryblock.
Here's the general form of these statements.
try {
statement(s)
} catch (exceptiontype1 name) {
statement(s)
} catch (exceptiontype2 name) {
statement(s)
.
.
.
}
finally {
statement(s)
}
Notes:
-
The try clause MUST be followed by at least one catch clause or one finally clause, i.e., can not have a try clause by itself. Otherwise, a compiler error will occur. If all three clauses are presented, the order is important.
-
The catch clause, if it is presented, must always associate with a try clause. A finally clause, if it is presented, must always associate with a try clause. Otherwise, a compiler error will occur.
-
In general, there must be one or more catch clauses accompany with a try clause. If there is a finally clause accompany with a try clause, there can be ZERO catch clauses.
- Each catch clause specifies one exception type that it is prepared to handle, and provides a name for it (similar to the way a function header specifies the type and name of a parameter). Java exceptions are objects, so the statements in a catch clause can refer to the thrown exception object using the specified name. Code in the catch block is used to handle the exception object.
-
If catch clauses are presented, the code in try clause must throw exceptions specified in the catch clauses, otherwise compiler will produce an error. (This is not the case for super-class Exception)
-
More than one catch clause can be used to catch different excepiton types after the same try block. However, the order that the exceptions are listed in the catch clauses is sometimes important. More specific classes of exceptions must be listed before more general classes that could catch the same exception.
-
The finally clause is optional and is placed after the last catch clause. You can place one and only one finally clause. The code in the finally clause will ALWAYS be executed whether or not an exception occurs. So code in the finally block is normally used to perform some type of cleanup. For example, close a socket connection or an opened file.
-
The code in a finally statement is executed even if no catch clause catches the exception sa well as even if a return statement is executed within the try or one of the catch clauses.
-
System.exit() and error conditions are the only exceptions where finally block is not executed.
- In the catch block, you can re-throw the catched exception again or re-throw a new checked exception that is declared in throws clause of your method header. If an exception handler re-throws an exception (throw in a catch block), same rules apply. Either you need to have a try/catch within the catch or specify the entire method as throwing the exception that?s being re-thrown in the catch block. Catch blocks at the same level will not handle the exceptions thrown in a catch block ? it needs its own handlers.
-
If there was no exception or the exception was handled, control flows to the finally clause statement, if there is one, and then to the first statement following the try/catch/finally blocks. After an exception has been handled the program execution resumes after the try/catch block, not after the throw statement!.
-
If the exception is not handled, the process repeats looking for next enclosing try block up the call hierarchy. If this search reaches the top level of the hierarchy (the point at which the thread was created), then the thread is killed and message stack trace is dumped to System.err.
-
Any variable declared within the try block is de-allocated before the catch statement is executed. Hence if you want access to a variable used or assigned a value within the try block, make sure you declare that variable outside the try block.