This week's book giveaway is in the Functional programming forum.
We're giving away four copies of A Functional Approach to Java: Augmenting Object-Oriented Java Code with Functional Principles and have Ben Weidig on-line!
See this thread for details.
  • 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
  • Liutauras Vilda
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Peter Rooke
  • Himai Minh
Bartenders:
  • Piet Souris
  • Mikalai Zaikin

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