File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

why extend Exception instead of Throwable ?

 
Edward Chen
Ranch Hand
Posts: 798
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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

Thanks.

 
nitin pokhriyal
Ranch Hand
Posts: 263
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 600
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 449
IntelliJ IDE Java Scala
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
Campbell Ritchie
Sheriff
Pie
Posts: 47288
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because "Throwable" is too non-specific. Don't know whether the Java™ Tutorials section is any help.
 
Vikash Ananda
Ranch Hand
Posts: 32
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic