Top Description Fields Constructors Methods
org.junit.platform.commons.util

public final Class ExceptionUtils

extends Object
Class Inheritance
Annotations
@API
status:INTERNAL
since:1.0
Static Imports
org.apiguardian.api.API.Status.INTERNAL

Collection of utilities for working with exceptions.

DISCLAIMER

These utilities are intended solely for usage within the JUnit framework itself. Any usage by external parties is not supported. Use at your own risk!

Since
1.0

Field Summary

Modifier and TypeField and Description
private static final String
private static final Predicate<String>

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public static List<Throwable>

Returns:

an immutable list of all throwables found, including the supplied one; never null
findNestedThrowables
(Throwable
the Throwable to explore; never null
rootThrowable
)

Find all causes and suppressed exceptions in the stack trace of the supplied Throwable.

public static void
pruneStackTrace(Throwable
the Throwable whose stack trace should be pruned; never null
throwable
,
List<String>
the class names that should stop the pruning if encountered; never null
classNames
)

Prune the stack trace of the supplied Throwable.

public static String
readStackTrace(Throwable throwable)

Read the stacktrace of the supplied Throwable into a String.

private static <T extends Throwable> T
public static RuntimeException

Returns:

this method always throws an exception and therefore never returns anything; the return type is merely present to allow this method to be supplied as the operand in a throw statement
throwAsUncheckedException
(Throwable
the Throwable to throw as an unchecked exception; never null
t
)

Throw the supplied Throwable, masked as an unchecked exception.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

JUNIT_PLATFORM_LAUNCHER_PACKAGE_PREFIXback to summary
private static final String JUNIT_PLATFORM_LAUNCHER_PACKAGE_PREFIX
STACK_TRACE_ELEMENT_FILTERback to summary
private static final Predicate<String> STACK_TRACE_ELEMENT_FILTER

Constructor Detail

ExceptionUtilsback to summary
private ExceptionUtils()

Method Detail

findNestedThrowablesback to summary
public static List<Throwable> findNestedThrowables(Throwable rootThrowable)

Find all causes and suppressed exceptions in the stack trace of the supplied Throwable.

Parameters
rootThrowable:Throwable

the Throwable to explore; never null

Returns:List<Throwable>

an immutable list of all throwables found, including the supplied one; never null

Annotations
@API
status:INTERNAL
since:1.10
Since
1.10
pruneStackTraceback to summary
public static void pruneStackTrace(Throwable throwable, List<String> classNames)

Prune the stack trace of the supplied Throwable.

Prune all stack trace elements up one of the supplied classNames are pruned. All subsequent elements in the stack trace will be retained.

If the classNames do not match any of the stacktrace elements then the org.junit, jdk.internal.reflect, and sun.reflect packages are pruned.

Additionally, all elements prior to and including the first JUnit Platform Launcher call will be removed.

Parameters
throwable:Throwable

the Throwable whose stack trace should be pruned; never null

classNames:List<String>

the class names that should stop the pruning if encountered; never null

Annotations
@API
status:INTERNAL
since:1.10
Since
1.10
readStackTraceback to summary
public static String readStackTrace(Throwable throwable)

Read the stacktrace of the supplied Throwable into a String.

throwAsback to summary
private static <T extends Throwable> T throwAs(Throwable t) throws Throwable-:T
Annotations
@Contract:_ -> fail
@SuppressWarnings:unchecked, TypeParameterUnusedInFormals
throwAsUncheckedExceptionback to summary
public static RuntimeException throwAsUncheckedException(Throwable t)

Throw the supplied Throwable, masked as an unchecked exception.

The supplied Throwable will not be wrapped. Rather, it will be thrown as is using an exploit of the Java language that relies on a combination of generics and type erasure to trick the Java compiler into believing that the thrown exception is an unchecked exception even if it is a checked exception.

Warning

This method should be used sparingly.

Parameters
t:Throwable

the Throwable to throw as an unchecked exception; never null

Returns:RuntimeException

this method always throws an exception and therefore never returns anything; the return type is merely present to allow this method to be supplied as the operand in a throw statement

Annotations
@Contract:_ -> fail