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");
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 1 of 3.
R.VINOD KUMAR said:
1 decade ago
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.
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:
1 decade ago
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.
Coders_Sutra said:
8 years ago
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.
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.
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.
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.
R.VINOD KUMAR said:
1 decade ago
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.
So Compiler will not read 2nd time. So leads to Compile Error.
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".
Mahesh Reddy said:
1 decade ago
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.
catch (ArithmeticException ae)
{
System.out.println(" Arithmetic Exception");
}
that is why compilation fails.
R.VINOD KUMAR said:
1 decade ago
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.
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".
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".
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:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers