• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

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

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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]
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
dennis deems
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Ranch Hand
Posts: 47
Eclipse IDE Spring Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.


 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@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
 
Yes, my master! Here is the tiny ad you asked for:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic