- java.lang.Object
-
- org.junit.platform.commons.util.ExceptionUtils
-
@API(status=INTERNAL, since="1.0") public final class ExceptionUtils extends java.lang.Object
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
Fields Modifier and Type Field Description private static java.lang.String
JUNIT_PLATFORM_LAUNCHER_PACKAGE_PREFIX
private static java.util.function.Predicate<java.lang.String>
STACK_TRACE_ELEMENT_FILTER
-
Constructor Summary
Constructors Modifier Constructor Description private
ExceptionUtils()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static java.util.List<java.lang.Throwable>
findNestedThrowables(java.lang.Throwable rootThrowable)
Find all causes and suppressed exceptions in the stack trace of the suppliedThrowable
.static void
pruneStackTrace(java.lang.Throwable throwable, java.util.List<java.lang.String> classNames)
Prune the stack trace of the suppliedThrowable
by removing stack trace elements from theorg.junit
,jdk.internal.reflect
, andsun.reflect
packages.static java.lang.String
readStackTrace(java.lang.Throwable throwable)
Read the stacktrace of the suppliedThrowable
into a String.private static <T extends java.lang.Throwable>
voidthrowAs(java.lang.Throwable t)
static java.lang.RuntimeException
throwAsUncheckedException(java.lang.Throwable t)
Throw the suppliedThrowable
, masked as an unchecked exception.
-
-
-
Field Detail
-
JUNIT_PLATFORM_LAUNCHER_PACKAGE_PREFIX
private static final java.lang.String JUNIT_PLATFORM_LAUNCHER_PACKAGE_PREFIX
- See Also:
- Constant Field Values
-
STACK_TRACE_ELEMENT_FILTER
private static final java.util.function.Predicate<java.lang.String> STACK_TRACE_ELEMENT_FILTER
-
-
Method Detail
-
throwAsUncheckedException
public static java.lang.RuntimeException throwAsUncheckedException(java.lang.Throwable t)
Throw the suppliedThrowable
, 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
- theThrowable
to throw as an unchecked exception; nevernull
- 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
-
throwAs
private static <T extends java.lang.Throwable> void throwAs(java.lang.Throwable t) throws T extends java.lang.Throwable
- Throws:
T extends java.lang.Throwable
-
readStackTrace
public static java.lang.String readStackTrace(java.lang.Throwable throwable)
Read the stacktrace of the suppliedThrowable
into a String.
-
pruneStackTrace
@API(status=INTERNAL, since="1.10") public static void pruneStackTrace(java.lang.Throwable throwable, java.util.List<java.lang.String> classNames)
Prune the stack trace of the suppliedThrowable
by removing stack trace elements from theorg.junit
,jdk.internal.reflect
, andsun.reflect
packages. If aStackTraceElement
matching one of the suppliedclassNames
is encountered, all subsequent elements in the stack trace will be retained.Additionally, all elements prior to and including the first JUnit Platform Launcher call will be removed.
- Parameters:
throwable
- theThrowable
whose stack trace should be pruned; nevernull
classNames
- the class names that should stop the pruning if encountered; nevernull
- Since:
- 1.10
-
findNestedThrowables
@API(status=INTERNAL, since="1.10") public static java.util.List<java.lang.Throwable> findNestedThrowables(java.lang.Throwable rootThrowable)
Find all causes and suppressed exceptions in the stack trace of the suppliedThrowable
.- Parameters:
rootThrowable
- theThrowable
to explore; nevernull
- Returns:
- an immutable list of all throwables found, including the supplied
one; never
null
- Since:
- 1.10
-
-