Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

DD

 
Ranch Hand
Posts: 856
  • 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.
 
Enthuware Software Support
Posts: 4453
41
  • 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>

 
Don't get me started about those stupid light bulbs.
    Bookmark Topic Watch Topic
  • New Topic