5 Exception Handling
5 Exception Handling
Chapter Five
Exception-Handling Fundamentals
thefollowing program includes a try block and a catch clause which processes the Arithmetic Exception generated
by the division-by-zero error:
class tryAndCatch
{
public static void main(String args[])
{
int d, a;
try
{ // monitor a block of code.
d = 0;
a = 42 / d;
System.out.println("This will not be printed.");
}
catch (ArithmeticException e)
{ // catch divide-by-zero error
System.out.println("Division by zero.");
}
System.out.println("After catch statement.");
}
}
throw
class ThrowDemo {
static void demoproc() {
try
{
throw new NullPointerException("demo");
}
catch(NullPointerException e) {
System.out.println("Caught inside demoproc.");
throw e; // rethrow the exception
}}
public static void main(String args[]) {
try
{
demoproc();
}
catch(NullPointerException e){
System.out.println("Recaught: " + e);
}}}
Cont....
The program gets two chances to deal with the same error.
First, main( ) sets up an exception context and then calls demoproc( ).
The demoproc( ) method then sets up another exception-handling context
and immediately throws a new instance of NullPointerException, which is
caught on the next line, then exception is rethrown.
Output :
Caught inside demoproc.
Recaught: java.lang.NullPointerException: demo
throws
class ThrowsDemo
{
static void throwOne() throws IllegalAccessException
{
System.out.println("Inside throwOne.");
throw new IllegalAccessException("demo");
}
public static void main(String args[])
{
try {
throwOne();
}
catch (IllegalAccessException e)
{
System.out.println("Caught " + e);
}
}
}
Output :
inside throwOne
caught java.lang.IllegalAccessException: demo
finally
finally creates a block of code that will be executed after a try/catch block
has completed and before the code following the try/catch block.
The finally block will execute whether or not an exception is thrown.
If an exception is thrown, the finally block will execute even if no catch
statement matches the exception.
The finally clause is optional. However, each try statement requires at least
one catch or a finally clause.
Cont….
Here is an example program that shows three methods that exit in various ways, none without executing
their finally clauses:
Example :
class FinallyDemo
{
// Through an exception out of the method.
static void procA()
{
try
{
System.out.println("inside procA");
throw new RuntimeException("demo");
}
Finally
{
System.out.println("procA's finally");
}
}
Cont….
In this example, procA( ) prematurely breaks out of the try by throwing an exception.
The finally clause is executed on the way out. procB( )’s try statement is exited via a
return statement. The finally clause is executed before procB( ) returns. In procC( ),
the try statement executes normally, without error. However, the finally block is still
executed.
If a finally block is associated with a try, the finally block will be executed upon
conclusion of the try.
Output :
inside procA
procA’s finally
Exception caught
inside procB
procB’s finally
inside procC
procC’s finally
THANK YOU
Any Questions?