GeekInterview.com
   Home |  Tech FAQ  |   Interview Questions |  Placement Papers |  Tech Articles |  Learn |  Freelance Projects |  Online Testing |  Geeks Talk |  Job Postings |  Knowledge Base | Site Search |  Add/Ask Question

GeekInterview.com  >  Interview Questions  >  J2EE  >  Core Java
Go To First  |  Previous Question  |  Next Question 
 Core Java  |  Question 474 of 492    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: 11 Last Update: July 16, 2008     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
  Sorting Options  
  Page 1 of 2   « First    1    2    >     Last »  
April 15, 2008 04:15:30   #1  
abuthahir.d Member Since: January 2008   Contribution: 10    

RE: Exception Handling
This will throw a compilation error .

I dont like to give the reason . check in any java books for exceptions. this is one of the basic rules for catching exceptions

 
Is this answer useful? Yes | NoAnswer is useful 0   Answer is not useful 3Overall Rating: -3    
April 15, 2008 06:37:32   #2  
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 | No
April 15, 2008 08:43:23   #3  
abuthahir.d Member Since: January 2008   Contribution: 10    

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   #4  
hemanth_1386 Member Since: April 2008   Contribution: 3    

RE: Exception Handling
yes ur correct...
 
Is this answer useful? Yes | No
April 15, 2008 15:38:03   #5  
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   #6  
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 1   Answer is not useful 0Overall Rating: +1    
June 08, 2008 07:02:04   #7  
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   #8  
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 | No
June 28, 2008 02:31:59   #9  
r.praveenkumar Member Since: October 2007   Contribution: 24    

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   #10  
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
  Page 1 of 2   « First    1    2    >     Last »  


 
Go To Top


 Sponsored Links

 




About Us  |   Privacy Policy  |   Terms and Conditions  |   Contact  |   Site Map  |   Add Question  |   Propose Category  |   RSS Feeds  |   Articles Sitemap  |   Site Updates  |   Add Resource

Copyright © 2005 - 2008 GeekInterview.com. All Rights Reserved
Page copy protected against web site content infringement by Copyscape