aspose file tools*
The moose likes Beginning Java and the fly likes Is toString() method of Exception class overriden ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Is toString() method of Exception class overriden ?" Watch "Is toString() method of Exception class overriden ?" New topic
Author

Is toString() method of Exception class overriden ?

Nikhil Reddy Lingala
Greenhorn

Joined: Dec 06, 2007
Posts: 15
Hello.,
I have a doubt in Exception handling.

I know Exception class is a sub class of Throwable class, where Throwable overrides the toString() method of Object class, and that toString() returns the Exception details in the form of a String.

Whats my doubt is..
does the ArithmeticException(or for that matter any class that inherits Exception) class override the toString() again?

I have a sample code here,


This gave me the output

MyException

That means while printing 'e' the Exception version of toString() method is called.. right?

But one of my instructors told me that there will be an overriden version of toString() in every class that inherits Exception.. like ArithmeticException, ArrayOutOfBoundsException.. etc.
I guess he is wrong..

Am I correct? or Am I wrong.. please correct me..
Thankyou
Phani Burra
Ranch Hand

Joined: Dec 13, 2007
Posts: 52
As far as I know each of the subclasses of Throwable will inherit the toString() method and it would return a concatenation of three strings:
name of the actual class of this object+": " (a colon and a space)+output of getMessage() method for this object.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Nikhil Reddy Lingala:
...does the ArithmeticException(or for that matter any class that inherits Exception) class override the toString() again? ...

No, it does not.

If you check the API for ArithmeticException, you will see that it inherits its implementation of toString from Throwable.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
 
 
subject: Is toString() method of Exception class overriden ?