Throwables

object Throwables

Static utility methods pertaining to instances of [[Throwable]].

Since

3.0

class Object
trait Matchable
class Any

Value members

Concrete methods

def propagate(throwable: Throwable): RuntimeException

Propagates throwable as-is if it is an instance of [[RuntimeException]] or [[Error]], or else as a last resort, wraps it in a RuntimeException then propagates.

Propagates throwable as-is if it is an instance of [[RuntimeException]] or [[Error]], or else as a last resort, wraps it in a RuntimeException then propagates.

This method always throws an exception. The RuntimeException return type is only for client code to make Java type system happy in case a return value is required by the enclosing method. Example usage:

T doSomething() {
 try {
   return someMethodThatCouldThrowAnything();
 } catch (IKnowWhatToDoWithThisException e) {
   return handle(e);
 } catch (Throwable t) {
   throw Throwables.propagate(t);
 }
}
Value Params
throwable

the Throwable to propagate

Returns

nothing will ever be returned; this return type is only for your convenience, as illustrated in the example above

def propagateIfInstanceOf[X <: Throwable](throwable: Throwable, declaredType: Class[X]): Unit

Propagates throwable exactly as-is, if and only if it is an instance of declaredType. Example usage:

Propagates throwable exactly as-is, if and only if it is an instance of declaredType. Example usage:

try {
 someMethodThatCouldThrowAnything();
} catch (IKnowWhatToDoWithThisException e) {
 handle(e);
} catch (Throwable t) {
 Throwables.propagateIfInstanceOf(t, IOException.class);
 Throwables.propagateIfInstanceOf(t, SQLException.class);
 throw Throwables.propagate(t);
}
def stackTrace(throwable: Throwable): String

Returns a string containing the result of [[Throwable#toString() toString()]], followed by the full, recursive stack trace of throwable. Note that you probably should not be parsing the resulting string; if you need programmatic access to the stack frames, you can call [[Throwable#getStackTrace()]].

Returns a string containing the result of [[Throwable#toString() toString()]], followed by the full, recursive stack trace of throwable. Note that you probably should not be parsing the resulting string; if you need programmatic access to the stack frames, you can call [[Throwable#getStackTrace()]].