Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Q: Appropriate Exception Handling in a servlet

 
Anand Wadhwani
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Question: Which of the following is a sensible way of sending an error page to the client in case of a business exception that extends from java.lang.Exception?

Select 2 correct options

a Catch the exception and use RequestDispatcher to forward the request to the error page.

b Don't catch the exception and define the 'exception to error-page' mapping in web.xml

c Catch the exception, wrap it into ServletException and define the 'business exception to error-page' mapping in web.xml

d Catch the exception, wrap it into ServletException, and define the 'ServletException to error-page' mapping in web.xml

e Don't do anything, the servlet container will automatically send a default error page.
--------------------------------

In my opinion, the answer should be a,d but correct answers are a and c.

Please confirm whether I am wrong or there is bug in mock exam result?

Thanks!
[ June 19, 2005: Message edited by: Anand Wadhwani ]
 
Kedar Dravid
Ranch Hand
Posts: 333
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since the question talks about a business exception, I think the answer in the mock exam is correct.
In this case, the exception-type tag for the error-page tag in the DD would likely have a value of java.lang.Exception, which can always refer to any business exception.
[ June 19, 2005: Message edited by: Kedar Dravid ]
 
PNS Subramanian
Ranch Hand
Posts: 150
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I get the business exception error page only when i map 'ServletException to error-page' - which means i get a,d as the answers to the above. When i try mapping 'business exception to error-page' i do not get the intended/mapped error page.

Isn't this the way, it is suppose to behave since we wrap the business exception into ServletException ?

Env: Tomcat 5.0.30/IE 6.0
 
Anand Wadhwani
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's all working. And now it's making me a lot sensible also. Here is what I have understood after spending quite some time on 'declarative' exception handling.

Container checks if a servlet throws ServletException, if so, it gets the root cause of that exception by invoking getRootCause() method on it, then it checks if developer has defined any handling for this root cause exception in DD, if so invokes the error page associated with that root cause exception, otherwise throws 500.

We can not throw any non-(IOException/ServletException) from servlet because of inheritance rule. A derived class cannot throw additional non-runtime exceptions than it's super class method does. Hence we are bound to wrap our CustomException object into ServletException object before throwing it.

Here is my code that worked perfactly.

===========================================================
Now my next question on the same line is why can't we extend our CustomException from RuntimeException and throw it from servlet without having to declare in throws clause and define a CustomException error-page mapping in DD. Here is another question from a mock exam:


The deployment descriptor of a web application contains the following:

<error-page>
<exception-type>com.exceptions.CustomException</exception-type>
<location>/customerror.html</location>
</error-page>

Assuming that com.exceptions.CustomException extends from java.lang.Exception, which of the following statements are correct?

Select 1 correct option.
a This error page mapping will never be used because a servlet can only throw IOException or ServletException.
b If a servlet throws CustomException, customerror.html will be shown to the user.
c As such, this error page mapping will only be used if a servlet wraps this exception into a ServletException and then throws the ServletException.
d This error page mapping will only be used if CustomException is made to extend from RuntimeException.
[/HR]

The answer to above question is c but in my understanding c and d both should be correct.

Any idea please?
[ June 19, 2005: Message edited by: Anand Wadhwani ]
 
PNS Subramanian
Ranch Hand
Posts: 150
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How is option c) mentioned above correct ? For this option to work dont we need to have an explicit mapping such as

<error-page>
<exception-type>javax.servlet.ServletException</exception-type>
<location>/customError.html</location>
</error-page>


When i tried executing the servlet, that wraps a customException (extending RuntimeException) as a ServletException, the following is what i got


description The server encountered an internal error () that prevented it from fulfilling this request.

exception

javax.servlet.ServletException
com.servlet.TestServlet.doGet(TestServlet.java:44)
com.servlet.TestServlet.doPost(TestServlet.java:30)
javax.servlet.http.HttpServlet.service(HttpServlet.java:709)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)


 
Anand Wadhwani
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please refer my example in my post above. This worked very well on tomcat. Container automatically checks the root exception and tries to find the mapping for that exception.
 
AmitKumar Jain
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is with reference to your line:
"Now my next question on the same line is why can't we extend our CustomException from RuntimeException and throw it from servlet without having to declare in throws clause and define a CustomException error-page mapping in DD. "

Now my question is can we create our own custom Runtime Exception???
I tried to search on net but didn't find any answers!!!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic