which exceptions to catch in a serlet

A

Andy Fish

Hi,

I'm trying to find some documentation or guidelines on how to handle
exceptions within my servlet (i.e. within the doGet or doPost method).
specifically:

1. if an error occurs and I have handled the problem and sent the correct
response to the client, should I still throw ServletException? what will the
container do if I throw this exception?

2. what about unchecked exceptions, should I catch these and throw
ServletException or should I just let them propogate up?

I have had a look at the servlet spec but it doesn't really seem to say
much, especially about unchecked exceptions

Andy
 
Z

zoopy

Hi,

I'm trying to find some documentation or guidelines on how to handle
exceptions within my servlet (i.e. within the doGet or doPost method).
specifically:

1. if an error occurs and I have handled the problem and sent the correct
response to the client, should I still throw ServletException?
No

what will the
container do if I throw this exception?

If the webapplication defined error pages (<error-page> tags in deployment descriptor), the
servletcontainer tries to send a matching error page. If no such page is found, the container sends
a HTTP 500 response (Internal server error).
2. what about unchecked exceptions, should I catch these and throw
ServletException or should I just let them propogate up?

You could catch them, but the container tries to match an error page, as above.
I have had a look at the servlet spec but it doesn't really seem to say
much, especially about unchecked exceptions

Handling errors is described in section 9.9.2 of the Java Servlet Specification 2.4
( said:
Regards,
Z.
 
O

Oscar kind

zopy said:
2. what about unchecked exceptions, should I catch these and throw
ServletException or should I just let them propogate up?

You could catch them, but the container [will display an error page,
user-defined or otherwise].

And IMO, you generally should not. Unchecked exceptions are mostly a sign
that the code contains a bug. However, this is not "The Truth":

A design choice (mostly J2EE-specific) could be to use these unchecked
exceptions to display specific error pages for specific exceptions. This
means that you have two exception designs (Sun's and your own), but it
can be useful. I try not to do this as mixing designs tends to be
confusing, but YMMV.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top