public class StackTrace extends Throwable
This class is not designed as an Error or an Exception and is not intended to be thrown or caught. StackTrace extends Throwable as a "blank slate" that still retains the stack trace machinery for monitoring and tracing purposes, but doesn't carry the semantic baggage of being an error state or a normal exception.
To log this StackTrace, treat it as a Throwable and call Throwable.printStackTrace() or
use a standard logger. E.g. when montioring a thread:
LOGGER.warn("Thread is stalled here", StackTrace.forThread(monitoredThread));
To highlight why a resource can't be used anymore, use the following pattern:
// in close()
closedHere = new StackTrace("Resource was closed here");
// in use()
if (closed)
throw new IllegalStateException("Resource is closed", closedHere));
To highlight where a resource was created, use the following pattern:
private final StackTrace createdHere = new StackTrace("Resource was created here");
// in warnIfNotClosed()
if (!closed)
LOGGER.warn("Resource was not closed", createdHere);
To combine with StackWalker, use the following pattern:
StackWalker walker = StackWalker.getInstance(StackWalker.Option.RETAIN_CLASS_REFERENCE);
// Skip the first element, which is the current method
StackTraceElement[] stackTrace = walker.walk(s -> s.skip(1).toArray(StackTraceElement[]::new));
StackTrace st = new StackTrace.Less("Resource was created here", stackTrace);
StackTrace can be used to diagnose resource leaks, single-threaded resource enforcement,
diagnosing when a resource is used after closing and monitoring long-running threads on demand.
Taking a StackTrace isn't free; however, if used judiciously, it can be utilized in production
to provide on-demand profiling.
For a deep dive into the StackTrace class see StackTrace User Guide.adoc
| Modifier and Type | Class and Description |
|---|---|
static class |
StackTrace.Less
A specialized
StackTrace that doesn't automatically fill in its own
call frames, so we can later call setStackTrace(...) with
another thread's frames. |
| Constructor and Description |
|---|
StackTrace()
Creates a stack trace for the current thread using the message "stack trace".
|
StackTrace(boolean addTimestamp)
Creates a stack trace for the current thread using the message "stack trace" and optionally adds a timestamp.
|
StackTrace(String message)
Creates a stack trace for the current thread with the specified message.
|
StackTrace(String message,
boolean addTimestamp)
Creates a stack trace for the current thread with the specified message and an optional timestamp.
|
StackTrace(String message,
Throwable cause)
Creates a stack trace with the specified message and cause.
|
StackTrace(String message,
Throwable cause,
boolean addTimestamp)
Creates a stack trace with the specified message and cause and can add a timestamp.
|
| Modifier and Type | Method and Description |
|---|---|
static @Nullable StackTrace |
forThread(Thread t)
Creates a
StackTrace object for the specified thread. |
addSuppressed, fillInStackTrace, getCause, getLocalizedMessage, getMessage, getStackTrace, getSuppressed, initCause, printStackTrace, printStackTrace, printStackTrace, setStackTrace, toStringpublic StackTrace()
public StackTrace(boolean addTimestamp)
addTimestamp - whether to add a timestamp to the stack trace messagepublic StackTrace(String message)
message - the detail message for this stack tracepublic StackTrace(String message, boolean addTimestamp)
message - the detail message for this stack traceaddTimestamp - whether to add a timestamp to the stack trace messagepublic StackTrace(String message, Throwable cause)
message - the detail message for this stack tracecause - the underlying cause, or null if unknownpublic StackTrace(String message, Throwable cause, boolean addTimestamp)
message - the detail message for this stack tracecause - the underlying cause, or null if unknownaddTimestamp - whether to add a timestamp to the stack trace message@Nullable public static @Nullable StackTrace forThread(Thread t)
StackTrace object for the specified thread.
If the thread is null, this method returns null.
If the thread is the current thread, a simple StackTrace with its name is returned.
Otherwise, a StackTrace.Less instance is returned with the thread's stack trace adjusted to
exclude native methods from the top of the stack.
t - the thread for which to obtain the stack trace, or null.StackTrace object representing the thread's stack trace, or null if the thread is null.Copyright © 2026 Chronicle Software Ltd. All rights reserved.