aspose file tools*
The moose likes Java in General and the fly likes Exceptions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Exceptions" Watch "Exceptions" New topic
Author

Exceptions

p hasini
Ranch Hand

Joined: Oct 24, 2009
Posts: 92
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

Joined: Nov 24, 2005
Posts: 14687
    
  16

What do you think ?


[My Blog]
All roads lead to JavaRanch
p hasini
Ranch Hand

Joined: Oct 24, 2009
Posts: 92
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

Joined: Nov 24, 2005
Posts: 14687
    
  16

toString() ? No. Instead you can use the Exception(String message) constructor.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19696
    
  20

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)


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38865
    
  23
Whenever I create my own Exceptions I simply create four (overloaded) constructors and leave it at that.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Exceptions