An exception is a run-time error that occurs during code execution. Java provides a rich number of exception handling classes in its API and powerful exception handling mechanisms to deal with a variety of exceptions. Java also allows programmers to define custom designed classes to handle specific exceptions that may occur in particular applications.
Figure : Commonly used Exception Classes
Traditional Exception Handling Mechanism:
try { //try block
//Including all statements that possibly throw exceptions ...
} //catch clause
catch (ExceptionName1 e) {
//Exception handling message ...
}
catch (ExceptionName2 e) {
//Exception handling message ...
}
...
catch (ExceptionNameN e) {
//Exception handling message
...
}
finally {
//Optional //Message that must be processed ...
}
Important concepts of the traditional exception handling mechanism are:
• A try block normally has at least one catch block. It may also have an optional finally block. If a try block does not have a catch block, however, it must have a finally block, otherwise a syntax error will occur.
• A catch block must be associated with an accompanying try block.
• A finally block is always executed whether or not an exception occurs.
A try block may have multiple catch blocks but only one optional finally block. The finally block must be placed after all catch blocks or after the try block if no catch block is provided.
• A pair of braces must be used in each block of the try-catch-finally mechanism, even if only one statement is present in the block. Missing braces within block will result in the generation of a syntax error.
try { //try block
//Including all statements that possibly throw exceptions ...
} //catch clause
catch (ExceptionName1 e) {
//Exception handling message ...
}
catch (ExceptionName2 e) {
//Exception handling message ...
}
...
catch (ExceptionNameN e) {
//Exception handling message
...
}
finally {
//Optional //Message that must be processed ...
}
Important concepts of the traditional exception handling mechanism are:
• A try block normally has at least one catch block. It may also have an optional finally block. If a try block does not have a catch block, however, it must have a finally block, otherwise a syntax error will occur.
• A catch block must be associated with an accompanying try block.
• A finally block is always executed whether or not an exception occurs.
A try block may have multiple catch blocks but only one optional finally block. The finally block must be placed after all catch blocks or after the try block if no catch block is provided.
• A pair of braces must be used in each block of the try-catch-finally mechanism, even if only one statement is present in the block. Missing braces within block will result in the generation of a syntax error.
Example :
Question : What will happen in the code below ?
try {
int value = Integer.parseInt("12ab");
// Other statements …
}
Answer :
Executing the statement will automatically throw a NumberFormatException object. This action is also known as an “implicit throw,” since it is thrown automatically by the JVM.
The flow of code execution will then jump out of the try block, ignoring any remaining statements in the try block, and resume at the first catch block provided in the code.
Lets Assume that we have two catch blocks :
catch (InputMismatchException e) {
//Process the exception and display the message …
}
catch (NumberFormatException e) {
//Process the exception and display the message …
}
The evaluation test determines the exception object does not match the specification of this catch statement and execution transfers to the second catch block. The second catch block is executed because a match exists between the thrown object and the specified argument type. The statements in the second catch block are executed and the exception is handled.
In situations wherein more than one thrown object matches the argument specified in the catch blocks, only the first matched catch block will be executed.
The order of specifying catch blocks is important since only the first catch block will be executed if more than one catch block matches the thrown object. As such, catch blocks should be listed in order from most specific to general.
Finally Block :
A finally block should be used in the following situations:
• When statements must be executed after completing the successful processing of statements in the try block, e.g., to stop thread execution.
• When statements must be executed after an exception has been handled, e.g., issuing a return statement of a method class to close a file.
• When statements must be executed in the absence or failure to match the exception specified in the provided list of catch blocks before the JVM assumes processing of the exception. Examples include releasing memory space and closing audio, video, or picture display mode.
Example :
finally {
System.out.println("Your verification of the data has failed after 4 attempts. “ + "Your session will be logged off. ");
verifying.off(); //close the operation
}
try {
int value = Integer.parseInt("12ab");
// Other statements …
}
Answer :
Executing the statement will automatically throw a NumberFormatException object. This action is also known as an “implicit throw,” since it is thrown automatically by the JVM.
The flow of code execution will then jump out of the try block, ignoring any remaining statements in the try block, and resume at the first catch block provided in the code.
Lets Assume that we have two catch blocks :
catch (InputMismatchException e) {
//Process the exception and display the message …
}
catch (NumberFormatException e) {
//Process the exception and display the message …
}
The evaluation test determines the exception object does not match the specification of this catch statement and execution transfers to the second catch block. The second catch block is executed because a match exists between the thrown object and the specified argument type. The statements in the second catch block are executed and the exception is handled.
In situations wherein more than one thrown object matches the argument specified in the catch blocks, only the first matched catch block will be executed.
The order of specifying catch blocks is important since only the first catch block will be executed if more than one catch block matches the thrown object. As such, catch blocks should be listed in order from most specific to general.
Finally Block :
A finally block should be used in the following situations:
• When statements must be executed after completing the successful processing of statements in the try block, e.g., to stop thread execution.
• When statements must be executed after an exception has been handled, e.g., issuing a return statement of a method class to close a file.
• When statements must be executed in the absence or failure to match the exception specified in the provided list of catch blocks before the JVM assumes processing of the exception. Examples include releasing memory space and closing audio, video, or picture display mode.
Example :
finally {
System.out.println("Your verification of the data has failed after 4 attempts. “ + "Your session will be logged off. ");
verifying.off(); //close the operation
}