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 3 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 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:
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.
GATTI said:
1 decade ago
Exception class is super class so it never reach the next exception.
Nikhil said:
1 decade ago
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.
Inderpreet said:
1 decade ago
Since the catch expresion contains
System.out.println("Exception");
Isn't it that the output should be 'exception' ?
System.out.println("Exception");
Isn't it that the output should be 'exception' ?
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.
Hema said:
1 decade ago
Since all kinds of exceptions are caught by Exception class the output should be "exception" isnt it? Why would compilation fail?
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers