• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Exceptions

 
p hasini
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When we create an user defined Exception class,

class MyException extends Exception
{
}

Do we have to override any of the methods in the Exception class hierarchy.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you think ?
 
p hasini
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do we override only the toString() method , to give a meaningful description of the Exception in the stack trace.

Should we override any other methods.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
toString() ? No. Instead you can use the Exception(String message) constructor.
 
Rob Spoor
Sheriff
Pie
Posts: 20514
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Most exception subclasses I create have no methods, only copies of the constructors of its parent class. All these constructors do is call super(arguments). There are usually two or four constructors to copy:
- no arguments for a no-message exception
- String for only a message
- Throwable to wrap a throwable (exception or error) (sometimes absent)
- String, Throwable to wrap a throwable but give a custom error message (sometimes absent)
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Whenever I create my own Exceptions I simply create four (overloaded) constructors and leave it at that.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I often create many constructors so I can include (and allow the retrieval of) application-specific properties. Often they're just used in the message, other times they're used by the handler.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic