You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@struts.apache.org by Rick Hightower <ri...@eblox.com> on 2001/11/16 09:20:45 UTC

Exception handling --- suggestion for improvements ---- (e.g., template tag masks original exception)

We have been using struts for a good while. We really dig the framework,
but...

Often times the struts tags catch the original exception (e.g.,
ClassCastException), and then throw a JspException; thus, losing the
original stack trace. This hides\masks the original exception. Hiding the
orginal stack trace makes it harder to debug the problem.

I wrote a JSPWrapperException that preserves the orginal stack trace for
debugging.
JSPWrapperException extends JspWrapper; however, like ServletException, it
captures the original stack trace and displays it for debugging, i.e., the
JSPWrapperException prints out the original stack trace of the original
exception. This is a real boon for debugging.

I modified 50 or so files in our copy of the struts code base (a 1.0
derivative with some bug fixes and extra error handing) to use the
JSPWrapperException instead of the JspException.

It cost about an hour to make the changes, but I feel it will save us hours
of debugging in the future.

There is an ant build file with struts so making the changes and creating
struts.jar was easy.



I search the struts code base for code like this.... (example code)

try{
}
catch(XYZException e){
	throw new JspException(msg);
}

to code that looks like this
try{
}
catch(XYZException e){
	throw new JspException(e, msg);
}


BTW Here is the code for JspWrapperException....

Enjoy.....

/*
 * JspWrapperException.java
 *
 * Created on November 15, 2001, 11:14 PM
 */

package org.apache.struts.util;
import javax.servlet.jsp.JspException;

/**
 *
 * @author  rick
 */
public class JspWrapperException extends JspException  {

        Exception e;

    /**
     * @param     Exception e
     * @param     String message
     */
    public JspWrapperException(Exception e, String message) {
        super(message);
        this.e = e;
    }

