1

I am writing a Java program, and I have a line of code that requires a try...catch statement. The problem is that the line of code is always falling through to the catch part of the statement. I do not know why or what the problem is, so I would like to print the contents of the error to the screen so that I may be able to try tracking down the problem. How can I do this? Could I try System.out.println(e) where e is the variable of type Exception?

DaveTheMinion
  • 664
  • 3
  • 22
  • 45

2 Answers2

5

You can try:

try {
    . . .
} catch (Exception e) {
    e.printStackTrace();
}

EDIT (motivated by Sujan Sivagurunathan's answer: If the exception might be a secondary exception (one that was thrown as a consequence of another exception), you can do this more elaborate version:

try {
    . . .
} catch (Exception e) {
    e.printStackTrace();

    for (Throwable t = e.getCause(); t != null; t = t.getCause()) {
        System.err.println("Caused by:")
        t.printStackTrace();
    }
}
Community
  • 1
  • 1
Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
3

The getMessage method contains a message describing the exception :

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

Output could be : Divided by zero in the case of an ArithmeticException

You can also try :

try {
    // ...
} catch (Exception e) {
    e.printStackTrace();
}

Output could be :

java.lang.ArithmeticException: Divided by zero
at javaapplication27.Test1.test(Test1.java:27)
at javaapplication27.Test1.main(Test1.java:19)

e.getMessage() will be more suitable to show to an end user, while e.printStackTrace() will be more suitable for a developer, or to put in a log file.

slaadvak
  • 4,611
  • 1
  • 24
  • 34
  • What's the difference between this and `e.printStackTrace();`? – DaveTheMinion May 18 '14 at 01:38
  • `getMessage` is a method of Throwable class (super class of all exceptions of Java) inherited by every exception class. It will only print the message contained within the exception. For exemple, for an `ArithmeticException`, the message could be "Divided by zero" for a zero division exception. `printStartTrace` will display the full code stack pointing to this exception, which is something not useful, because maybe you want to show this message to the end user, which doesn't know what a stack trace is, but will understand the "Divided by zero" sentence. – slaadvak May 18 '14 at 01:42
  • Thanks. I am not looking for something meant to be user friendly, but programmer friendly for myself. With the shape the program is in now, it won't work unless I get this exception issue resolved. – DaveTheMinion May 18 '14 at 01:47
  • @slaadvak - The stack trace is "something not useful" -- really?! From OP's question: _"I would like to print the contents of the error to the screen **so that I may be able to try tracking down the problem**"_ – Ted Hopp May 18 '14 at 01:47
  • It can be not useful, or too verbose, in the case of showing this to an end user. But for debugging, its the way to go. – slaadvak May 18 '14 at 01:51