• 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
  • Ron McLeod
  • Liutauras Vilda
  • Paul Clapham
  • paul wheaton
Sheriffs:
  • Tim Cooke
  • Devaka Cooray
  • Rob Spoor
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:

Throwable and Error Classes

 
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class Box
{
public void doIt() { throw new Throwable(); }
}

The above code does not compile. However if Throwable is replaced by Error it compiles fine.

I find this strange because Error descends from Throwable. So I would have expected the code not to compile if Error replaced Throwable in the above code.

Does anybody have any explanation of this anomaly ?
 
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well the Java API states that

An Error is a subclass of Throwable that indicates serious problems that a reasonable application should not try to catch

.So it is not necessary to catch a error whereas you need to catch or declare thrown a Throwable (or Exception).
 
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
We cannot throw Thowable . we can throw Error , Exception which are dereived from Throwable. For error , it is not neccessary that we have to catch that exception(catch block) (or throws ) . But for Exception (for Checked exceptions only) , you have to catch by means of catch block or throws clause.

Please correct me it i am wrong.
 
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try to compile this:
 
All that thinking. Doesn't it hurt? What do you think about this tiny ad?
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic