The protected Throwable(String, Throwable, boolean, boolean) constructor can disable stack trace capture, which is much cheaper for exceptions thrown very frequently for control flow rather than for diagnosing bugs.
class FastSignal extends RuntimeException {
FastSignal(String message) {
super(message, null, false, false);
}
}
try {
throw new FastSignal("stackless");
} catch (FastSignal e) {
System.out.println(e.getClass().getSimpleName() + ": " + e.getMessage());
System.out.println("Stack trace length: " + e.getStackTrace().length);
}
FastSignal: stackless
Stack trace length: 0
Run this yourself in the Online Java Compiler, spin up a live REST API in the API Sandbox, or practise with Java interview questions.
Published 2026-09-27