Win a copy of The Business Blockchain this week in the Cloud forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

DD

 
Amandeep Singh
Ranch Hand
Posts: 850
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The deployment descriptor of a web application contains the following:

<error-page>
<exception-type>com.enthuware.NoLicenseException</exception-type>
<location>/nolicense.html</location>
</error-page>

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

1)This error page mapping will never be used because a servlet can only throw IOException or ServletException.

2)If a servlet throws NoLicenseException, nolicense.html will be shown to the user.

3)As such, this error page mapping will only be used if a servlet wraps this exception into a ServletException and then throws the ServletException.

4)This error page mapping will only be used if NoLicenseException is made to extend from RuntimeException.

The correct answer given is 3. But according to me the correct answer should be 2.

If we look at answer 3, it is wrapping exception into a ServletException but throwing again Servlet Exception so here in this answer there is mention about NoLicenseException.

This question is from Enthuware. Please advice.
 
Paul Anilprem
Enthuware Software Support
Ranch Hand
Posts: 3817
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
There is a detailed explanation provided with the answer -

The throws clause of service() method (or any doXXX() methods ) contains only IOException and ServletException. So a servlet cannot directly throw any other checked exception which is not a subclass of either of these two exceptions. To throw a business exception such as NoLicenseException, it must wrap the exception into a ServletException. For example, let's say your servlet wants to throw NoLicenseException, which extends from java.lang.Exception. It should be handled as follows:

//in doXXX() of MyServlet
try
{
...lot of code
if(someCondition) throw new NoLicenseException();
...lot of code

}
catch(NoLicenseException ne)
{
throw new ServletException(ne) // wrap the exception inside ServletException
}


The error mapping in DD should be:-

<exception-type>com.enthuware.NoLicenseException</exception-type>
<location>/errorPage.jsp</location>

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic