wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes How to return the detail message string of a custom exception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How to return the detail message string of a custom exception" Watch "How to return the detail message string of a custom exception" New topic
Author

How to return the detail message string of a custom exception

Eduardo Hernandez
Greenhorn

Joined: Jul 09, 2011
Posts: 13


I have written a custom exception and invoking the getMessage() method inherited from java.lang.Throwable doesn't return the detail message string.

The following is my custom exception code:



The MyException(String) constructor calls super(message) to construct a throwable with the specified detail message. If I do call printStackTrace() method on a MyException reference it does print the stacktrace along with the detail message:

MyException: My Message
at exTest.check(exTest.java:13)
at exTest.main(exTest.java:5)

The getMessage() method doesn't return the detail message. The following is the test class:



Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

You call "getMessage()" to retrieve the message, but you don't print it, so the method call has no visible effect. Try



[Jess in Action][AskingGoodQuestions]
Eduardo Hernandez
Greenhorn

Joined: Jul 09, 2011
Posts: 13

wow.. I'm just here laughing of myself... how I didn't figure it out haha! Thanks!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: How to return the detail message string of a custom exception