Java Programming - Exceptions - Discussion

Discussion :: Exceptions - Pointing out the correct statements (Q.No.5)

5. 

Which statement is true?

[A]. catch(X x) can catch subclasses of X where X is a subclass of Exception.
[B]. The Error class is a RuntimeException.
[C]. Any statement that can throw an Error must be enclosed in a try block.
[D]. Any statement that can throw an Exception must be enclosed in a try block.

Answer: Option A

Explanation:

Option A is correct. If the class specified in the catch clause does have subclasses, any exception object that subclasses the specified class will be caught as well.

Option B is wrong. The error class is a subclass of Throwable and not Runtime Exception.

Option C is wrong. You do not catch this class of error.

Option D is wrong. An exception can be thrown to the next method higher up the call stack.


Tam├íS Kifor said: (Oct 23, 2011)  
It is possible that X is not subclass of Exception but subclass of Throwable.

Priyanshu said: (Jun 4, 2016)  
Can anyone please help me for explaining the option D?

I would be very grateful for your help.

Divyang said: (Feb 6, 2017)  
For example, if the method is called in the Main(), no need for try catch inside the specified method.

That's the explanation for option D.

Sumalya said: (Aug 20, 2017)  
May anybody explain the option A? please.

Pratik Rajan Kale said: (Oct 16, 2019)  
Can anyone explain A option with good example?

Tamil said: (Aug 1, 2021)  
In a catch block;

For example catch (ArithmeticException e),
In the question catch(X x),
Match these two lines.
ArithmeticException=X.
e=x.

So, X(ArithmeticException) is the subclass of Exception.

Correct me, if I'm wrong.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.