This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes why extend Exception instead of Throwable ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "why extend Exception instead of Throwable ?" Watch "why extend Exception instead of Throwable ?" New topic
Author

why extend Exception instead of Throwable ?

Edward Chen
Ranch Hand

Joined: Dec 23, 2003
Posts: 798

We usually extend Exception instead of Throwable. Why? In what situations we extend Throwable.

Thanks.

nitin pokhriyal
Ranch Hand

Joined: May 19, 2005
Posts: 263
It is not necessary to extend from exception but a good programing practice. If you look at the Exception hierarchy it has a parallel to Error which is child of throwable.
More generally in application you create your own exception hierarchies.
John de Michele
Rancher

Joined: Mar 09, 2009
Posts: 600
Edward,

There's no real reason to extend Throwable, unless Sun decides to develop a third type of exceptional condition (I'm not sure what that would be, though).

John.
Muhammad Khojaye
Ranch Hand

Joined: Apr 12, 2009
Posts: 449

Edward Chen wrote:
We usually extend Exception instead of Throwable. Why? In what situations we extend Throwable.

Never extends throwable, extends Exception or an existing subclass of Exception as you want to add another kind of Exception not a kind of Throwable. You can extends RuntimeException if you need your exception to be unchecked.


http://muhammadkhojaye.blogspot.com/
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38519
    
  23
Because "Throwable" is too non-specific. Don't know whether the Java™ Tutorials section is any help.
Vikash Ananda
Ranch Hand

Joined: Oct 12, 2009
Posts: 32
Hi,

As you can see the specs throwable has 2 child hierarchies namely error and exception. Error are "serious" errors that a program generally shouldn't expect to catch and recover from while Exceptions are errors that a program can reasonably recover from.

Hence we should try to use Exceptions in all our applications.

Thanks,
Vikash Anand.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: why extend Exception instead of Throwable ?