Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

ServletException  RSS feed

 
Shiaber Shaam
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could you please explain the significance of ServletException class and the different ways of handling exception/error in a Servlets?


Thanks a lot...
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Shiaber Shaam
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, if you want the container to handle your NullPointerException you would re-throw it as a ServletException.
 
Shiaber Shaam
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What does it mean like "the container will handle the exception"?

Could you please explain with a scenario?

Thanks a lot!!!
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Shiaber Shaam
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Even if we don't throw servletexception, we will get that default stacktrace. so, what is the point in throwing it?

Please clarify...
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Shiaber Shaam
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can't i declare that the particular exception is thrown?
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you tried it?

Try adding SQLException to the throws clause of a doGet or doPost method and see what the compiler says.
 
Shiaber Shaam
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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??
 
Shiaber Shaam
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!