I use declaritive exception handling which is built into Struts.  Works great 
for me.  I really don't use custom exceptions that much any more because it's 
harder to track the original error.  Also, I don't clutter my code with alot of 
try/catch blocks unless it's absolutely necessary.  I allow all exceptions to 
propagate through the Struts built-in exception handling framework.

Here is an example of declaritive exception handling in the struts config:

        <global-exceptions>
                <exception
                        key="error.application.nullpointer"
                        type="java.lang.NullPointerException"
                        
path="/tmpl_main2.jsp?error=/error/null.jsp&amp;pagetitle=error.title.key">
                </exception>
                <exception
                        key="error.application.parse"
                        type="java.text.ParseException"
                        
path="/tmpl_main2.jsp?pageleft=/mainmenu.jsp&amp;pagecenter=/error/syserror.jsp&amp;pagetitle=error.title.key">
                </exception>
                <exception
                        key="error.application.import.zip"
                        type="java.util.zip.ZipException"
                        
path="/tmpl_main2.jsp?pageleft=/mainmenu.jsp&amp;pagecenter=/error/syserror.jsp&amp;pagetitle=error.title.key">
                </exception>
                <exception 
                        key="error.application.unhandled" 
                        type="java.lang.Exception" 
                        
path="/tmpl_main2.jsp?pageleft=/mainmenu.jsp&amp;pagecenter=/error/syserror.jsp&amp;pagetitle=error.title.key">
                </exception>

When these exception types are thrown, the user is forwarded to the proper 
custom page.  If your creative you can grab the current error and print the 
stack trace as an HTML comment within the page for your review.  You can then 
view the exception stack trace by selecting view/source from you browser.  Only 
show the user and nice "pretty" message.


Jim


-----Original Message-----
From: Brian McGovern <[EMAIL PROTECTED]>
Sent: Feb 15, 2005 10:28 AM
To: user@struts.apache.org
Subject: Proper n tiered exception handling

I'm looking to get a handle on best exception handling practices in my app.  
Kinda beginner question i guess, sorry.

Im catching the various sql and naming exceptions in the data classes and 
logging and throwing a custom exception called ApplicationException which is 
blank and provided below.  I've read up but an still a little confused on how 
exactly to build this exception class to extend the available struts exception 
classes that would gracefully map to an error display jsp where i could display 
the error to the user.  Right now I just get the HTTP status 500 strack trace 
on the screen.  

Controller snippet that catches the data obj's thrown ApplicationException:
------------------------------------------------------------------------
try {
        zRepBeanBn = 
MyData.getRepByID(StringUtils.convertToInt(request.getParameter("RepID")));
}catch (ApplicationException zAppEx){
        throw zAppEx;
}
------------------------------------------------------------------------

ApplicationException that needs work:  How do I extend this?
------------------------------------------------------------------------
public class ApplicationException extends Exception {
    public ApplicationException(String message) {  }
}
------------------------------------------------------------------------

Thanks









---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to