A simple stack trace
Before coming to complex stack traces, we first investigate a stack trace originating from a very simple Java application.
package com.example.someapplication;
public class StackTraceDemo {
public static void main(String[] args) {
someMethod();
}
private static void someMethod() {
throw new IllegalStateException("This is for demonstration.");
}
}
Running this class results in the following output.
Exception in thread "main" java.lang.IllegalStateException: This is for demonstration.
at com.example.someapplication.StackTraceDemo.someMethod(StackTraceDemo.java:9)
at com.example.someapplication.StackTraceDemo.main(StackTraceDemo.java:5)
First of all, Java tells us that we got an exception in a thread called main. As we haven't used anything related to starting other threads, this is the only application thread in this example. In applications involving multiple threads, the thread name can be a useful piece of diagnostic information. This part may be omitted or look different depending on what printed the stack trace.
After the thread name, the full class name of the exception is shown. As we have thrown an IllegalStateException in our example, this is java.lang.IllegalStateException. If a message is associated with the exception, that message is included in the stack trace as well. In our case, we passed the message This is for demonstration. to the IllegalStateException constructor resulting in that text being part of the stack trace.