Exception Handling

What is the output of the following program, When tested under JDK 5.0

class ExceptionDemo
{
public static void main(String[] args)
{
int a[] = new int[] {1,2,3,4,5};
try{
System.out.prinltln(a[6]);
} catch(Exception e) {
System.out.println("Catching Exception ...");
} catch(ArrayIndexOutofBoundsException ae) {
System.out.println("Catching ArrayIndexOutOfBounds Exception ...");
}
}
}



Questions by khadarzone   answers by khadarzone

Showing Answers 1 - 71 of 71 Answers

It doesn't give any compilation error.. it compiles successfully..
It throws ArrayIndexOutOfBoundException and it is caught by catch (Exception e) as it matches all the exceptions.

  Was this answer useful?  Yes

abuthahir.d

  • Apr 15th, 2008
 

Exception in thread "main" java.lang.Error: Unresolved compilation problem:

Unreachable catch block for ArrayIndexOutOfBoundsException. It is already handled by the catch block for Exception


this the error i got

  Was this answer useful?  Yes

ramesh124

  • Jun 8th, 2008
 

It will not compile
The ERror because of
the catching exceptions should start from child Class of Exceptions and from  there it can go to parent class.
If the catch blocks hierarchy is changed, it will compile successfully.

This will throw a compiler error while catching exceptions we should first catch child class exceptions and then only base class. The reason is if we follow this we will get the more specific reason for the failure ie what caused the exception to rise.

rinumca

  • Jun 29th, 2008
 

Here the child exception is caught before parent. In your program there are two compilation errors they are println is typed wrong and ArrayIndexOutOfBoundsException is also typed worng which result in compilation errors

  Was this answer useful?  Yes

vanigeetha

  • Jul 16th, 2008
 

I got this program in compilation error.

ArrayIndexOutOfBoundException is UnCheckedException, but i got error in compile time. Compile time exceptions are Checked Exception. I need Correct ans. plz send my mail id

  Was this answer useful?  Yes

The above example will throw compile time error. This is because, while catching checked/un-checked exceptions, exception hierarchy is to be followed. In this case, Exception is the superclass of all the exceptions(checked/un-checked). But, in this program, the catch block contains Exception and then ArrayIndexOutofBoundsException . As a result, compile time error is evident.

  Was this answer useful?  Yes

jhumriait

  • Jul 2nd, 2009
 

The program will compile successfully in below 2 ways.

class ExceptionDemo
{
     public
static void main(String[] args)

     {
          int a[] = new int[] {1,2,3,4,5};
          try
{

               System.out.println(a[6]);}

               catch
(ArrayIndexOutOfBoundsException ae) {

               System.out.println("Catching ArrayIndexOutOfBounds Exception ...");
          }


          catch(Exception e) {System.out.println("Catching Exception ...");

          }
     }
}

In the above it will be caught in the ArrayIndexOutOfBoundsException . Or


class ExceptionDemo
{
     public
static void main(String[] args)

     {
          int a[] = new int[] {1,2,3,4,5};
          try
{

               System.out.println(a[6]);
          }


     catch(Exception e) {System.out.println("Catching Exception ...");

          }
     }
}

In the above it will be caught in the general Exception block.

  Was this answer useful?  Yes

Compiler Error Will Result because Exception Subclasses are to be catched first in the Java program. But in this program The Exception will be always  Caught By Superclass Exception() and control will never reaches to ArrayIndexOutOfBoundsException().   In Java Unreachable Code is Error. Hence Compiler Will report an Error.

  Was this answer useful?  Yes

Sarje

  • Aug 18th, 2009
 

When you provide multiple catches subclass Exceptions must come before super class Exception otherwise it won't compile and will say that ArrayIndexOutOfBoundsException is already caught because Exception is the super class of ArrayIndexOutOfBoutsException.

This error is not ArrayIndexOutOfBoundsException but it is violation of the provided mechanism to give multiple catch so it will be caught at compile time.

  Was this answer useful?  Yes

mahesh19nov

  • Jan 19th, 2011
 

You can write like this -
<code>
     int a[] = new int[] {1,2,3,4,5};
        try{
           System.out.println(a[6]);
        } catch(ArrayIndexOutOfBoundsException e) {
           System.out.println("Exception Caught: "+e);
        }catch(Exception ex){
            System.out.println("Exception Caught ex "+ex);
        }
</code>

  Was this answer useful?  Yes

KhyatiK

  • Apr 8th, 2011
 

It will give you compilation error since exception is  broader than array out of bound exception it should not be placed before array out of bound exception

  Was this answer useful?  Yes

It will give compile time error (exception has already been caught) because you have caught the exceptions two times.Give any one exception then your program will run.

  Was this answer useful?  Yes

sudhir

  • Aug 1st, 2011
 

It will become error...order of placing the exceptions is important....first place sub class exception then after super class exception...

  Was this answer useful?  Yes

alin20_84

  • Feb 14th, 2012
 

It doesnt compile because ArrayIndexOutOfBoundsException catch is put after Exception catch and this is not ok because Exception is super class of ArrayIndexOutOfBoundsException (unchecked exception)

  Was this answer useful?  Yes

kuchipudi Prasad

  • May 30th, 2012
 

Output is "catching exception"

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions