Java

TOPIC i04 – ERROR HANDLING 1 - BASICS

 

LESSON WORK

 

 

QUESTION #1


For each of the following, state what will be outputted to screen.  The focus is on whether you fully understand the try/catch and ‘throws Exception’ flow path.

//A)

   public static void main(String[] args)

   {

        int[] a = {4,5,6};

       

        try

        {

             System.out.println("A");

             a[3]=4;   //creates exception?

             System.out.println("B");

        }

        catch(IndexOutOfBoundsException e)

        {

             System.out.println("C");

        }

        finally

        {

             System.out.println("D");

        }

        System.out.println("E");

   }

//B)

   public static void main(String[] args)

   {

        int[] a = {44,62,51,16};

       

        try

        {

             System.out.println("A");

             a[3]=4;   //creates exception?

             System.out.println("B");

        }

        catch(IndexOutOfBoundsException e)

        {

             System.out.println("C");

        }

        finally

        {

             System.out.println("D");

        }

        System.out.println("E");

   }

//C)

public class SmallHeadache

{

   public static void main(String[] args)

   {

        System.out.println("i");

        yokes();

        System.out.println("ii");

   }

    

   public static void yokes()

   {

        System.out.println("1");

        yikes();

        System.out.println("2");

   }

   public static void yikes()

   {

        int[] a = {4,5,6};

        try

        {

             System.out.println("A");

             a[-1]=4;        //creates exception

             System.out.println("B");

        }

        catch(IndexOutOfBoundsException e)

        {

             System.out.println("C");

        }

        finally

        {

             System.out.println("D");

        }

        System.out.println("E");

   }

}

//D

public class SmallHeadache2

{

   public static void main(String[] args)

   {

       System.out.println("i");

       try

       {

          System.out.println("ii");

           yokes();

           System.out.println("iii");

       }

       catch (IndexOutOfBoundsException e)

       {

          System.out.println("iv");

       }

       System.out.println("v");

   }

    

   public static void yokes()

   {

       System.out.println("1");

       yikes();

       System.out.println("2");

   }

 

   public static void yikes() throws IndexOutOfBoundsException

   {

       int[] a = {4,5,6};

       System.out.println("A");

       a[-1]=4;        //creates exception

       System.out.println("B");

   }

}

//E

public class SmallHeadache

{

   public static void main(String[] args)

   {

      yikes();

   }

    

   public static void yikes()

   {

        try

        {

           System.out.println("A");

           int[] a = {4,5,6,7,8};

           a[-1]=4;        //creates exception

           System.out.println("B");

           return;

        }

        catch (Exception e)

        {

             System.out.println("C");

             return;

        }

        finally

        {

             System.out.println("D");

        }

   }

}