Win a copy of Soft Skills: The software developer's life manual this week in the Jobs Discussion forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Is toString() method of Exception class overriden ?

 
Nikhil Reddy Lingala
Greenhorn
Posts: 15
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11343
Java Mac Safari
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic