NullPointerException
GD.java
class GD
{
    public static void main(String args[])
    {
        String s=null;
        System.out.println("Length: "+s.length());
    }
}
Output
godarda@gd:~$ javac GD.java
godarda@gd:~$ java GD Exception in thread "main" java.lang.NullPointerException at GD.main(GD.java:6) godarda@gd:~$
Java program to demonstrate the use of try and catch blocks
GD.java
class GD
{
    public static void main(String args[])
    {
        String s=null;
        try
        {
            System.out.println("Length: "+s.length());
        }
        catch(Exception e)
        {
            System.out.println("String is null, hence, Unable to find the length.");
        }
    }
}
Output
godarda@gd:~$ javac GD.java
godarda@gd:~$ java GD String is null, hence, Unable to find the length. godarda@gd:~$
Java program to demonstrate the use of try with multiple catch blocks
GD.java
class GD
{
    public static void main(String args[])
    {
        String s=null;
        try
        {
            System.out.println("Length: "+s.length());
        }
        catch(ArithmeticException e)
        {
            System.out.println("Exception caught in the first catch block");
        }
        catch(NullPointerException e)
        {
            System.out.println("Exception caught in the second catch block");
        }
        catch(Exception e)
        {
            System.out.println("Exception caught in the third catch block");
        }
    }
}
Output
godarda@gd:~$ javac GD.java
godarda@gd:~$ java GD Exception caught in the second catch block godarda@gd:~$
Java program to handle the NullPointerException using Exception class
GD.java
class GD
{
    public static void main(String args[])
    {
        String s=null;
        try
        {
            System.out.println("Length: "+s.length());
        }
        catch(ArithmeticException e)
        {
            System.out.println("Exception caught in the first catch block");
        }
        catch(StringIndexOutOfBoundsException e)
        {
            System.out.println("Exception caught in the second catch block");
        }
        catch(Exception e)
        {
            System.out.println("Exception caught in the third catch block");
        }
    }
}
Output
godarda@gd:~$ javac GD.java
godarda@gd:~$ java GD Exception caught in the third catch block godarda@gd:~$
Java program to handle the NullPointerException using RuntimeException class
GD.java
class GD
{
    public static void main(String args[])
    {
        String s=null;
        try
        {
            System.out.println("Length: "+s.length());
        }
        catch(ArithmeticException e)
        {
            System.out.println("Exception caught in the first catch block");
        }
        catch(RuntimeException e)
        {
            System.out.println("Exception caught in the second catch block");
        }
        catch(Exception e)
        {
            System.out.println("Exception caught in the third catch block");
        }
    }
}
Output
godarda@gd:~$ javac GD.java
godarda@gd:~$ java GD Exception caught in the second catch block godarda@gd:~$
The following sequence of multiple catch blocks are not allowed
GD.java
class GD
{
    public static void main(String args[])
    {
        String s=null;
        try
        {
            System.out.println("Length: "+s.length());
        }
        catch(Exception e)
        {
            System.out.println("Exception caught in the third catch block");
        }
        catch(RuntimeException e)
        {
            System.out.println("Exception caught in the second catch block");
        }
        catch(ArithmeticException e)
        {
            System.out.println("Exception caught in the first catch block");
        }
    }
}
Output
godarda@gd:~$ javac GD.java
godarda@gd:~$ java GD GD.java:14: error: exception RuntimeException has already been caught catch(RuntimeException e) ^ GD.java:18: error: exception ArithmeticException has already been caught catch(ArithmeticException e) ^ 2 errors godarda@gd:~$
Comments and Reactions