Java

 

 

 

 

Exception Handling

 

 

 

Why We need Exception Handling ?

 

 

Exception01.java

public class Exception01 {

 

    public static void main(String[] args) {

 

        double a = 5/0;

        System.out.println(a);

 

        System.out.println("Reached the end of the program");

    }

 

}

 

 

    C:\temp> java Exception01

     

    Exception in thread "main" java.lang.ArithmeticException: / by zero

    at Exception01.main(Exception01.java:5)

     

    NOTE : Program crashed and 'System.out.println("Reached the end of the program");' is not executed.

 

 

 

How to Capture the Exceptions ?

 

 

Exception02.java

public class Exception02 {

 

    public static void main(String[] args) {

            

        try {

            

            double a = 5/0;

            System.out.println(a);

            

    

        } catch (Exception e) {

            System.out.println("An Exception occurred with following details :");

            System.out.println("   " + e);

            System.out.println("   " + e.toString());

            System.out.print("   ");

            e.printStackTrace();

            System.out.println("   " + e.getClass());

            System.out.println("   " + e.getMessage());

        };

        

        System.out.println("Reached the end of the program");

        

    }

 

}

 

 

    C:\temp> java Exception02

     

    An Exception occurred with following details :

       java.lang.ArithmeticException: / by zero <-- System.out.println("   " + e);

       java.lang.ArithmeticException: / by zero <-- System.out.println("   " + e.toString());

       java.lang.ArithmeticException: / by zero <-- e.printStackTrace();

            at Exception02.main(Exception02.java:8)

       class java.lang.ArithmeticException <-- System.out.println("   " + e.getClass());

       / by zero  <--

    Reached the end of the program  <-- System.out.println("Reached the end of the program");

     

    NOTE : Exception occurred but program move forward

 

 

 

How to Capture a Specific Exception ?

 

 

Exception03.java

public class Exception03 {

 

    public static void main(String[] args) {

            

        try {

            

            double a = 5/0;

            System.out.println(a);

            

    

        } catch (ArithmeticException e) {

            System.out.println("An ArithmeticException occurred with following details :");

            System.out.println("   " + e);

            System.out.println("   " + e.toString());

            System.out.print("   ");

            e.printStackTrace();

            System.out.println("   " + e.getClass());

            System.out.println("   " + e.getMessage());

        };

        

        System.out.println("Reached the end of the program");

        

    }

 

}

 

 

    C:\temp> java Exception03

     

    An ArithmeticException occurred with following details :

       java.lang.ArithmeticException: / by zero <-- System.out.println("   " + e);

       java.lang.ArithmeticException: / by zero <-- System.out.println("   " + e.toString());

       java.lang.ArithmeticException: / by zero <-- e.printStackTrace();

            at Exception03.main(Exception03.java:8)

       class java.lang.ArithmeticException <-- System.out.println("   " + e.getClass());

       / by zero  <-- System.out.println("   " + e.getMessage));

    Reached the end of the program  <-- System.out.println("Reached the end of the program");

     

    NOTE : Exception occurred but program move forward

 

 

How to Capture a Multiple Exceptions ?

 

 

Exception04.java

public class Exception04 {

 

    public static void main(String[] args) {

            

        try {

            

            int ary[]= {0,1,2,3,4};  

            ary[5]=30;

    

        } catch (ArithmeticException e) {

            System.out.println("An ArithmeticException occurred with following details :");

            System.out.println("   " + e);

            System.out.println("   " + e.toString());

            System.out.print("   ");

            e.printStackTrace();

            System.out.println("   " + e.getClass());

            System.out.println("   " + e.getMessage());

        } catch (ArrayIndexOutOfBoundsException e) {

            System.out.println("An ArrayIndexOutOfBoundsException occurred with following details :");

            System.out.println("   " + e);

            System.out.println("   " + e.toString());

            System.out.print("   ");

            e.printStackTrace();

            System.out.println("   " + e.getClass());

            System.out.println("   " + e.getMessage());

       } catch (Exception e) { // This part is executed if the exception is not trapped above

            System.out.println("An Exception occurred with following details :");

            System.out.println("   " + e);

            System.out.println("   " + e.toString());

            System.out.print("   ");

            e.printStackTrace();

            System.out.println("   " + e.getClass());

            System.out.println("   " + e.getMessage());

        } ;

        

        System.out.println("Reached the end of the program");

        

    }

 

}

 

 

    C:\temp> java Exception04

     

    An ArrayIndexOutOfBoundsException occurred with following details :

       java.lang.ArrayIndexOutOfBoundsException: 5 <-- System.out.println("   " + e);

       java.lang.ArrayIndexOutOfBoundsException: 5 <-- System.out.println("   " + e.toString());

       java.lang.ArrayIndexOutOfBoundsException: 5 <-- e.printStackTrace();

            at Exception04.main(Exception04.java:12)

       class java.lang.ArrayIndexOutOfBoundsException <-- System.out.println("   " + e.getClass());

         5 <-- System.out.println("   " + e.getMessage());

    Reached the end of the program  <-- System.out.println("Reached the end of the program");

     

    NOTE : Exception occurred but program move forward

 

 

Exception05.java

import java.io.*;

 

public class Exception05 {

 

    public static void main(String[] args) {

            

        try {

            

            int ary[]= {0,1,2,3,4};  

            ary[5]=30;

    

        } catch (ArithmeticException e) {

            System.out.println("An ArithmeticException occurred with following details :");

            System.out.println("   " + e);

            System.out.println("   " + e.toString());

            System.out.print("   ");

            e.printStackTrace();

            System.out.println("   " + e.getClass());

            System.out.println("   " + e.getMessage());

        } catch (ArrayIndexOutOfBoundsException e) {

            System.out.println("An ArrayIndexOutOfBoundsException occurred with following details :");

            System.out.println("   " + e);

            System.out.println("   " + e.toString());

            System.out.print("   ");

            e.printStackTrace();

            System.out.println("   " + e.getClass());

            System.out.println("   " + e.getMessage());

       } catch (Exception e) { // This part is executed if the exception is not trapped above

            System.out.println("An Exception occurred with following details :");

            System.out.println("   " + e);

            System.out.println("   " + e.toString());

            System.out.print("   ");

            e.printStackTrace();

            System.out.println("   " + e.getClass());

            System.out.println("   " + e.getMessage());

        } ;

        

        System.out.println("Reached the end of the program");

        

    }

 

}

 

 

    C:\temp> java Exception05

     

    An Exception occurred with following details :

       java.io.FileNotFoundException: input.txt (The system cannot find the file specified)

                                                   <-- System.out.println("   " + e);

       java.io.FileNotFoundException: input.txt (The system cannot find the file specified)

                                                   <-- System.out.println("   " + e.toString());

       java.io.FileNotFoundException: input.txt (The system cannot find the file specified)

                                                   <-- e.printStackTrace();

            at java.io.FileInputStream.open0(Native Method)

            at java.io.FileInputStream.open(Unknown Source)

            at java.io.FileInputStream.<init>(Unknown Source)

            at java.io.FileInputStream.<init>(Unknown Source)

            at Exception05.main(Exception05.java:11)

       java.io.FileNotFoundException <-- System.out.println("   " + e.getClass());

         input.txt (The system cannot find the file specified)

                                     <-- System.out.println("   " + e.getMessage());

    Reached the end of the program  <-- System.out.println("Reached the end of the program");

     

    NOTE : Exception occurred but program move forward