• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Bear Bibeault
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh

How to return the detail message string of a custom exception

 
Greenhorn
Posts: 13
Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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:



 
author and iconoclast
Posts: 24203
44
Mac OS X Eclipse IDE Chrome
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You call "getMessage()" to retrieve the message, but you don't print it, so the method call has no visible effect. Try

 
Eduardo Hernandez
Greenhorn
Posts: 13
Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
wow.. I'm just here laughing of myself... how I didn't figure it out haha! Thanks!
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic