Forums Register Login

ServletException

+Pie Number of slices to send: Send
Could you please explain the significance of ServletException class and the different ways of handling exception/error in a Servlets?


Thanks a lot...
+Pie Number of slices to send: Send
Servlet exception is what the container is checking for.

If your exceptions subclass ServletException, you can throw them and let the container handle them for you.

You can also chain exceptions by passing non-ServletExceptions to ServletException's constuctor.
+Pie Number of slices to send: Send
Do we need to rethrow a ServletException catching any normal exception to let the container handle the exception?

For example: if my code has a chance of giving NumberFormatException, do i need to catch NFE and rethrow servletexception?

Please explain elaborately.
+Pie Number of slices to send: Send
Yes, if you want the container to handle your NullPointerException you would re-throw it as a ServletException.
+Pie Number of slices to send: Send
What does it mean like "the container will handle the exception"?

Could you please explain with a scenario?

Thanks a lot!!!
+Pie Number of slices to send: Send
The default behavior for most of the containers I know of is to send 500 error code along with a stack trace in the response body.

Using the <error-page> entry in web.xml you can declare your own JSP pages to be presented for various error/exception types.
+Pie Number of slices to send: Send
Even if we don't throw servletexception, we will get that default stacktrace. so, what is the point in throwing it?

Please clarify...
+Pie Number of slices to send: Send
NumberFormatException probably isn't a good example because it's an unchecked exception.

Operations that throw checked exceptions have to be dealt with in your code in order for your servlet to be compiled.
In these cases, you might want to catch them and re-throw them to let the container deal with them.
+Pie Number of slices to send: Send
Can't i declare that the particular exception is thrown?
+Pie Number of slices to send: Send
Have you tried it?

Try adding SQLException to the throws clause of a doGet or doPost method and see what the compiler says.
+Pie Number of slices to send: Send
A doPost can declare exceptions like ServletException and IOException alone. Otherwise, it won't be compiled.

In this case, which exceptions are the sub classes of ServletException??
+Pie Number of slices to send: Send
Also,

If our code is about to throw any checked exception, we should catch it and throw ServletException, so that the code is compilable and also the container can redirect the custom page defined in DD.

Am i right?
+Pie Number of slices to send: Send
 

Originally posted by Shiaber Shaam:
Also,

If our code is about to throw any checked exception, we should catch it and throw ServletException, so that the code is compilable and also the container can redirect the custom page defined in DD.

Am i right?



Yes, if you don't want to deal with it in your code, you can re-throw it as a servletException.
+Pie Number of slices to send: Send
 

Originally posted by Shiaber Shaam:
A doPost can declare exceptions like ServletException and IOException alone. Otherwise, it won't be compiled.

In this case, which exceptions are the sub classes of ServletException??



The only one provided by the spec is UnavailableException.
You can always subclass it with your own exceptions if you like.
Or... as mentioned earlier, you can simple re-throw it as a ServletException.
Get me the mayor's office! I need to tell him about this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1689 times.
Similar Threads
servlet Exceptions
what's wrong when i create new jdbc provider?
Why not to include Exception
ServletException from destroy( )
ServletException()
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 19, 2024 00:49:51.