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?

6
  • how are you calling the code that throws the exception? Commented May 23, 2014 at 15:44
  • why don't you use printStackTrace() instead of the SOP call? Commented May 23, 2014 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? Commented May 23, 2014 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); Commented May 23, 2014 at 15:53
  • 1
    @Govan that sounds like a problem with your logger. Commented May 23, 2014 at 18:11

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

6 Comments

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 Well... they do.
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 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, which says: "A throwable contains a snapshot of the execution stack of its thread at the time it was created."
Also, a Throwable is free return an empty array of stack traces from the getStackTrace() method.
|
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[].

Comments

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.

5 Comments

Except that I do see a stack trace if I do new Exception().printStackTrace().
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.
i don't want to throw my exeptions. is it a way to fill in stacktrace?
So, you just want to print the stack trace in a point in your code?
"The fillInStackTrace in throwable sets the stacktrace of the exception to an empty array of stack trace elements." No it doesn't...?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.