ch.javasoft.util
Class ExceptionUtil

java.lang.Object
  extended by ch.javasoft.util.ExceptionUtil

public class ExceptionUtil
extends Object

ExceptionUtil is a utility class with static helper methods for exception handling, for instance to convert exceptions to some desired exception type.


Method Summary
static IOException toIOException(String msg, Throwable cause)
          Jdk 1.6++ contains constructors with causes for IOException, but older jdk versions do not.
static IOException toIOException(Throwable cause)
          Jdk 1.6++ contains constructors with causes for IOException, but older jdk versions do not.
static RuntimeException toRuntimeException(Throwable throwable)
          Converts the given throwable into a RuntimeException, if necessary, and returns it.
static
<T extends Throwable>
T
toRuntimeExceptionOr(Class<T> exClass, Throwable throwable)
          Returns the given throwable if it is of the expected type and returns it, or converts it into a RuntimeException, if necessary, and throws it.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

toRuntimeException

public static RuntimeException toRuntimeException(Throwable throwable)
                                           throws Error
Converts the given throwable into a RuntimeException, if necessary, and returns it.

If throwable already was a runtime exception, it is returned as is. If throwable was an Error, it is rethrown. In every other case, a new runtime exception is created, nesting the original throwable (see RuntimeException.RuntimeException(Throwable)).

The intended use of this method could somehow look like this:

 void myMethod(...) throws RuntimeException {
   try {
      ... //any kinds of exceptions might occur 
   }
   catch(Throwable th) {
     throw ExceptionUtil.toRuntimeException(th);
     //you never get here (and the compiler knows this)
   }
 }
 

Parameters:
throwable - the given exception to convert, if necessary
Returns:
the converted exception, nesting the original exception if this was not a RuntimeException nor an Error
Throws:
Error - if throwable was an instance of Error

toRuntimeExceptionOr

public static <T extends Throwable> T toRuntimeExceptionOr(Class<T> exClass,
                                                           Throwable throwable)
                                                throws Error,
                                                       RuntimeException
Returns the given throwable if it is of the expected type and returns it, or converts it into a RuntimeException, if necessary, and throws it. If throwable is of the desired exception class, it is returned. If not, an Error is thrown if throwable was such. Otherwise, a RuntimeException is thrown, either the throwable itself or a newly created RuntimeException nesting throwable (see RuntimeException.RuntimeException(Throwable)).

The intended use of this method could somehow look like this:

 void myMethod(...) throws MyException {
   try {
      ... //any kinds of exceptions might occur 
   }
   catch(Throwable th) {
     throw ExceptionUtil.toRuntimeExceptionOr(MyException.class, th);
     //you never get here (and the compiler knows this)
   }
 }
 

Parameters:
throwable - the given exception to validate
Returns:
the validated exception of type exClass
Throws:
Error - if throwable was such an instance and exClass was Error not nor a super class
RuntimeException - if throwable was such an instance and exClass was not RuntimeException nor a super class. If throwable was not an instance of the expected exception class exClass, a runtime exception is throw nesting the original throwable.

toIOException

public static IOException toIOException(String msg,
                                        Throwable cause)
Jdk 1.6++ contains constructors with causes for IOException, but older jdk versions do not. This method simulates the missing constructor.

See Also:
IOException, Exception.Exception(String, Throwable)

toIOException

public static IOException toIOException(Throwable cause)
Jdk 1.6++ contains constructors with causes for IOException, but older jdk versions do not. This method simulates the missing constructor.

See Also:
IOException, Exception.Exception(Throwable)