org.zkoss.lang
Class Exceptions

java.lang.Object
  extended by org.zkoss.lang.Exceptions

public class Exceptions
extends Object

Utilities for Exceptions.

Author:
tomyeh

Constructor Summary
Exceptions()
           
 
Method Summary
static Throwable findCause(Throwable ex, Class<?> cause)
          Finds the causes of an exception, ex, to see whether any of them is the given type.
static StringBuffer formatStackTrace(StringBuffer sb, Throwable t, String prefix)
          Formats the stack trace and appends it to the specified string buffer.
static StringBuffer formatStackTrace(StringBuffer sb, Throwable t, String prefix, int maxcnt)
          Formats the stack trace and appends it to the specified string buffer, but only display at most maxcnt lines.
static String formatStackTrace(Throwable t, String prefix)
          Formats the stack trace and returns the result.
static String getBriefStackTrace(Throwable t)
          Returns the first few lines of the stack trace.
static Throwable getCause(Throwable ex)
          Returns the cause of the given throwable.
static String getExtraMessage(Throwable ex)
          Returns the extra message, or null if no extra.
static String getMessage(int code)
          Gets the message for an exception without format-argument.
static String getMessage(int code, Object fmtArg)
          Gets the message for an exception with one format-argument.
static String getMessage(int code, Object[] fmtArgs)
          Gets the message for an exception's getMessage method.
static String getMessage(Throwable ex)
          Returns a message of the exception.
static Throwable getRealCause(Throwable ex)
          Unveils the real cause.
static Throwable unwrap(Throwable ex)
          Unwraps an exception if the enclosing one is InvocationTargetException or UndeclaredThrowableException.
static Throwable wrap(Throwable ex, Class<? extends Throwable> targetExceptCls)
          Converts an exception to the specified class.
static Throwable wrap(Throwable ex, Class<? extends Throwable> targetExceptCls, int code)
          Converts an exception to the specified class plus a message.
static Throwable wrap(Throwable ex, Class<? extends Throwable> targetExceptCls, int code, Object fmtArg)
          Converts an exception to the specified class plus a message.
static Throwable wrap(Throwable ex, Class<? extends Throwable> targetExceptCls, int code, Object[] fmtArgs)
          Converts an exception to the specified class plus a message.
static Throwable wrap(Throwable ex, Class<? extends Throwable> targetExceptCls, String msg)
          Converts an exception to the specified class plus a message.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Exceptions

public Exceptions()
Method Detail

findCause

public static final Throwable findCause(Throwable ex,
                                        Class<?> cause)
Finds the causes of an exception, ex, to see whether any of them is the given type.

Returns:
the cause if found; null if not found

getCause

public static final Throwable getCause(Throwable ex)
Returns the cause of the given throwable. It is the same as t.getCause, but it solves the compatibility of J2EE that might not support JDK 1.4.


getRealCause

public static final Throwable getRealCause(Throwable ex)
Unveils the real cause. A throwable object is called a real cause, if it doesn't have any cause (or called chained throwable).

Parameters:
ex - the throwable
Returns:
the real cause; ex itself if it is already the real cause
See Also:
wrap(java.lang.Throwable, java.lang.Class)

wrap

public static final Throwable wrap(Throwable ex,
                                   Class<? extends Throwable> targetExceptCls)
Converts an exception to the specified class. If any of causes is the specified class, the cause is returned. If the giving exception is RuntimeException or error, it is re-thrown directly. In other words, Unlike findCause(java.lang.Throwable, java.lang.Class), it is designed to be used in the throw statement as follows.

Usage 1: one kind of exception is examined.


 try {
   ...
 } catch (Exception ex) {
   throw (MyException)Exceptions.wrap(ex, MyException.class);
 }

Usage 2: two kinds of exceptions are examined.


 try {
   ...
 } catch (Exception ex) {
   Throwable t = Exceptions.findCause(ex, AnotherException.class);
   if (t != null)
      throw (AnotherException)t;
   throw (MyException)Exceptions.wrap(ex, MyException.class);
 }

Usage 3: two kinds of exceptions are examined.


 try {
   ...
 } catch (AnotherException ex) {
   throw ex;
 } catch (Exception ex) {
   throw (MyException)Exceptions.wrap(ex, MyException.class);
 }

