You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by Henri Yandell <ba...@generationjava.com> on 2003/01/22 20:41:50 UTC

[lang] RE: Exception to XML?

I think you're right. Looks like we need to make a few improvements:

1) getStackTrace: Should this handle nested?

public static String getStackTrace(Throwable t) {
    StringWriter sw = new StringWriter();
    PrintWriter pw = new PrintWriter(sw, true);
    Throwable ts = getThrowables(t);
    for(int i=0; i<ts.length; i++) {
        ts[i].printStackTrace(pw);
        if(isCauseMethod(ts[i])) {
            break;
        }
    }
    return sw.getBuffer().toString()
}

static public boolean isCauseMethod(Throwable t) {
    // checks if this is a 'nested' exception. if so we trust it
    // to print itself and its children (?)
}

2)
  public void printStackTrace(Throwable t, PrintStream)  [calls
getStackTrace]
  public void printStackTrace(Throwable, PrintWriter)

Any views?

On Wed, 22 Jan 2003, Gary Gregory wrote:

> Hm, I do not see ExceptionUtils.toString(Throwable) in Commons-Lang v.1.0.1
> or in CVS (as of v.1.20, the latest as far as I can tell). To be more
> specific, what I really want to is something that will work with a
> NestableException or a JRE 1.4 Exception.
>
> Gary
>
> -----Original Message-----
> From: scolebourne@btopenworld.com [mailto:scolebourne@btopenworld.com]
> Sent: Wednesday, January 22, 2003 10:00 AM
> To: commons-user@jakarta.apache.org
> Subject: Re: Exception to XML?
>
>
> ExceptionUtils.toString(Throwable) IIRC.
> Stephen
>
> >  from:    Gary Gregory <gg...@seagullsw.com>
> >  date:    Wed, 22 Jan 2003 17:11:39
> >  to:      commons-user@jakarta.apache.org
> >  subject: Re: Exception to XML?
> >
> > Hello All,
> >
> > Is there something somewhere in the Commons or XML project that will let
> me
> > turn an Exception in XML (DOM or String)? I would then use this XML
> snippet
> > as part of a larger enclosing document.
> >
> > Thank you,
> > Gary
>
>
> --
> To unsubscribe, e-mail:
> <ma...@jakarta.apache.org>
> For additional commands, e-mail:
> <ma...@jakarta.apache.org>
>


Re: [lang] RE: Exception to XML?

Posted by Henri Yandell <ba...@generationjava.com>.
I'd like to propose the following two methods to ExceptionUtils:

1) getFullStackTrace. This returns a stack-trace including its full
print-out of nestables. It assumes that when it hits a nestable, it
doesn't need to carry on.

2) isNestedException. [okay, needs to be name changed to
isNestedThrowable]. This lets us see if a throwable is nestable, or at
least if ExceptionUtils thinks it is.

I would be aiming these for the 2.0 release. Any nays?

Hen

>      * A way to get the entire nested stack-trace of an throwable.
>      *
>      * @param t The <code>Throwable</code>.
>      * @return The nested stack trace, with the root cause first.
>      */
>     public static String getFullStackTrace(Throwable t) {
>         StringWriter sw = new StringWriter();
>         PrintWriter pw = new PrintWriter(sw, true);
>         Throwable[] ts = getThrowables(t);
>         for(int i=0; i<ts.length; i++) {
>             ts[i].printStackTrace(pw);
>             if(isNestedException(ts[i])) {
>                 break;
>             }
>         }
>         return sw.getBuffer().toString();
>     }
>
>     /**
>      * Whether an Throwable is considered nested or not.
>      *
>      * @param t The <code>Throwable</code>.
>      * @return boolean true/false
>      */
>     public static boolean isNestedException(Throwable throwable) {
>         if(throwable == null) {
>             return false;
>         }
>
>         if (throwable instanceof Nestable) {
>             return true;
>         } else if (throwable instanceof SQLException) {
>             return true;
>         } else if (throwable instanceof InvocationTargetException) {
>             return true;
>         }
>
>         int sz = CAUSE_METHOD_NAMES.length;
>         for(int i=0; i<sz; i++) {
>             try {
>                 Method method = throwable.getClass().getMethod(CAUSE_METHOD_NAMES[i], null);
>                 if(method != null) {
>                     return true;
>                 }
>             } catch (NoSuchMethodException ignored) {
>             } catch (SecurityException ignored) {
>             }
>         }
>
>         try {
>             Field field = throwable.getClass().getField("detail");
>             if(field != null) {
>                 return true;
>             }
>         } catch (NoSuchFieldException ignored) {
>         } catch (SecurityException ignored) {
>         }
>
>         return false;
>     }


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: [lang] RE: Exception to XML?

Posted by Henri Yandell <ba...@generationjava.com>.
I'd like to propose the following two methods to ExceptionUtils:

1) getFullStackTrace. This returns a stack-trace including its full
print-out of nestables. It assumes that when it hits a nestable, it
doesn't need to carry on.

2) isNestedException. [okay, needs to be name changed to
isNestedThrowable]. This lets us see if a throwable is nestable, or at
least if ExceptionUtils thinks it is.

I would be aiming these for the 2.0 release. Any nays?

Hen

>      * A way to get the entire nested stack-trace of an throwable.
>      *
>      * @param t The <code>Throwable</code>.
>      * @return The nested stack trace, with the root cause first.
>      */
>     public static String getFullStackTrace(Throwable t) {
>         StringWriter sw = new StringWriter();
>         PrintWriter pw = new PrintWriter(sw, true);
>         Throwable[] ts = getThrowables(t);
>         for(int i=0; i<ts.length; i++) {
>             ts[i].printStackTrace(pw);
>             if(isNestedException(ts[i])) {
>                 break;
>             }
>         }
>         return sw.getBuffer().toString();
>     }
>
>     /**
>      * Whether an Throwable is considered nested or not.
>      *
>      * @param t The <code>Throwable</code>.
>      * @return boolean true/false
>      */
>     public static boolean isNestedException(Throwable throwable) {
>         if(throwable == null) {
>             return false;
>         }
>
>         if (throwable instanceof Nestable) {
>             return true;
>         } else if (throwable instanceof SQLException) {
>             return true;
>         } else if (throwable instanceof InvocationTargetException) {
>             return true;
>         }
>
>         int sz = CAUSE_METHOD_NAMES.length;
>         for(int i=0; i<sz; i++) {
>             try {
>                 Method method = throwable.getClass().getMethod(CAUSE_METHOD_NAMES[i], null);
>                 if(method != null) {
>                     return true;
>                 }
>             } catch (NoSuchMethodException ignored) {
>             } catch (SecurityException ignored) {
>             }
>         }
>
>         try {
>             Field field = throwable.getClass().getField("detail");
>             if(field != null) {
>                 return true;
>             }
>         } catch (NoSuchFieldException ignored) {
>         } catch (SecurityException ignored) {
>         }
>
>         return false;
>     }