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?
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?
Well, it actually was that simple:
final NullPointerException npe = new NullPointerException();
npe.setStackTrace(new StackTraceElement[0]);
throw npe;
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;
}