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

why extend Exception instead of Throwable ?

 
Ranch Hand
Posts: 798
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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

Thanks.

 
Ranch Hand
Posts: 263
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Rancher
Posts: 600
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 449
Scala IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because "Throwable" is too non-specific. Don't know whether the Java™ Tutorials section is any help.
 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can extend from Throwable and it would work the same because Exception doesn't have any new methods, but if you're working for a company and you write a package of classes and there are Exceptions that those classes will throw, let's say your classes will throw an Exception called CodeRanchException. If it only extends Throwable and the user tries to catch it with Exception, they'll get kicked out of the program. The user can read the stack trace then catch a CodeRanchException, but then they'll have other Exceptions not getting caught. They would have to refer to the package website and would find out it's a direct subclass of Throwable. They can declare a Throwable and catch them all. As a convenience to users, it's so much easier to extend Exception so your hierarchy makes sense and avoid crashes.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic