Java Programming

Topic No. 8 : About Exception Handling

About Excpetion Handling

The Exception Handling in Java is one of the powerful mechanism to handle the runtime errors so that normal flow of the application can be maintained.

Using try and catch block
Source Code of main class
package javaapplication1;

public class JavaApplication1 {

    public static void main(String[] args) {
   
        int a[];
        a = new int[10];
        try
        {
            System.out.print(10/0);
            a[11] = 0;
        }
        catch(Exception e)
        {
            System.out.print(e.toMessage());
        }
    }
}           
                          
Using try,catch and finally
Source Code of main class
package javaapplication1;

public class JavaApplication1 {

    public static void main(String[] args) {
   
        public static void main(String[] args) {
                // TODO code application logic here
                int a[];
                a = new int[10];

                try
                {
                    a[9] = 0;
                    try
                    {
                        int i,j;
                        i=10;
                        j=2;
                        int k = i/j;
                    }
                    catch(Exception e1)
                    {
                        System.out.print("\nDivision By 0 is not possible");
                    }
                }
                catch(Exception e2)
                {
                    System.out.print("\nArray index is out of range");
                }
                finally
                {
                    System.out.print("\nOk Then Bye");
                }
            }
    }
}