Java Programming - Exceptions - Discussion

Discussion Forum : Exceptions - Finding the output (Q.No. 2)
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");
finished
Exception
Compilation fails.
Arithmetic Exception
Answer: Option
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).

Discussion:
28 comments Page 2 of 3.

Sanjeev said:   1 decade ago
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:   1 decade ago
If we give finally statement and use Arithmetic ;what will be the output?

Tejas said:   1 decade ago
After running code:

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

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

Dheeraj said:   1 decade ago
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:   1 decade ago
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:   1 decade ago
When Exception class is caught, why "Exception" is not printed.

Pop said:   1 decade ago
Can we use more than one catch blocks with one try block ? if not than please tell me the reason.

Bhaskara said:   1 decade ago
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:   1 decade ago
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:   9 years ago
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.


Post your comments here:

Your comments will be displayed after verification.