Win a copy of Rust Web Development this week in the Other Languages forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

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:
reply
    Bookmark Topic Watch Topic
  • New Topic