10

I have the following code:

import org.apache.commons.lang.exception.ExceptionUtils;
public void myMethod() {
    try {
        // do something
    } catch (Exception e) {
        System.out.println(ExceptionUtils.getStackTrace(e)); // prints "java.lang.NullPointerException"
        System.out.println(ExceptionUtils.getFullStackTrace(e)); // prints "java.lang.NullPointerException"
        e.printStackTrace(); // prints "java.lang.NullPointerException"
    }
}

The output that I would like to see is a full stacktrace with line numbers and the hierarchy of classes that failed. For example,

Exception in thread "main" java.lang.NullPointerException
        at org.Test.myMethod(Test.java:674)
        at org.TestRunner.anotherMethod(TestRunner.java:505)
        at java.util.ArrayList(ArrayList.java:405)

This code is being run inside a larger app which also has log4j, but I'm hoping to be able to get the exception into a string so I can send it as an email to the java developers.

Does anyone have any ideas about how I can capture the full stack trace to a string? I can't use Thread.currentThread().getStackTrace() since this app runs on Java 4. What might be blocking the above code from printing the full stacktrace?

4
  • Looks like the StackTraceElement array in NPE is being reset to empty. Is anything calling setStackTrace on the NPE? Commented Sep 11, 2012 at 14:41
  • @DanGravell - No, nothing should be calling setStackTrace. Commented Sep 11, 2012 at 14:45
  • Are you able to debug this in an IDE? Might be worth setting a breakpoint in the catch block to see if the Exception is 'perfectly formed'. Commented Sep 11, 2012 at 14:49
  • add the logged output of your myMethod() or your actual method. e.printStackTrace(); normally should print the fill trace. Commented Sep 11, 2012 at 14:53

8 Answers 8

18

this is beacause java does some code optimiziation when run in server mode (java -server) to skip this. use -XX:-OmitStackTraceInFastThrow in java args see link below for details:

http://jawspeak.com/2010/05/26/hotspot-caused-exceptions-to-lose-their-stack-traces-in-production-and-the-fix/

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

4 Comments

Instead of turning off this optimization on a production server I would just follow Peter's advice and look at an earlier error in the log.
I agree that can be done when you own the code, if you are dependenty on someone else, till they fix, this is the best you could do.
This is definitely what caused the similar problem for me on Prod. Thank you for the answer!
This exists on OpenJDK7 but not on OpenJDK8.
14

If you repeatedly throw an exception, the JVM stop filling in the stack trace. I am not sure why but it may be to reduce load on the JVM. You need to be looking at an earlier stack trace to see the details.

for (int n = 0; ; n++) {
    try {
        Integer i = null;
        i.hashCode();
    } catch (Exception e) {
        if (e.getStackTrace().length == 0) {
            System.out.println("No more stack trace after " + n + " thrown.");
            break;
        }
    }

prints

No more stack trace after 20707 thrown.

Comments

4

Does anyone have any ideas about how I can capture the full stack trace to a string?

You can use the below way(StringWriter.toString()) to transfer the stack trace into a String.

 StringWriter writer = new StringWriter();
 e.printStackTrace( new PrintWriter(writer,true ));
 System. out.println("exeption stack is :\n"+writer.toString());

Comments

2

The exception might be caught and thrown somewhere before your catch block. Maybe in another class which your are calling to do the logic.

An Exception created like
new Exception(new Throwable("java.lang.NullPointerException"));
will print something like what you see.

2 Comments

It did turn out that inside the try block, that it made a call to another method which had a try block with a catch statement that explicitly reduced the exception (similar to your example except that they didn't hard-code "NullPointerException", but rather just truncated the cause of the stacktrace).
@David - I would be seriously considering switching to a different product. Deliberately suppressing exception information like that is going against the best interests of their customers / users. Find an alternative supplier that doesn't do this kind of crappy thing.
1

May be you don't have an appender for console output. You consider adding one. If not, log it as LOGGER.error(ex); with log4j or SLF4J

1 Comment

For a while, I thought it might be due to console logging. So I tried storing the exception in a string and emailing it to myself. The email only contained "java.lang.NullPointerException"
1

I can only think of 2 reasons why e.printStackTrace() might output just the string "java.lang.NullPointerException".

  • Something might have called setStackTrace(new StackTraceElement[0]) on the exception.
  • The exception object might be an instance of a tricky class that has overridden the printStackTrace() or some other method to return misleading information.

Comments

1

What might be blocking the above code from printing the full stacktrace?

In my case, a superclass of the Exception I was catching had this code:

/* avoid the expensive and useless stack trace for API exceptions */
@Override
public Throwable fillInStackTrace() {
    return this;
}

This meant that the stack trace array was never filled in so all I got was:

org.apache.kafka.AuthException: Auth failed: Invalid username or password

with no stack information so no idea where it happens. Lovely.

I was able to override this behavior by grabbing the source for the Exception, copying it into my project, and removing the fillInStackTrace() method. It's a hack but I needed information from that Exception.

Comments

-1

Make sure you used e.printStackTrace(), instead of e.getStackTrace();

Comments

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.