info.magnolia.cms.util
Class ExceptionUtil

java.lang.Object
  extended by info.magnolia.cms.util.ExceptionUtil

public class ExceptionUtil
extends Object

Util to handle exceptions.

Version:
$Revision: $ ($Author: $)

Constructor Summary
ExceptionUtil()
           
 
Method Summary
static String classNameToWords(Exception e)
          Translates an exception class name to an english-readable idiom.
static String exceptionToWords(Exception e)
          Translates an exception class name to an english-readable idiom, along with the exception's message.
static void rethrow(Throwable e, Class<? extends Throwable>... allowedExceptions)
          This method helps palliating the absence of multi-catch (introduced in Java 7) - catch the lower common denominator exception and let this method do the rest - Use with great care!
static
<E extends Throwable>
void
unwrapIf(RuntimeException e, Class<E> unwrapIf)
          Given a RuntimeException, this method will: - throw its cause exception, if the cause exception is an instance of the type of the unwrapIf parameter - throw its cause exception, if the cause exception is a RuntimeException - throw the given RuntimeException otherwise.
static boolean wasCausedBy(Throwable e, Class<? extends Throwable> suspectedCause)
          Returns true if the given exception or any of the nested cause exceptions is an instance of the suspectedCause exception argument, or a subclass thereof.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ExceptionUtil

public ExceptionUtil()
Method Detail

unwrapIf

public static <E extends Throwable> void unwrapIf(RuntimeException e,
                                                  Class<E> unwrapIf)
                     throws E extends Throwable
Given a RuntimeException, this method will: - throw its cause exception, if the cause exception is an instance of the type of the unwrapIf parameter - throw its cause exception, if the cause exception is a RuntimeException - throw the given RuntimeException otherwise.

Throws:
E extends Throwable

rethrow

public static void rethrow(Throwable e,
                           Class<? extends Throwable>... allowedExceptions)
This method helps palliating the absence of multi-catch (introduced in Java 7) - catch the lower common denominator exception and let this method do the rest - Use with great care!. Warning: this method can be abused, and would let one throw undeclared exceptions, which would in turn produce unexpected and undesirable effects on calling code. Just resist the urge to use this outside "multi-catch" scenarios.


wasCausedBy

public static boolean wasCausedBy(Throwable e,
                                  Class<? extends Throwable> suspectedCause)
Returns true if the given exception or any of the nested cause exceptions is an instance of the suspectedCause exception argument, or a subclass thereof. This is equivalent to ExceptionUtils.indexOfThrowable(e, javax.jcr.AccessDeniedException.class) >= 0, only more readable, and possibly more performant.


classNameToWords

public static String classNameToWords(Exception e)
Translates an exception class name to an english-readable idiom. Example: an instance of AccessDeniedException will be returned as "Access denied".


exceptionToWords

public static String exceptionToWords(Exception e)
Translates an exception class name to an english-readable idiom, along with the exception's message. Example: an instance of AccessDeniedException("/foo/bar") will be returned as "Access denied: /foo/bar".



Copyright © 2003–2014 Magnolia International Ltd.. All rights reserved.