commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Nicola Ken Barozzi <nicola...@apache.org>
Subject Re: DBCP/LANG
Date Tue, 13 Aug 2002 19:15:33 GMT

Jack, Paul wrote:
>>Well, seriously speaking there are few other patterns I've seen:
>>  getNextException
>>  getRootCause
>>  getException
> 
> 
> Plus getTargetException() used by
> java.lang.reflect.InvocationTargetException,
> quite common.

Here is the relevent code from a Cocoon class
(http://cvs.apache.org/viewcvs.cgi/xml-cocoon2/src/java/org/apache/cocoon/components/notification/DefaultNotifyingBuilder.java?rev=HEAD&content-type=text/vnd.viewcvs-markup)
that does some exception walk, also with SAXExceptions.

   /**
    * Print recursively all nested causes of a Throwable in a PrintWriter.
    */
   private static void appendCauses (PrintWriter out, Throwable t) {
     Throwable cause = null;
     if (t instanceof CascadingThrowable) {
       cause = ((CascadingThrowable) t).getCause();
     } else if (t instanceof SAXException) {
       cause = ((SAXException) t).getException();
     } else if (t instanceof java.sql.SQLException) {
       cause = ((java.sql.SQLException) t).getNextException();
     }
     if (cause != null) {
       out.print("Original exception : ");
       cause.printStackTrace(out);
       out.println();
       // Recurse
       appendCauses(out, cause);
     }
   }

   /**
    * Get root Exception.
    */
   private static Throwable getRootCause (Throwable t) {
     Throwable cause = null;
     if (t instanceof CascadingThrowable) {
       cause = ((CascadingThrowable) t).getCause();
     } else if (t instanceof SAXException) {
       cause = ((SAXException) t).getException();
     } else if (t instanceof java.sql.SQLException) {
       cause = ((java.sql.SQLException) t).getNextException();
     }
     if (cause == null) {
       return t;
     } else {
       // Recurse
       return getRootCause(cause);
     }
   }


-- 
Nicola Ken Barozzi                   nicolaken@apache.org
             - verba volant, scripta manent -
    (discussions get forgotten, just code remains)
---------------------------------------------------------------------


--
To unsubscribe, e-mail:   <mailto:commons-dev-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:commons-dev-help@jakarta.apache.org>


Mime
View raw message