IndiaBIX.com
Arithmetic Aptitude Data Interpretation
Logical Reasoning Verbal Reasoning Non Verbal Reasoning
General Knowledge
Sudoku Number puzzles Missing letters puzzles Logical puzzles Playing cards puzzles Clock puzzles
C Programming C++ Programming C# Programming Java Programming
Microbiology Biochemistry Biotechnology Biochemical Engineering
Civil Engineering Mechanical Engineering Chemical Engineering Networking Database Questions Computer Science Basic Electronics Digital Electronics Electronic Devices Circuit Simulation Electrical Enigneering Engineering Mechanics Technical Drawing
Placement Papers Group Disucssion HR Interview Technical Interview Body Language
Aptitude Test Verbal Ability Test Verbal Reasoning Test Logical Reasoning Test C Programming Test Java Programming Test Data Interpretation Test General Knowledge Test
Data Structures Operating Systems Networking DATABASE Database Basics SQL Server Basics SQL Server Advanced SQL Server 2008 JAVA Core Java Java Basics Advanced Java UNIX Unix File Management Unix Memory Management Unix Process Managemnt C Interview Questions The C Language Basics .NET Interview Questions .NET Framework ADO.NET ASP.NET Software Testing

Java Programming - Exceptions - Discussion

@ : Home > Java Programming > Exceptions > Finding the output - Discussion

Read more:

"You cannot shake hands with a clenched fist."
- Indira Gandhi
2. 

What will be the output of the program?

try 
{ 
    int x = 0; 
    int y = 5 / x; 
} 
catch (Exception e) 
{
    System.out.println("Exception"); 
} 
catch (ArithmeticException ae) 
{
    System.out.println(" Arithmetic Exception"); 
} 
System.out.println("finished");

[A]. finished[B]. Exception
[C]. Compilation fails.[D]. Arithmetic Exception

Answer: Option C

Explanation:

Compilation fails because ArithmeticException has already been caught. ArithmeticException is a subclass of java.lang.Exception, by time the ArithmeticException has been specified it has already been caught by the Exception class.

If ArithmeticException appears before Exception, then the file will compile. When catching exceptions the more specific exceptions must be listed before the more general (the subclasses must be caught before the superclasses).


Hema said: (Fri, Sep 9, 2011 10:28:52 AM)    
 
Since all kinds of exceptions are caught by Exception class the output should be "exception" isnt it? Why would compilation fail?

Mahesh Reddy said: (Mon, Oct 10, 2011 02:25:08 PM)    
 
Since Exception class is super most class for all exception classes. so the below code is never reachable

catch (ArithmeticException ae)
{
System.out.println(" Arithmetic Exception");
}


that is why compilation fails.

Inderpreet said: (Fri, Oct 14, 2011 09:26:44 PM)    
 
Since the catch expresion contains

System.out.println("Exception");

Isn't it that the output should be 'exception' ?

Nikhil said: (Thu, May 31, 2012 03:14:44 PM)    
 
I think Inderpreet is correct. If it can't catch exception then what is use of try catch block. It is supposed to catch exception.

Gatti said: (Sun, Sep 23, 2012 12:11:28 PM)    
 
Exception class is super class so it never reach the next exception.

R.Vinod Kumar said: (Wed, Oct 17, 2012 10:36:46 AM)    
 
To Hema: While Compilation time, Control is already came through Arithmetic Exception residing in Exception class in 1st catch block. Again the same Arithmetic Exception came to it in 2nd catch block. So, it feels useless(Why to execute again unnecessarily which had already executed), So gives us Compilation Error.

R.Vinod Kumar said: (Wed, Oct 17, 2012 10:43:59 AM)    
 
To Inderpreet: It is compilation error(means if had been blocked in 1st phase only, So why to think about execution phase). if it is would not have compilation error(means if you would not have write 2nd catch block), then you are right.

R.Vinod Kumar said: (Wed, Oct 17, 2012 10:59:37 AM)    
 
To Nikhil: Error means which occured at compilation time.
Exception means which occurs at runtime error.
Here problem is with compiler, not with JVM/try,catch block.
Means try,catch blocks are well and dynamic but compiler telling that already i saw Arithmetic Exception in Exception class, again why you want me to see the same Arithmetic Exception which is useless. So it is asking us to avoid it by displaying Compilation Error.

But if you write these 2 catch blocks in reversed order, it will work, because 1st Arithmetic Exception will be seen by the Compiler and in next catch block remaining Exceptions will be seen.

R.Vinod Kumar said: (Wed, Oct 17, 2012 11:06:00 AM)    
 
Compiler will always reads only once. Exception is the Super class which had been read by Compiler. Next Arithmetic Exception is the subclass of that Exception class which had already read by Compiler.
So Compiler will not read 2nd time. So leads to Compile Error.

Asad said: (Wed, Nov 7, 2012 08:32:02 PM)    
 
Unreachable catch block for ArithmeticException. It is already handled by the catch block for Exception.

So remove catch and replace throws. Compilation error is correct option

Sanjeev said: (Tue, Nov 27, 2012 06:18:47 AM)    
 
So simply speaking,

Whenever try with multiple catch blocks, the order of catch blocks is very important and it should be from child to parent, otherwise we will get an compilation error saying "Exception already been caught".

Vijeesh V R said: (Mon, Jan 7, 2013 01:38:23 PM)    
 
If we give finally statement and use Arithmetic ;what will be the output?

Write your comments here:
Name *:     Email:


© 2008-2013 by IndiaBIX™ Technologies. All Rights Reserved | Copyright | Terms of Use & Privacy Policy

Contact us: info@indiabix.com     Follow us on twitter!