Multiple Catch Statements
It is possible to have more than one catch statement in the catch block as illustrated below:
………………………
try
{
statement; // generates an exception
}
catch (Exception-Type-1 e)
{
statement; // process exception type 1
}
catch (Exception-Type-2 e)
{
statement; // process exception type 2
}
catch (Exception-Type-N e)
{
statement; // process exception type N
}
…………………………….
…………………………….
When an exception in a try block is generated, the Java treats the multiple catch statements like case in a switch statement. The first statement whose parameter matches with the exception object will be executed, and the remaining statements will be skipped.
Note that Java does not require any processing of the exception at all. We can simply have a catch statement with an empty block to avoid program abortion.
Example:
catch (Exception e);
The catch statement simply ends with a semicolon, which does nothing. This statement will catch an exception and then ignore it.
Using finally statement
Java supports another statement known as finally statement that can be used to handle an exception that is not caught by any of the previous catch statements. finally block can be used to handle any exception generated within a try block. It may be added immediately after the try block or after the last catch block shown as follows:
try
{
………..
}
catch (………)
{
………………..
………………..
}
.
.
finally
{
………………..
……………….
}
If a problem occurs during execution of the try block, the Java Virtual Machine (JVM) immediately stops executing the try block and looks for a catch block that can process that type of exception. Any remaining statements in the try block are not executed. The search begins at the first catch block. If the type of the exception object matches the type of the catch block parameter, those statements are executed. Otherwise, the remaining catch clauses are examined in sequence for a type match.
One of the biggest advantages of the Java exception handling mechanism is that it allows you to write software that is better organized and easier to understand. You no longer need to distribute code to check and handle errors throughout your program. Instead, a try block can be written and, if problems occur during its execution, control proceeds in a well-defined manner to the appropriate catch blocks.
When a catch block completes execution, control passes to the statements in the finally block. The Java compiler ensures that the finally block is executed in all circumstances. When a try block completes without problems, the finally block executes. Even if a return statement is included in a try block, the compiler ensures that the finally block is executed before the current method returns.
The finally block is optional. However, in some applications it can provide a useful way to close files or databases at this point. Each try block must have at least one catch or finally block. Otherwise, a compiler error occurs. The compiler also checks the type of each catch block parameter.
Note:
Hereby, the article "Exception Handling" is finished. If there will be any more topics related to it, then I'll post that and also you are invited to send any request that you would like to see on this blog on my e-mail ID.
Resources Used:
1. E Balagurusamy
2. SAMS Teach yourself
Compiled By: Chaudhary Amit V.
Comments
Post a Comment