Class ExceptionUtils


@API(status=INTERNAL, since="1.0") public final class ExceptionUtils extends Object
Collection of utilities for working with exceptions.


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!

  • Method Details

    • throwAsUncheckedException

      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.


      This method should be used sparingly.

      t - the Throwable to throw as an unchecked exception; never null
      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
    • readStackTrace

      public static String readStackTrace(Throwable throwable)
      Read the stacktrace of the supplied Throwable into a String.
    • pruneStackTrace

      @API(status=INTERNAL, since="1.10") public static void pruneStackTrace(Throwable throwable, List<String> classNames)
      Prune the stack trace of the supplied Throwable by removing stack trace elements from the org.junit, jdk.internal.reflect, and sun.reflect packages. If a StackTraceElement matching one of the supplied classNames 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.

      throwable - the Throwable whose stack trace should be pruned; never null
      classNames - the class names that should stop the pruning if encountered; never null
    • findNestedThrowables

      @API(status=INTERNAL, since="1.10") public static List<Throwable> findNestedThrowables(Throwable rootThrowable)
      Find all causes and suppressed exceptions in the stack trace of the supplied Throwable.
      rootThrowable - the Throwable to explore; never null
      an immutable list of all throwables found, including the supplied one; never null