Exception Handling In Java

Introduction To Exception Handling

Exception Handling in Java basically brings run-time error management into the object oriented world. An exception is an abnormal condition that arises in a code sequence at a run time. The hierarchy of the Exception class can be given as-:
THROWABLE(super class occupying the top most position in the hierarchy)
• Exception – for exceptional conditions that user defined programs should catch. Exceptions like division by zero and invalid array indexing come under this class.
• Error -defines exceptions that are not expected to be caught under normal circumstances by the user defined program. This class includes exceptions like Stack overflow.
Now before we learn to handle exceptions let us consider the need of Exception Handling .Let us consider a small example-
class Inst
{
public static void main(String args[])
{
int b=0;
int a=64/b; //division by zero
}
}
When the Java run-time system detects the attempt of division by zero ,it immediately constructs a new exception object ,throws the exception and stops the execution of the program (throwing an exception means indicating that an unexpected error has occurred ).Since an exception has been detected the method cannot be executed till it has been handled.
This exception can be handled in two ways-
• Using the Default Handler (when the programmer has not provided the exception handling code).
The output in this case is as follows-
java.lang.ArithmeticException:/ by zero
at Inst.main(Inst.java:4)
This specifies the subclass of Exception, Arithmetic Exception;exception occurred,/ by zero;class name,Inst; the method name,main; the file name,Inst.java; line number,4. However it causes termination of program.
• Using the User defined Exception Handling code
This has two major advantages-:
1. It allows us to fix the error.
2. It prevents the program from terminating automatically
User defined Exception Handling uses the try and catch block.In this case the Arithmetic Exception generated by the division by zero error can be handled as-:

class Inst
{
public static void main(String args[])
{
int b,a;
try //to monitor the block of code for exceptions
{
b=0;
a=64/b;
System.out.println(“This will not be printed.”);
}
catch(ArithmeticException e) //to handle divide by zero error
{
System.out.println(“Division by zero.”);
}
System.out.println(“After catch statement.”);
}
}

OUTPUT

Division by zero
After catch statement
**The call to println() inside the try block is never executed.Once an exception is thrown,program control transfers out of the try block into he catch block.So the line”This will not be printed” is never displayed.After the execution of catch statement program control continues with the next line in the program following the try/catch mechanism.
In certain cases ,due to Exception Handling,a non linear path of execution of code is followed which alters the normal flow through the method.For eg.if a method opens a file upon entry and closes it upon exit,then it is not desired that the closing of file is bypassed by the exception handling mechanism.FINALLY is used for this.
FINALLY-
It creates a block of code that will be executed after a try/catch block has completed execution and before the code following the try/catch block.It will execute whether or not the exception is thrown.If an exception is thrown finally block is executed even if no catch matches the exception.
Example illustrating use of Finally block-
class student
{
static void Demo1()
{
try
{
System.out.println(“Inside Demo1”);
throw new RuntimeException(“demo”);
}
finally
{
System.out.println(Demo1’s finally”);
}
}
static void Demo2( )
{
try
{
System.out.println(“Inside Demo2”);
}
finally
{
System.out.println(“Demo2’s finally”);
}
}
public static void main(String args[])
{
try
{
Demo1( );
}
catch(Exception e)
{
System.out.println(“Exception caught”);
}
Demo2( );
}
}
OUTPUT-
Inside Demo1
Demo1’s finally
Exception caught
Inside Demo2
Demo2’s finally
**If finally is associated with try,it will be executed upon conclusion of try.The order is try->finally->catch.
KEY POINTS
• Scope of each catch clause is restricted to those statements specified by the immediately preceding try statement.Catch statement cannot catch an exception thrown by another try statement.
• Finally clause is optional.However,each statement requires atleast one catch or a finally clause.
• Difference between Throw and Throws-
THROW statement is used by the program to throw an exception explicitly which can be handled by the catch code.
However if a method is capable of causing an exception that it does not handle,it must specify this behavior so that callers of the method can guard themselves against the exception.this is done by the THROWS clause in the method declaration.

Comments on this entry are closed.

css.php