//*****************************************************************************
// Catching more specific Exceptions 
// Must be done prior to catching more generic exceptions 
// Of course, you do not need to catch every single specific exception                        
//*****************************************************************************
public class ExceptSpecific
{
    public static void main(String[ ] args)
    {
        try {
            double x = 123 / 0;                         //triggers ArithmeticException

            String s = null;
            char   c = s.charAt(1);                     //triggers NullPointerException

            String str1 = "hello world";
            String str2 = str1.substring(10,20);        //triggers StringIndexOutOfBoundsException

            String[ ] array1 = {"one","two","three"};
            System.out.print(array1[5]);                //triggers ArrayIndexOutOfBoundsException

            String num1 = "123 ";
            int    num2 = Integer.parseInt(num1);       //triggers NumberFormatException
        }
        catch(ArithmeticException e)
        {   
            System.out.println("Invalid mathematical operation - " + e); 
        }
        catch(NullPointerException e)
        {   
            System.out.println("The source reference is null - " + e); 
        }
        catch(StringIndexOutOfBoundsException e)
        {   
            System.out.println("String subset is invalid - " + e); 
        }
        catch(ArrayIndexOutOfBoundsException e)
        {   
            System.out.println("Accessing element beyond end of array - " + e); 
        }
        catch(NumberFormatException e)
        {   
            System.out.println("String contains non-numeric characters - " + e); 
        }
        catch(Exception e)
        {   
            System.out.println("Other/unknown exception - " + e); 
            e.printStackTrace( );               //print stack trace (where the error occurred
        }
        System.out.print("Done");
    }
}