Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Throwable.getMessage()

 
Hemant Khurana
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 9258
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Hemant Khurana
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to add a message to the exception. Change your code to
and see what happens.
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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:
 
Hemant Khurana
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ya that worked!
Thanks every body for there inputs and help
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic