This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
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.
Joined: Jan 11, 2007
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
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.