Meaningless Drivel is fun!*
The moose likes Beginning Java and the fly likes Throwable.getMessage() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Throwable.getMessage()" Watch "Throwable.getMessage()" New topic
Author

Throwable.getMessage()

Hemant Khurana
Greenhorn

Joined: Jan 11, 2007
Posts: 27
Hi All

In my catch block

try{
......
......
}
catch(Exception e){
System.out.println(e.getMessage());
}

I am using e.getMessage to print my error message but it is always returning null

Any body please help how to use the getMessage() method properly

Regards
Hemant
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8836
    
    7

If one has a null pointer exception, the message of that exception will be null. You will find it helpful to use printStackTrace() to indicate the source lines and stack where an exception occurred.


"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
printStackTrace() is a good idea.

Where you are printing out an exception for debugging, rather than user interface, purposes, it is better to do toString(), rather than getMessage(). Exceptions vary in the informativeness of their message (it can even be null as you found). All Exceptions will toString() to something readable.


Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
Hemant Khurana
Greenhorn

Joined: Jan 11, 2007
Posts: 27
I have been advised not to use printStackTrace() method due to coding standards and no matter what kind of exception i throw it always returns null

I even tried a small example like:

public class Test_Exception
{
public static void main(String[] args)
{
try{
ArrayIndexOutOfBoundsException et=new ArrayIndexOutOfBoundsException();
throw et;
}
catch(Exception e){
System.out.println(e.getMessage());
}
}
}

but it prints null only
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3419
    
  12
You need to add a message to the exception. Change your code to
and see what happens.


Joanne
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336


I have been advised not to use printStackTrace() method due to coding standards and no matter what kind of exception i throw it always returns null

Odd and unhelpful coding standards!

In the above example the reason you get null is because you don't give your exception any message. This will work:


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Hemant Khurana
Greenhorn

Joined: Jan 11, 2007
Posts: 27
ya that worked!
Thanks every body for there inputs and help
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Unfortunately there will be other exceptions thrown by the JVM that may not have any message. A common example is NullPointerException. This almost never has a message - the important info is (a) the name of the exception, and (b) the stack trace. An easy way to get the name of the exception and the message is to use toString() rather than getMessage(). This is almost always preferable to using getMessage() by itself.

Additionally, losing the stack trace info is a very very bad idea. I think you need to find out more about the coding standard against using printStackTrace(). Perhaps what they mean is, use a Logger instead. (Such as from log4j or java.util.logging.) This is a more powerful and flexible mechanism than using printStackTrace(), as it makes it easy for you to control the format and location of the logging using a configuration file. E.g. maybe they want the logging to go to a certain file rather than appearing on the console. You can still log exception information using a logger. In log4j it will typically look something like

This way the logger will not lose your stack trace, which is often the most important info that the exception contains. You don't necessarily have to print the stack trace to the screen, but you really do need to have this info somewhere. Talk more to the coding standard people to find out more about what they really mean.


"I'm not back." - Bill Harding, Twister
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Throwable.getMessage()
 
Similar Threads
Applet can write to, but not read from local file.
NPE for sql Connection
I am getting StreamCorruptedException
e.getMessage() not get printed on JSP using ActionError
Connction faile please help me