The easiest way to suppress the stacktrace on any Exception is
throwable.setStackTrace(new StackTraceElement[0]);
If the exception has a cause, you may need to do the same recursively.
This also reduces the costly creation of the stack trace, as much as possible
The stacktrace for a throwable is initialized in
Throwable#fillInStackTrace()
, which is called by any constructor and thus cannot be avoided.
When the stacktrace actually is used, an StackTraceElement[] is lazily constructed in
Throwable#getOurStackTrace()
which only happens, if the field Throwable.stackTrace was not already set.
Setting the stacktrace to whatever non null value, avoids the construction of the StackTraceElement[] in Throwable#getOurStackTrace() and reduces the performance penalty as much as possible.