File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Q: Appropriate Exception Handling in a servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "Q: Appropriate Exception Handling in a servlet" Watch "Q: Appropriate Exception Handling in a servlet" New topic
Author

Q: Appropriate Exception Handling in a servlet

Anand Wadhwani
Ranch Hand

Joined: Mar 21, 2005
Posts: 151
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 ]

SCWCD 1.4<br />---------------------<br />Ability is what you're capable of. <br />Motivation determines what you do. <br />Attitude determines how well you do it.<br />---------------------
Kedar Dravid
Ranch Hand

Joined: May 28, 2004
Posts: 333
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

Joined: Jul 13, 2004
Posts: 150
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

Joined: Mar 21, 2005
Posts: 151
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:
[HR]
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

Joined: Jul 13, 2004
Posts: 150
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

Joined: Mar 21, 2005
Posts: 151
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

Joined: Jun 13, 2005
Posts: 95
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!!!


SCJP 1.4 : 91%
SCWCD 1.4 : 95%
SCBCD 1.3 : 95%
SCJP 6 Upgrade : 95%
Next SCBCD 1.5
Man is not finished when he is defeated. He is finished when he quits.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Q: Appropriate Exception Handling in a servlet