    /**
     */
    public void printStackTrace () {
        super.printStackTrace();
        String sep = System.getProperty("line.separator", "\r\n");
        if (e != null) {
            System.err.println("--------------- extended Exception
nest ----------- ");
            e.printStackTrace();
        }
    }

    /**
     * @param ps
     */
    public void printStackTrace (java.io.PrintStream ps) {
        super.printStackTrace(ps);
        String sep = System.getProperty("line.separator", "\r\n");
        if (e != null) {
            ps.println("--------------- extended Exception nest -----------
");
            e.printStackTrace(ps);
        }
    }

    /**
     * @param pw
     */
    public void printStackTrace (java.io.PrintWriter pw) {
        super.printStackTrace(pw);
        String sep = System.getProperty("line.separator", "\r\n");
            //
            //Nested exception
        if (e != null) {
            pw.println("--------------- extended Exception nest -----------
");
            e.printStackTrace(pw);
        }
    }

    /**
     * @return
     */
    public String getMessage () {
        StringBuffer message = new StringBuffer(150);
        message.append(super.getMessage());

            //
            //add Line separator
        String sep = System.getProperty("line.separator", "\r\n");
        message.append(sep);

            //
            //Add the nested exception
        if (e != null) {
            message.append(e.getMessage());
            //char = props["line.separator"]
        }
        return  message.toString();
    }
}



Rick Hightower
Director of Development
eBlox, Inc.

Check out our new website!
www.eblox.com

Contact Info:
eBlox Tucson
phone: 520-615-9345 x103
fax: 520-529-5774

Rick's stuff:
http://www.eblox.com/people_detail.php?id=52
http://www.geocities.com/rick_m_hightower/
http://www.brainbench.com/transcript.jsp?pid=2351036


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


Re: Exception handling --- suggestion for improvements ---- (e.g., template tag masks original exception)

Posted by Erik Hatcher <ja...@ehatchersolutions.com>.
I recommend switching the parameters around in your JspWrapperException to
match the Servlet 2.3's constructor order (message, exception, rather than
exception, message) and that will make the switch later much easier (just
change the classname, and not have to fiddle with re-ordering parameters).

You rock, Rick!

    Erik



----- Original Message -----
From: "Rick Hightower" <ri...@eblox.com>
To: "Struts Developers List" <st...@jakarta.apache.org>
Sent: Friday, November 16, 2001 9:49 AM
Subject: RE: Exception handling --- suggestion for improvements ---- (e.g.,
template tag masks original exception)


> I think it is worth it to add the JspWrapperException in lieu of waiting
for
> the next Servlet spec. to further its adoption.
>
> Hiding the original stack trace makes debugging tough. This is especially
> frustrating to new users. I did this to our copy of Struts last night, and
> it only took an hour.
>
> In six months or a year when everyone is using the latest version of the
> Servlet spec., it will only take an hour to switch to the new JspException
> constructor if so desired.
>
> BTW I volunteer to make the change.
>
>
> Rick Hightower
> Director of Development
> eBlox, Inc.
>
> Check out our new website!
> www.eblox.com
>
> Contact Info:
> eBlox Tucson
> phone: 520-615-9345 x103
> fax: 520-529-5774
>
> Rick's stuff:
> http://www.eblox.com/people_detail.php?id=52
> http://www.geocities.com/rick_m_hightower/
> http://www.brainbench.com/transcript.jsp?pid=2351036
>
>
> -----Original Message-----
> From: craigmcc@neo.austinite.com [mailto:craigmcc@neo.austinite.com]On
> Behalf Of Craig R. McClanahan
> Sent: Friday, November 16, 2001 10:07 AM
> To: Struts Developers List
> Subject: Re: Exception handling --- suggestion for improvements ----
> (e.g., template tag masks original exception)
>
>
>
>
> On Fri, 16 Nov 2001, Rick Hightower wrote:
>
> > Date: Fri, 16 Nov 2001 01:20:45 -0700
> > From: Rick Hightower <ri...@eblox.com>
> > Reply-To: Struts Developers List <st...@jakarta.apache.org>
> > To: struts-dev@jakarta.apache.org
> > Subject: Exception handling --- suggestion for improvements ---- (e.g.,
> >      template tag masks original exception)
> >
> > We have been using struts for a good while. We really dig the framework,
> > but...
> >
> > Often times the struts tags catch the original exception (e.g.,
> > ClassCastException), and then throw a JspException; thus, losing the
> > original stack trace. This hides\masks the original exception. Hiding
the
> > orginal stack trace makes it harder to debug the problem.
> >
>
> In JSP 1.2, the capability to add a root cause exception (the way
> ServletException does it) was added to JspException, so once we migrate to
> that platform as a minimum we'll be able to modify Struts to use it.  How
> important is it to deal with this issue in the mean time?
>
> Craig
>
>
> --
> To unsubscribe, e-mail:
<ma...@jakarta.apache.org>
> For additional commands, e-mail:
<ma...@jakarta.apache.org>
>
>
> --
> To unsubscribe, e-mail:
<ma...@jakarta.apache.org>
> For additional commands, e-mail:
<ma...@jakarta.apache.org>
>
>


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


RE: Exception handling --- suggestion for improvements ---- (e.g., template tag masks original exception)

Posted by Rick Hightower <ri...@eblox.com>.
I think it is worth it to add the JspWrapperException in lieu of waiting for
the next Servlet spec. to further its adoption.

Hiding the original stack trace makes debugging tough. This is especially
frustrating to new users. I did this to our copy of Struts last night, and
it only took an hour.

In six months or a year when everyone is using the latest version of the
Servlet spec., it will only take an hour to switch to the new JspException
constructor if so desired.

BTW I volunteer to make the change.


Rick Hightower
Director of Development
eBlox, Inc.

Check out our new website!
www.eblox.com

Contact Info:
eBlox Tucson
phone: 520-615-9345 x103
fax: 520-529-5774

Rick's stuff:
http://www.eblox.com/people_detail.php?id=52
http://www.geocities.com/rick_m_hightower/
http://www.brainbench.com/transcript.jsp?pid=2351036


-----Original Message-----
From: craigmcc@neo.austinite.com [mailto:craigmcc@neo.austinite.com]On
Behalf Of Craig R. McClanahan
Sent: Friday, November 16, 2001 10:07 AM
To: Struts Developers List
Subject: Re: Exception handling --- suggestion for improvements ----
(e.g., template tag masks original exception)




On Fri, 16 Nov 2001, Rick Hightower wrote:

> Date: Fri, 16 Nov 2001 01:20:45 -0700
> From: Rick Hightower <ri...@eblox.com>
> Reply-To: Struts Developers List <st...@jakarta.apache.org>
> To: struts-dev@jakarta.apache.org
> Subject: Exception handling --- suggestion for improvements ---- (e.g.,
>      template tag masks original exception)
>
> We have been using struts for a good while. We really dig the framework,
> but...
>
> Often times the struts tags catch the original exception (e.g.,
> ClassCastException), and then throw a JspException; thus, losing the
> original stack trace. This hides\masks the original exception. Hiding the
> orginal stack trace makes it harder to debug the problem.
>

In JSP 1.2, the capability to add a root cause exception (the way
ServletException does it) was added to JspException, so once we migrate to
that platform as a minimum we'll be able to modify Struts to use it.  How
important is it to deal with this issue in the mean time?

Craig


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


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


Re: Exception handling --- suggestion for improvements ---- (e.g., template tag masks original exception)

Posted by "Craig R. McClanahan" <cr...@apache.org>.

On Fri, 16 Nov 2001, Rick Hightower wrote:

> Date: Fri, 16 Nov 2001 01:20:45 -0700
> From: Rick Hightower <ri...@eblox.com>
> Reply-To: Struts Developers List <st...@jakarta.apache.org>
> To: struts-dev@jakarta.apache.org
> Subject: Exception handling --- suggestion for improvements ---- (e.g.,
>      template tag masks original exception)
>
> We have been using struts for a good while. We really dig the framework,
> but...
>
> Often times the struts tags catch the original exception (e.g.,
> ClassCastException), and then throw a JspException; thus, losing the
> original stack trace. This hides\masks the original exception. Hiding the
> orginal stack trace makes it harder to debug the problem.
>

In JSP 1.2, the capability to add a root cause exception (the way
ServletException does it) was added to JspException, so once we migrate to
that platform as a minimum we'll be able to modify Struts to use it.  How
important is it to deal with this issue in the mean time?

Craig


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


Exception handling --- suggestion for improvements ---- (e.g., template tag masks original exception) correction in sample code

Posted by Rick Hightower <ri...@eblox.com>.
We have been using struts for a good while. We really dig the framework,
but...

Often times the struts tags catch the original exception (e.g.,
ClassCastException), and then throw a JspException; thus, losing the
original stack trace. This hides\masks the original exception. Hiding the
orginal stack trace makes it harder to debug the problem.

I wrote a JSPWrapperException that preserves the orginal stack trace for
debugging.
JSPWrapperException extends JspWrapper; however, like ServletException, it
captures the original stack trace and displays it for debugging, i.e., the
JSPWrapperException prints out the original stack trace of the original
exception. This is a real boon for debugging.

I modified 50 or so files in our copy of the struts code base (a 1.0
derivative with some bug fixes and extra error handing) to use the
JSPWrapperException instead of the JspException.

It cost about an hour to make the changes, but I feel it will save us hours
of debugging in the future.

There is an ant build file with struts so making the changes and creating
struts.jar was easy.



I search the struts code base for code like this.... (example code)

try{
}
catch(XYZException e){
	throw new JspException(e.getMessage());
}

to code that looks like this
try{
}
catch(XYZException e){
	throw new JspWrapperException(e, e.getMessage());
}


BTW Here is the code for JspWrapperException....

Enjoy.....

/*
 * JspWrapperException.java
 *
 * Created on November 15, 2001, 11:14 PM
 */

package org.apache.struts.util;
import javax.servlet.jsp.JspException;

/**
 *
 * @author  rick
 */
public class JspWrapperException extends JspException  {

        Exception e;

    /**
     * @param     Exception e
     * @param     String message
     */
    public JspWrapperException(Exception e, String message) {
        super(message);
        this.e = e;
    }

    /**
     */
    public void printStackTrace () {
        super.printStackTrace();
        String sep = System.getProperty("line.separator", "\r\n");
        if (e != null) {
            System.err.println("--------------- extended Exception
nest ----------- ");
            e.printStackTrace();
        }
    }

    /**
     * @param ps
     */
    public void printStackTrace (java.io.PrintStream ps) {
        super.printStackTrace(ps);
        String sep = System.getProperty("line.separator", "\r\n");
        if (e != null) {
            ps.println("--------------- extended Exception nest -----------
");
            e.printStackTrace(ps);
        }
    }

    /**
     * @param pw
     */
    public void printStackTrace (java.io.PrintWriter pw) {
        super.printStackTrace(pw);
        String sep = System.getProperty("line.separator", "\r\n");
            //
            //Nested exception
        if (e != null) {
            pw.println("--------------- extended Exception nest -----------
");
            e.printStackTrace(pw);
        }
    }

    /**
     * @return
     */
    public String getMessage () {
        StringBuffer message = new StringBuffer(150);
        message.append(super.getMessage());

            //
            //add Line separator
        String sep = System.getProperty("line.separator", "\r\n");
        message.append(sep);

            //
            //Add the nested exception
        if (e != null) {
            message.append(e.getMessage());
            //char = props["line.separator"]
        }
        return  message.toString();
    }
}



Rick Hightower
Director of Development
eBlox, Inc.

Check out our new website!
www.eblox.com

Contact Info:
eBlox Tucson
phone: 520-615-9345 x103
fax: 520-529-5774

Rick's stuff:
http://www.eblox.com/people_detail.php?id=52
http://www.geocities.com/rick_m_hightower/
http://www.brainbench.com/transcript.jsp?pid=2351036


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



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