0

On OS X 10.11 users of our application experience a couple of NPEs without stacktraces (see this stackoverflow-question). I now want to create one myself to debug our error handling in this case.

How to prevent creating the stacktrace?

Community
  • 1
  • 1
Thomas S.
  • 5,804
  • 5
  • 37
  • 72

2 Answers2

1

Well, it actually was that simple:

final NullPointerException npe = new NullPointerException();
npe.setStackTrace(new StackTraceElement[0]);
throw npe;
Thomas S.
  • 5,804
  • 5
  • 37
  • 72
1

As mentioned in the linked post, the optimization happens when the same code causes the same exception frequently enough. So one way would be to cycle your exception-throwing and error handling code enough times to start seeing the optimization.

Or you can try a hack like this:

try {
    throw new NullPointerException("fake");
} catch (NullPointerException e) 
    e.setStackTrace(new StackTraceElement[0]);
    throw e;
}
Jiri Tousek
  • 12,211
  • 5
  • 29
  • 43