2

I am inheriting Exception class for creating my own exception. Something like the code below

public class ApplicationException extends Exception {
  ApplicationException(String errorMessage) {
    super(errorMessage); 
  }
}

The problem is that the stacktrace is always empty. The below code will write nothing to the console.

ApplicationException(String errorMessage) {
  super(errorMessage); 
  System.out.println(this.getStackTrace());
}

I don't understand why it is empty because the Throwable(String) is calling fillInStackTrace method. Is there a way to fill in Stack or is it something else that I should do?

Duncan Jones
  • 67,400
  • 29
  • 193
  • 254
Govan
  • 2,079
  • 4
  • 31
  • 53
  • how are you calling the code that throws the exception? – enrique7mc May 23 '14 at 15:44
  • why don't you use printStackTrace() instead of the SOP call? – Stultuske May 23 '14 at 15:46
  • 1
    When I new up your exception in main, `this.getStackTrace()` gives me an object, and `this.printStackTrace()` prints the stack just fine. How are you using your exception in code? – azurefrog May 23 '14 at 15:47
  • I use the below code to report my exception t to eclipse errorlog. But it shows that I don't have stacktrace. final Status status = new Status(severity, Activator.PLUGIN_ID, msg + className, t); logger.log(status); – Govan May 23 '14 at 15:53
  • 1
    @Govan that sounds like a problem with your logger. – Bombe May 23 '14 at 18:11
  • thank you. @Bombe. It was some problem in the code. fixed it. – Govan May 24 '14 at 19:37

3 Answers3

6

The stack trace is not empty. It's just that it's an array, and the default toString() of an array doesn't show its elements, only its class name and hashCode (e.g., [Ljava.lang.StackTraceElement;@b0cf5f). Call:

this.printStackTrace();

or loop through the array and display its contents manually, and you will see the stack trace.

Boann
  • 48,794
  • 16
  • 117
  • 146
  • yes you are write. But then it is strange. I am using eclipse and I am reporting my errors to Eclipse ErrorLog. It showing that my exceptions don't have stack trace. – Govan May 23 '14 at 15:50
  • 1
    @Govan Well... they do. – Boann May 23 '14 at 15:52
  • This is only partly correct. The exception won't have a stacktrace until it has been thrown. Up until that time the printStackTrace will report the current stacktrace. – mikea May 23 '14 at 15:58
  • 1
    @mikea That's not true. Display the stack trace of an exception that was created and returned from (but not thrown by) a method call, and you will see the stack trace includes that method, showing that the stack trace is filled in at exception creation time, whether it has been thrown or not. Also see the [documentation of Throwable](http://docs.oracle.com/javase/8/docs/api/java/lang/Throwable.html), which says: "A throwable contains a snapshot of the execution stack of its thread at the time it was created." – Boann May 23 '14 at 16:02
  • Also, a `Throwable` is free return an empty array of stack traces from the `getStackTrace()` method. – Bombe May 23 '14 at 18:12
  • @Boann is right. There is no need to threw the exceptions to get the stacktrace. I am never threwing my exceptions. Just logging them in the place. It was a bug in code (sending null instead of the throwable object to logger). After fixing it I am getting stack trace without prolem and without throwing them. – Govan May 24 '14 at 19:40
4

this.getStackTrace() returns a StackTraceElement[]. Arrays never override toString, so printing it just yields something like:

[Ljava.lang.StackTraceElement;@1a23b006

You may want to invoke printStackTrace() instead. If you don't care about its causes (in your case, it won't have any), you can just iterate over the StackTraceElement objects in the array and print each one out.

In fact, if all you want is the stack trace, you can do without the exception altogether: Thread.currentThread().getStackTrace() gets you the same StackTraceElement[].

Community
  • 1
  • 1
yshavit
  • 42,327
  • 7
  • 87
  • 124
0

At the point that you create a new exception you haven't thrown it so it won't have a stack trace.

EDIT:

Expanding on this. The fillInStackTrace in throwable sets the stacktrace of the expection to an empty array of stack trace elements. getStackTrace returns this stacktrace. It printStackTrace finds the stacktrace set to the default empty array it prints the current stack trace where it was called from, otherwise it prints the stacktrace contained in the exception.

mikea
  • 6,537
  • 19
  • 36
  • Except that I do see a stack trace if I do `new Exception().printStackTrace()`. – yshavit May 23 '14 at 15:44
  • Not true. If you just **new** one up and put a `this.printStackTrace()` in the constructor, it prints the line that it was instantiated on. – azurefrog May 23 '14 at 15:45
  • i don't want to throw my exeptions. is it a way to fill in stacktrace? – Govan May 23 '14 at 15:47
  • So, you just want to print the stack trace in a point in your code? – mikea May 23 '14 at 15:49
  • "The fillInStackTrace in throwable sets the stacktrace of the exception to an empty array of stack trace elements." No it doesn't...? – Boann May 23 '14 at 15:56