Java Programming - Exceptions - Discussion


What will be the output of the program?

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

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

Answer: Option C


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: (Sep 9, 2011)  
Since all kinds of exceptions are caught by Exception class the output should be "exception" isnt it? Why would compilation fail?

Mahesh Reddy said: (Oct 10, 2011)  
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: (Oct 14, 2011)  
Since the catch expresion contains


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

Nikhil said: (May 31, 2012)  
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: (Sep 23, 2012)  
Exception class is super class so it never reach the next exception.

R.Vinod Kumar said: (Oct 17, 2012)  
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: (Oct 17, 2012)  
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: (Oct 17, 2012)  
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: (Oct 17, 2012)  
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: (Nov 7, 2012)  
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: (Nov 27, 2012)  
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: (Jan 7, 2013)  
If we give finally statement and use Arithmetic ;what will be the output?

Tejas said: (Sep 13, 2013)  
After running code:

Exception in thread "main" java.lang.RuntimeException:

Uncompilable source code - exception java.lang.ArithmeticException has already been caught
at Test.main(
Java Result: 1

Dheeraj said: (Oct 3, 2013)  
Some of you said that Exception is the super top class but it is wrong, all the Exceptions has only one base class i.e. Throwable class.

Akhilesh said: (Oct 14, 2013)  
Answer is C.

Because here Exception class is used before ArithmeticException class. Exception class is parent class of all the other exception classes. We caught the exception using parent class hence child class will show error. We should always use catch block with Exception at last.

Tony said: (Jan 3, 2014)  
When Exception class is caught, why "Exception" is not printed.

Pop said: (Aug 23, 2014)  
Can we use more than one catch blocks with one try block ? if not than please tell me the reason.

Bhaskara said: (Apr 27, 2015)  
When ever we have try with multiple catch blocks, the order of the catch block is very important in this case, and it should be from child to parent, otherwise then we will get compilation fails saying "the exception is already been caught".

Hritika said: (Sep 28, 2015)  
What will the output if Arithmetic Exception class is handled first & if Exception class is found later, then also compiler has to read same thing again then why not compilation error is printed?

Akash said: (May 14, 2016)  
Once it find an error which is caught by 1st catch block then automatically it will display compilation error because once compilation error occurred then it quit by displaying error msg so the 2nd block will not be executed.

Anup Kumar said: (Jul 1, 2016)  
Here, the try block is of arithmetic exception type. So, a catch block arithmetic exception should come before the catch block exception ae.

That's why compilation fails.

Shruti said: (Aug 5, 2016)  
After ending of try block braces is not there so compilation failed.

Srinivas said: (Aug 26, 2016)  
It will check whether the most specific one is next or not and has nothing to do with compilation.

N.Saikrishna said: (Sep 18, 2016)  
The order is important.

First, we have to write child class and next parent class other wise there will be compile time error.

Coders_Sutra said: (Jun 16, 2017)  
Yes, right @Vinod Kumar.

As the 5/0 was already lead to an exception just the compiler now is finding the Arithmetic exception declaration in the catch block or the finally block in the program after try block therefore if you write the super class ""Exception e"" before the subclass this is not the concept.

Choti said: (May 22, 2019)  
If we change the order catch block means if we write 2nd catch block at a 1st place then the o/p:

Arithmetic Exception.

Omkar said: (Jul 16, 2019)  
Hi, why we do use the Exception e in the catch block? what is the exact meaning of try and catch block & when it needs in a program?

Veera said: (Dec 18, 2021)  
If we get any type of exception, Exception block will catch that, then why we need to take different catch blocks like ArithmeticException, ArrayIndexOutOfBoundsException etc.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.