GeekInterview.com
  I am new, Sign me up!
 
GeekInterview.com  >  Interview Questions  >  J2EE  >  Core Java
Go To First  |  Previous Question  |  Next Question 
 Core Java  |  Question 474 of 502    Print  
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 ...");
}
}
}






  
Total Answers and Comments: 16 Last Update: September 30, 2009     Asked by: khadarzone 
  
 Sponsored Links

 
 Best Rated Answer
Submitted by: jrcheenu
 
while writing catch the inheritance hierarchy should be followed. The child class of exception should be caought before parent.

Above answer was rated as good by the following members:
harrychahal, bino75, sandeep549
  Sorting Options  
  Page 1 of 2   « First    1    2    >     Last »  
April 15, 2008 06:37:32   #1  
hemanth_1386 Member Since: April 2008   Contribution: 3    

RE: Exception Handling
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.

 
Is this answer useful? Yes | NoAnswer is useful 0   Answer is not useful 2Overall Rating: -2    
April 15, 2008 08:43:23   #2  
abuthahir.d Member Since: January 2008   Contribution: 9    

RE: Exception Handling

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


 
Is this answer useful? Yes | No
April 15, 2008 13:23:05   #3  
hemanth_1386 Member Since: April 2008   Contribution: 3    

RE: Exception Handling
yes ur correct...
 
Is this answer useful? Yes | NoAnswer is useful 0   Answer is not useful 1Overall Rating: -1    
April 15, 2008 15:38:03   #4  
charkat Member Since: April 2008   Contribution: 1    

RE: Exception Handling
it wont compile...in catch statement..we need to define catch for specific exception and then and then we need to give catch for general statement

 
Is this answer useful? Yes | No
April 23, 2008 18:19:45   #5  
jrcheenu Member Since: April 2008   Contribution: 5    

RE: Exception Handling
while writing catch the inheritance hierarchy should be followed. The child class of exception should be caought before parent.
 
Is this answer useful? Yes | NoAnswer is useful 3   Answer is not useful 0Overall Rating: +3    
June 08, 2008 07:02:04   #6  
ramesh124 Member Since: June 2008   Contribution: 2    

RE: Exception Handling
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.

 
Is this answer useful? Yes | No
June 19, 2008 23:40:54   #7  
manu.raj2007 Member Since: June 2008   Contribution: 1    

RE: Exception Handling
This code results in a compilation error because ArrayIndexOutOfBoundsException has already been caught by its base class Exception.
 
Is this answer useful? Yes | NoAnswer is useful 1   Answer is not useful 0Overall Rating: +1    
June 28, 2008 02:31:59   #8  
r.praveenkumar Member Since: October 2007   Contribution: 32    

RE: Exception Handling
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.
 
Is this answer useful? Yes | No
June 29, 2008 12:33:49   #9  
rinumca Member Since: June 2008   Contribution: 7    

RE: Exception Handling
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
 
Is this answer useful? Yes | No
July 16, 2008 02:18:12   #10  
vanigeetha Member Since: January 2008   Contribution: 4    

RE: Exception Handling
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

 
Is this answer useful? Yes | No
  Page 1 of 2   « First    1    2    >     Last »  


 
Go To Top


 Sponsored Links

 
About Us -  Privacy Policy -  Terms and Conditions -  Contact -  Ask Question -  Propose Category -  Site Updates 

Copyright © 2005 - 2009 GeekInterview.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape