Java program to demonstrate the use of nested try and catch blocks
KW.java
class KW
{
    public static void main(String args[])
    {
        String s=null;
        int n=515, d=0, result=0;
        try
        {
            try
            {
                result=n/d;
            }
            catch(ArithmeticException e)
            {
                System.out.println("Exception caught in the first nested catch block");
            }
            System.out.println("Length: "+s.length());
        }
        catch(ArithmeticException e)
        {
            System.out.println("Exception caught in the first catch block");
        }
        catch(Exception e)
        {
            System.out.println("Exception caught in the third catch block");
        }
    }
}
Output
kodingwindow@kw:~$ javac KW.java
kodingwindow@kw:~$ java KW Exception caught in the first nested catch block Exception caught in the third catch block kodingwindow@kw:~$
Java program to demonstrate the use of nested try and catch blocks
KW.java
class KW
{
    public static void main(String args[])
    {
        String s=null;
        int n=515, d=0, result=0;
        try
        {
            System.out.println("Length: "+s.length());
        }
        catch(ArithmeticException e)
        {
            System.out.println("Exception caught in the first catch block");
        }
        catch(Exception e1)
        {
            try
            {
                result=n/d;
            }
            catch(ArithmeticException e2)
            {
                System.out.println("Exception caught in the first nested catch block");
            }
            System.out.println("Exception caught in the third catch block");
        }
    }
}
Output
kodingwindow@kw:~$ javac KW.java
kodingwindow@kw:~$ java KW Exception caught in the first nested catch block Exception caught in the third catch block kodingwindow@kw:~$
Java program to demonstrate the use of nested try, catch, and finally blocks
KW.java
class KW
{
    public static void main(String args[])
    {
        String s=null;
        int n=515, d=0, result=0;
        try
        {
            System.out.println("Length: "+s.length());
        }
        catch(ArithmeticException e)
        {
            System.out.println("Exception caught in the first catch block");
        }
        catch(Exception e1)
        {
            try
            {
                result=n/d;
            }
            catch(ArithmeticException e2)
            {
                System.out.println("Exception caught in the first nested catch block");
            }
            finally
            {
                System.out.println("Finally block will always execute");
            }
            System.out.println("Exception caught in the third catch block");
        }
        finally
        {
            System.out.println("Finally block will always execute");
        }
    }
}
Output
kodingwindow@kw:~$ javac KW.java
kodingwindow@kw:~$ java KW Exception caught in the first nested catch block Finally block will always execute Exception caught in the third catch block Finally block will always execute kodingwindow@kw:~$
Advertisement