Exceptions: a stackless exception skips capturing a stack trace for speed

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.

Code
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);
}
Output
FastSignal: stackless
Stack trace length: 0
Advertisement
More in JAVA

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

© Java Coding Hub · About · Contact · Privacy · Terms