Hi Marc,
Exception handling is always one of the harder things to do gracefully. I've
got a couple of comments for you to to consider.
* It seems sort of backwards for your servlet (the "customer" of
the action classes) to dictate what exceptions they can throw.
The typical style would be to have the action class itself decide
what exceptions it can throw, and declare them in the "throws"
clause and/or Javadocs. Then, the caller knows what they have
to be prepared to deal with.
* To me, an exception is an "event message". It contains data
describing what happened, but normally contains no executable
logic of its own. Thus, the idea of "executing" an exception for
logging seems rather odd. The decision to log (and the mechanics
of where and how) are up to the class that catches the exception,
not to the exception itself.
* Although you can define what "checked" exceptions are thrown
by your actions and other classes, you must still be ready to
deal with the fact that your actions can throw "runtime exception"
errors like NullPointerException, IllegalArgumentException, and
so on. As you've described it below, your are only catching
ExceptionController errors -- a null pointer exception in your action
is going to get caught by the servlet container.
I'd suggest something like this in your outer servlet's doGet() or doPost()
method:
try {
... call the right action ...
} catch (Throwable t) {
getServletContext().log("My action threw an exception", t);
if (t instanceof SevereError) {
... do something ...
} else if (t instanceof SomeOtherError) {
... do something else ...
} ... and so on
}
the servlet context log() call logs your message, the "message" from the
exception itself, and the stack trace. It works no matter what checked
exception or runtime exception is thrown. And it doesn't impose any
restrictions on the types of exceptions that can be thrown by actions --
instead, you can make these descriptive of the particular type of problem that
is being reported.
Craig McClanahan
PS: If you look at the source code of a servlet container like Tomcat, you'll
find that something very similar to the above is wrapped around the call to your
servlet's service() method.
Marc Krisjanous wrote:
> Hi all,
>
> I would like to tell you of a concept I have for error handling in Servlets.
> I would be delighted if anyone can provide feedback and recommendations
> regarding this concept.
>
> Concept:
>
> Every servlet and bean within a project attempts to execute actions. If an
> action causes an exception an ExceptionController object is thrown. When
> thrown eg.
>
> throw new ExceptionController(message);
>
> the ExceptionController writes the error to the error log and performs
> analysis on the error to determine what level the exception is eg is it
> critical, is it medium or is it minor.
>
> In the catch block the method catching it would perform any clean up and
> then call the ExceptionControllers execute method. The ExceptionController
> would then work out what to do with the error based on the level.
>
> An example:
> Calling method.....
> try{
>
> performTask(request,response);
>
> }
> catch(ExceptionController exControll)
> {
> //do any clean required
> //then tell the ExceptionController to execute the error
> exControll.execute();
> }
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> private void performTask(HttpServletRequest request, HttpServletResponse
> response) throws ExceptionController{
>
> //this method has had an error....
> catch(Exception e){
>
> throw new ExceptionController(e.getMessage());
>
> }
>
> ___________________________________________________________________________
> To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
> of the message "signoff SERVLET-INTEREST".
>
> Archives: http://archives.java.sun.com/archives/servlet-interest.html
> Resources: http://java.sun.com/products/servlet/external-resources.html
> LISTSERV Help: http://www.lsoft.com/manuals/user/user.html
--
====================
See you at ApacheCon Europe <http://www.apachecon.com>!
Session VS01 (23-Oct 13h00-17h00): Sun Technical Briefing
Session T06 (24-Oct 14h00-15h00): Migrating Apache JServ
Applications to Tomcat
___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".
Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html