net.sf.staccatocommons.lang
Class SoftException

java.lang.Object
  extended by java.lang.Throwable
      extended by java.lang.Exception
          extended by java.lang.RuntimeException
              extended by org.apache.commons.lang.exception.NestableRuntimeException
                  extended by org.apache.commons.lang.UnhandledException
                      extended by net.sf.staccatocommons.lang.SoftException
All Implemented Interfaces:
Serializable, org.apache.commons.lang.exception.Nestable

public class SoftException
extends org.apache.commons.lang.UnhandledException

A SoftException is a RuntimeException that wraps another with only solely purpose of making it unchecked.

Author:
flbulgarelli
See Also:
Serialized Form

Field Summary
 
Fields inherited from class org.apache.commons.lang.exception.NestableRuntimeException
delegate
 
Method Summary
static
<T> T
callOrSoften(Callable<T> callable)
          Tries return callable.call().
static Exception harden(RuntimeException runtimeException)
          Converts a given RuntimeException into a checked Exception, if possible.
static Exception harden(SoftException e)
          Converts the given SoftException into a checked Exception, by returning its cause
static RuntimeException soften(Throwable exception)
          Converts an exception into a RuntimeException, either by casting it, if possible, or wrapping it into a SoftException
static
<T> T
valueOrHarden(net.sf.staccatocommons.defs.Thunk<T> thunk)
          Tries to return thunk.value().
 
Methods inherited from class org.apache.commons.lang.exception.NestableRuntimeException
getCause, getMessage, getMessage, getMessages, getThrowable, getThrowableCount, getThrowables, indexOfThrowable, indexOfThrowable, printPartialStackTrace, printStackTrace, printStackTrace, printStackTrace
 
Methods inherited from class java.lang.Throwable
fillInStackTrace, getLocalizedMessage, getStackTrace, initCause, setStackTrace, toString
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Method Detail

soften

@NonNull
public static RuntimeException soften(@NonNull
                                              Throwable exception)
Converts an exception into a RuntimeException, either by casting it, if possible, or wrapping it into a SoftException

Parameters:
exception - the target exception to soften.
Returns:
The given exception, casted to RuntimeException, if possible, or a new SoftException that wrapps it, otherwise.

harden

@NonNull
public static Exception harden(@NonNull
                                       SoftException e)
Converts the given SoftException into a checked Exception, by returning its cause

Parameters:
e - the soft exception to harden
Returns:
the cause of the given soft exception

harden

@NonNull
public static Exception harden(@NonNull
                                       RuntimeException runtimeException)
Converts a given RuntimeException into a checked Exception, if possible. This method traverses the throwable causes chain, until a non RuntimeException is found. If such throwable exists and is an Exception, it returns it. Otherwise, returns the original runtimeException

Parameters:
runtimeException -
Returns:
The first non RuntimeException in the causes chain, if exists and is an Exception. The given RuntimeException otherwise.

callOrSoften

public static <T> T callOrSoften(@NonNull
                                 Callable<T> callable)
Tries return callable.call(). If an exception is thrown, it is softened and rethrown.

Type Parameters:
T - the callable return type
Parameters:
callable - the callable to execute. Non null.
Returns:
the result of the callable, if Callable.call() succeeds
Throws:
RuntimeException - if the call failed
See Also:
#soften(Exception)

valueOrHarden

public static <T> T valueOrHarden(@NonNull
                                  net.sf.staccatocommons.defs.Thunk<T> thunk)
                       throws Exception
Tries to return thunk.value(). If an exception is thrown, it is hardened and rethrown

Type Parameters:
T - the thunk return type
Parameters:
thunk -
Returns:
the value of the Thunk, if it succeeds
Throws:
Exception - if Thunk.value() failed
See Also:
Thunk, harden(RuntimeException)


Copyright © 2010-2012 Staccatocommons. All Rights Reserved.