Class CatchThrowable

java.lang.Object
com.googlecode.catchexception.throwable.CatchThrowable

public final class CatchThrowable extends Object
The Class CatchThrowable.
  • Method Details

    • caughtThrowable

      public static <T extends Throwable> T caughtThrowable()
      Returns the throwable caught during the last call on the proxied object in the current thread.
      Type Parameters:
      T - throwable caught during the last call on the proxied object
      Returns:
      Returns the throwable caught during the last call on the proxied object in the current thread - if the call was made through a proxy that has been created via verifyThrowable(ThrowingCallable, Class) verifyThrowable()} or catchThrowable(ThrowingCallable). Returns null the proxy has not caught an throwable. Returns null if the caught throwable belongs to a class that is no longer loaded.
    • caughtThrowable

      @Deprecated(since="2.3.0", forRemoval=true) public static <T extends Throwable> T caughtThrowable(Class<T> caughtThrowableType)
      Deprecated, for removal: This API element is subject to removal in a future version.
      Use caghtThrowable() instead as the passed argument does nothing
      Caught throwable.
      Type Parameters:
      T - the generic type
      Parameters:
      caughtThrowableType - the caught throwable type
      Returns:
      the t
    • verifyThrowable

      public static void verifyThrowable(ThrowingCallable actor)
      Use it to verify that an throwable is thrown and to get access to the thrown throwable (for further verifications). The following example verifies that obj.doX() throws a Throwable: verifyThrowable(obj).doX(); // catch and verify assert "foobar".equals(caughtThrowable().getMessage()); // further analysis If doX() does not throw a Throwable, then a ThrowableNotThrownAssertionError is thrown. Otherwise the thrown throwable can be retrieved via caughtThrowable().
      Parameters:
      actor - The instance that shall be proxied. Must not be null.
    • verifyThrowable

      public static void verifyThrowable(ThrowingCallable actor, Class<? extends Throwable> clazz)
      Use it to verify that an throwable of specific type is thrown and to get access to the thrown throwable (for further verifications). The following example verifies that obj.doX() throws a MyThrowable: verifyThrowable(obj, MyThrowable.class).doX(); // catch and verify assert "foobar".equals(caughtThrowable().getMessage()); // further analysis If doX() does not throw a MyThrowable, then a ThrowableNotThrownAssertionError is thrown. Otherwise the thrown throwable can be retrieved via caughtThrowable().
      Parameters:
      actor - The instance that shall be proxied. Must not be null.
      clazz - The type of the throwable that shall be thrown by the underlying object. Must not be null
    • catchThrowable

      public static void catchThrowable(ThrowingCallable actor)
      Use it to catch an throwable and to get access to the thrown throwable (for further verifications). In the following example you catch throwables that are thrown by obj.doX(): catchThrowable(obj).doX(); // catch if (caughtThrowable() != null) { assert "foobar".equals(caughtThrowable().getMessage()); // further analysis } If doX() throws a throwable, then caughtThrowable() will return the caught throwable. If doX() does not throw a throwable, then caughtThrowable() will return null.
      Parameters:
      actor - The instance that shall be proxied. Must not be null.
    • catchThrowable

      public static void catchThrowable(ThrowingCallable actor, Class<? extends Throwable> clazz)
      Use it to catch an throwable of a specific type and to get access to the thrown throwable (for further verifications). In the following example you catch throwables of type MyThrowable that are thrown by obj.doX(): catchThrowable(obj, MyThrowable.class).doX(); // catch if (caughtThrowable() != null) { assert "foobar".equals(caughtThrowable().getMessage()); // further analysis } If doX() throws a MyThrowable, then caughtThrowable() will return the caught throwable. If doX() does not throw a MyThrowable, then caughtThrowable() will return null. If doX() throws an throwable of another type, i.e. not a subclass but another class, then this throwable is not thrown and caughtThrowable() will return null.
      Parameters:
      actor - The instance that shall be proxied. Must not be null.
      clazz - The type of the throwable that shall be caught. Must not be null.
    • resetCaughtThrowable

      public static void resetCaughtThrowable()
      Sets the caught throwable to null. This does not affect throwables saved at threads other than the current one. Actually you probably never need to call this method because each method call on a proxied object in the current thread resets the caught throwable. But if you want to improve test isolation or if you want to 'clean up' after testing (to avoid memory leaks), call the method before or after testing.