If you already know the exception, you don't need this method -- AnotherException won't never be RuntimeException or MyException.


 try {
   ...
 } catch (AnotherException ex) {
   throw new MyException(ex);
 }

Note: It assumes the exception has the constructor: MyException(Throwable cause);

Parameters:
targetExceptCls - the class of exception to be converted to
ex - the exception being caught (and to be converted)
See Also:
getRealCause(java.lang.Throwable), SystemException.Aide.wrap(java.lang.Throwable)

wrap

public static final Throwable wrap(Throwable ex,
                                   Class<? extends Throwable> targetExceptCls,
                                   String msg)
Converts an exception to the specified class plus a message. It is similar to wrap(Throwable, Class) but with an additional message. Thus, the target exception class must has the constructor: MyException(String msg, Throwable cause);


wrap

public static final Throwable wrap(Throwable ex,
                                   Class<? extends Throwable> targetExceptCls,
                                   int code,
                                   Object[] fmtArgs)
Converts an exception to the specified class plus a message. It is similar to wrap(Throwable, Class) but with an additional message code. Thus, the target exception class must has the constructor: MyException(int code, Object[] fmtArgs, Throwable cause);


wrap

public static final Throwable wrap(Throwable ex,
                                   Class<? extends Throwable> targetExceptCls,
                                   int code,
                                   Object fmtArg)
Converts an exception to the specified class plus a message. It is similar to wrap(Throwable, Class) but with an additional message code. Thus, the target exception class must has the constructor: MyException(int code, Object fmtArgs, Throwable cause);


wrap

public static final Throwable wrap(Throwable ex,
                                   Class<? extends Throwable> targetExceptCls,
                                   int code)
Converts an exception to the specified class plus a message. It is similar to wrap(Throwable, Class) but with an additional message code. Thus, the target exception class must has the constructor: MyException(int code, Throwable cause);


unwrap

public static final Throwable unwrap(Throwable ex)
Unwraps an exception if the enclosing one is InvocationTargetException or UndeclaredThrowableException.

Use it if you are catching exceptions thrown by Method.invoke().


getExtraMessage

public static final String getExtraMessage(Throwable ex)
Returns the extra message, or null if no extra.

Currently, it handles only SQLException


getMessage

public static final String getMessage(Throwable ex)
Returns a message of the exception.


getMessage

public static final String getMessage(int code,
                                      Object[] fmtArgs)
Gets the message for an exception's getMessage method.

It actually delegates to Messages, and then appends an error code.


getMessage

public static final String getMessage(int code,
                                      Object fmtArg)
Gets the message for an exception with one format-argument.


getMessage

public static final String getMessage(int code)
Gets the message for an exception without format-argument.


getBriefStackTrace

public static final String getBriefStackTrace(Throwable t)
Returns the first few lines of the stack trace. It is useful to make the cause exception's stack trace to be part of the message of the exception thrown to the user.


formatStackTrace

public static final String formatStackTrace(Throwable t,
                                            String prefix)
Formats the stack trace and returns the result. Currently, it only adds the prefix to each line.

Parameters:
prefix - the prefix shown in front of each line of the stack trace; null to denote empty
See Also:
Log.realCause(java.lang.Throwable)

formatStackTrace

public static final StringBuffer formatStackTrace(StringBuffer sb,
                                                  Throwable t,
                                                  String prefix)
Formats the stack trace and appends it to the specified string buffer.

Parameters:
sb - the string buffer to append the stack trace. A string buffer will be created if null.
prefix - the prefix shown in front of each line of the stack trace; null to denote empty

formatStackTrace

public static final StringBuffer formatStackTrace(StringBuffer sb,
                                                  Throwable t,
                                                  String prefix,
                                                  int maxcnt)
Formats the stack trace and appends it to the specified string buffer, but only display at most maxcnt lines.

The maximal allowed number of lines is controlled by maxcnt. Note: a stack frame is not counted, if it belongs to java.*, javax.* or sun.*.

Parameters:
sb - the string buffer to append the stack trace. A string buffer will be created if null.
prefix - the prefix shown in front of each line of the stack trace; null to denote empty
maxcnt - the maximal allowed number of lines to dump (<=0: no limit)


Copyright © 2013. All rights reserved.