>>> Marc Krisjanous <[EMAIL PROTECTED]> 28-Sep-00 5:24:33 AM
>>>

>Now, the perfromTask method throws an
>ExceptionController(extends Exception)
>instance which is caught in the doGet or doPost methods.
>Then I currently send the error back to the web browser.
> The question is: is this good exception handling??

It's perfectly acceptable.

Personally I'd do the work that performTask does inside service() and
catch all the different exceptions seperately.

The thing is that you can't use a formula for exception handling...
it's down to what you want to do at the time and in the
circumstances:

- handle any exception that could be produced in the same way...
- handle particular bits of code with special handlers
- handle all exceptions specifically

This is judgement... if you think hard about what you want to do now
it'll come and it'll get easier.

Something that can help is looking at good free software (open source
code) to see how the pros do it.


Here's a few pointers, but even these can't be taken too literally:

- always catch Throwable not Exception for generic exceptions
Throwable is actually the lowest exception... if you just catch
Exception your code will blow up if it gets a Throwable

- for small code use a single try/catch with specific exception
handlers

- for larger code use a try/catch around code that might blow up

- use the ServletContext.log method that allows writing stack traces
to the log

- with servlets think hard about the link between an exception and an
HTTP error
is it a  500? a 501? a 400? what?


Lastly, here's a code fragment that you can use to print stack traces
to an html page.

The Throwable is called: t.
The Writer connected to the response is called: m_out

      //we need to do this to collect the stacktrace lines
      StringWriter buf=new StringWriter();
      PrintWriter stackout=new PrintWriter(buf);
      t.printStackTrace(stackout);
      //create the tokenizer to get each line of input
      StringTokenizer st=new
StringTokenizer(buf.getBuffer().toString(),"\n");
      while(st.hasMoreTokens())
      {
        m_out.println(st.nextToken());
        m_out.println("<br>");
      }
      m_out.println("</p>");

This can be usefull... eg: I use it when I get something that I class
as a 500.


Nic

___________________________________________________________________________
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

Reply via email to