Java Exception Handling
Java Exception Handling
Exception Handling
Exception
Introduction
• An exception is an event, which occurs during the
execution of a program, that disrupts the normal flow of
the program's instructions.
Exception Error
Checked Exception
Unchecked Exception
Types of Exceptions
Checked Exception
• Checked Exceptions are those, that have to be either
caught or declared to be thrown in the method in which
they are raised.
Unchecked Exception
• Unchecked Exceptions are those that are not forced by
the compiler either to be caught or to be thrown.
• ArrayIndexOutOfBoundsException
• NumberFormatException
• NullPointerException
• IOException
Commonly used sub-classes of Errors
• VirualMachineError
• StackOverFlowError
• NoClassDefFoundError
• NoSuchMethodError
Scenarios where exceptions may
occur
1) Scenario where ArithmeticException occurs
If we divide any number by zero, there occurs an
ArithmeticException.
int a=50/0;//ArithmeticException
java.lang.ArithmeticException: / by zero at
Exc0.main(Exc0.java:4)
Why Exception Handling?
• When the default exception handler is provided by the
Java run-time system , why Exception Handling?
• try
• catch
• throw
• throws
• finally
Keywords for Exception Handling
try
• Java try block is used to enclose the code that might
throw an exception. It must be used within the method.
try {
Statements whose execution may cause an
exception
}
catch
• catch is used to define a handler.
Output:
Compile-time error
Nested try block
• Sometimes a situation may arise where a part of a block may cause one error and
the entire block itself may cause another error. In such cases, exception handlers
have to be nested.
• Syntax:
• ....
• try
• {
• statement 1;
• statement 2;
• try
• {
• statement 1;
• statement 2;
• }
• catch(Exception e)
• {
• }
• }
• catch(Exception e)
• {
• }
• ....
• class Excep6{
• public static void main(String args[]){
• try{
• try{
• System.out.println("going to divide");
• int b =39/0;
• }catch(ArithmeticException e){System.out.println(e);}
•
• try{
• int a[]=new int[5];
• a[5]=4;
• }catch(ArrayIndexOutOfBoundsException e){System.out.println(e);}
•
• System.out.println("other statement”);
• }catch(Exception e){System.out.println("handeled");}
•
• System.out.println("normal flow..");
• }
• }
Java finally block
• Java finally block is always executed whether
exception is handled or not.
• By the help of custom exception, you can have your own exception
and message.
Output:exception handled
normal flow...
• In the above example exception occurs in m()
method where it is not handled,
• So it is propagated to previous n() method where
it is not handled, again it is propagated to p()
method where exception is handled.
• Exception can be handled in any method in call
stack either in main() method,p() method,n()
method or m() method.
• Rule: By default, Checked Exceptions are not forwarded in calling chain (propagated).
• class TestExceptionPropagation2{
• void m(){
• throw new java.io.IOException("device error");//checked exception
• }
• void n(){
• m();
• }
• void p(){
• try{
• n();
• }catch(Exception e){System.out.println("exception handeled");}
• }
• public static void main(String args[]){
• TestExceptionPropagation2 obj=new TestExceptionPropagation2();
• obj.p();
• System.out.println("normal flow");
• }
• }
• Output:Compile Time Error
Defining Generalized Exception Handler
• The Exception class does not define any methods of its own.
• Exception( )
• Exception(String msg)
Syntax:
try(resource-specification)
{ //use the resource }
catch(Exception ex) {...}
Try with Resource Statement
• The try statement contains a parenthesis in which one or more
resources is declared.