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).
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?