wood burning stoves 2.0*
The moose likes Java in General and the fly likes why I am allowed to write Throwable or Error in catch block ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "why I am allowed to write Throwable or Error in catch block ?" Watch "why I am allowed to write Throwable or Error in catch block ?" New topic
Author

why I am allowed to write Throwable or Error in catch block ?

Vijay Kumar Raman
Greenhorn

Joined: Oct 19, 2010
Posts: 12
I tried writing a program to throw a Error object explicitly and written try catch block to handle the Error. It is working !!!
Even when I try throwing a Throwable object also it works just fine.
Can anyone tell me why we are allowed to write Throwable or Error in catch block while writing Exception alone would be more relevant :-)



Console output:
java.lang.Error
at ErrorCheckTry.testingthrowError(ErrorCheckTry.java:3)
at ErrorCheckTry.main(ErrorCheckTry.java:8)
Error caught in catch block

Note: I tried adding a recursive call of the testingthrowError() method above. Program terminated throwing StackOverflowError. The catch block does't work here !!!

[Added code tags - see UseCodeTags]
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4342
    
    7

Why should it stop you? You can catch Throwable or any subclass. Usually you shouldn't catch errors, but there are certain circumstances where you should.
dennis deems
Ranch Hand

Joined: Mar 12, 2011
Posts: 808
Vijay Kumar Raman wrote:Note: I tried adding a recursive call of the testingthrowError() method above. Program terminated throwing StackOverflowError. The catch block does't work here !!!

Show us that version of the code. When I tried it, the error was caught.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7503
    
  18

Vijay Kumar Raman wrote:Can anyone tell me why we are allowed to write Throwable or Error in catch block while writing Exception alone would be more relevant :-)

Says who? The syntax of the catch statement is catch(Throwable); and since both Error and Exception are subclasses of Throwable, there's absolutely no ambiguity.

Other than that: "Why does Java do {whatever}..." questions are best left to e-mails to one (or all) of the designers. Any answer you get here will be our interpretation of what they were thinking, so you need to take it with whatever size pinch of salt you deem fit.

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
dennis deems
Ranch Hand

Joined: Mar 12, 2011
Posts: 808
Winston Gutkowski wrote:
Vijay Kumar Raman wrote:Can anyone tell me why we are allowed to write Throwable or Error in catch block while writing Exception alone would be more relevant :-)

"Why does Java do {whatever}..." questions are best left to e-mails to one (or all) of the designers.


I generally interpret "why are we allowed to" questions as "under what circumstances are we advised to do so". There was a big discussion not too long ago about the advisability of catching Throwable; personally I remained unconvinced. As Matthew says above, you should almost never catch an Error. When might you? Maybe if you're designing a framework, I suppose. In general, catch Exceptions, and catch them as specifically as possible.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7503
    
  18

Dennis Deems wrote:I generally interpret "why are we allowed to" questions as "under what circumstances are we advised to do so".

Me too, but I don't think either the subject or the line I quoted fall into that category. The simple answer to the first, pretty much any way you look at it, is: because Error is a subclass of Throwable.

There was a big discussion not too long ago about the advisability of catching Throwable; personally I remained unconvinced. As Matthew says above, you should almost never catch an Error. When might you? Maybe if you're designing a framework, I suppose. In general, catch Exceptions, and catch them as specifically as possible.

Me too. However, if catch didn't catch a Throwable, you couldn't catch an Error; so again, to me it boils down to a 'why did they do it?' question.

Winston
Guillaume Jourdan
Ranch Hand

Joined: Jul 24, 2012
Posts: 47

From the Java API Error class specification :
An Error is a subclass of Throwable that indicates serious problems that a reasonable application should not try to catch. Most such errors are abnormal conditions. [...]

A method is not required to declare in its throws clause any subclasses of Error that might be thrown during the execution of the method but not caught, since these errors are abnormal conditions that should never occur.

Prasad prap
Greenhorn

Joined: Sep 26, 2008
Posts: 28
@Vijay

Check out this link Throwable

Throwable is the parent class for Errors and Exception. So catch block basically catches Throwable class but we usually write catch(Exception e) often because errors are thrown on much serious accounts. Check out this Stackoverflow
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: why I am allowed to write Throwable or Error in catch block ?
 
Similar Threads
Finally abrupt return
Exception
ExamLab Exception
Exception doubt.
wizlab mock question