aspose file tools*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Exception Handling Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "Exception Handling" Watch "Exception Handling" New topic
Author

Exception Handling

Reshma Shanbhag
Ranch Hand

Joined: Sep 17, 2002
Posts: 203
Hi,

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.

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

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

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

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

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

Answers are 1.) and 3.)


what i am confused is about the option 3.) as any exception other than the runtime exception thrown from servlet is wrapped as ServletException then how can you map the Business Exception to error page mapping in web.xml?

any help on this is greatly appriciated.

Thanks,
Reshma


SCJP 1.4, SCWCD 1.4
Gaurav Gambhir
Ranch Hand

Joined: Mar 04, 2006
Posts: 254
Yes we cannot throw any exception other than ServletException and IOException or the runtime exception or their subclass, so to throw the other exceptions like SQLException or custom non runtime exception, wrap them in ServletException and then rethrow the ServletException.The wrapped exception is called root exception.
Consider
try{
Statement stmt=connection.createStatement();
ResultSet rs=stmt.executeQuery(�select * from item�);
while(rs.next()){
items.addElement(rs.getString(�name�));
}
}catch(SQLException e){
throw new ServletException(�wrapped SQLException�,e);
}

Container use ServletException.getRootCause() method to extract the wrapped exception
Then container will look through <error-page> element and try to find an error-page that is mapped to SQLException exception type.
<web-app>
<servlet>
<servlet-name>Itemservlet</servlet-name>
<servlet-class>Itemservlet</servlet-class>
</servlet>
<error-page>
<error-code>403</error-code>
<location>/errorpage/security.html</location>
</error-page>
<error-page>
<exception-type>java.sql.SQLException</exception-type>
<location>/errorpages/sqlerror.html</location>
</error-page>
</web-app>
Reshma Shanbhag
Ranch Hand

Joined: Sep 17, 2002
Posts: 203
ok that makes it a little clear .. but if the deployment Descriptor had a javax.servlet.ServletException mapping with error-page .. would the container still go for ServletException.getRootCause() ?



<web-app>
<servlet>
<servlet-name>Itemservlet</servlet-name>
<servlet-class>Itemservlet</servlet-class>
</servlet>
<error-page>
<error-code>403</error-code>
<location>/errorpage/security.html</location>
</error-page>
<error-page>
<exception-type>javax.servlet.ServletException</exception-type>
<location>/errorpages/sqlerror.html</location>
</error-page>
<error-page>
<exception-type>java.sql.SQLException</exception-type>
<location>/errorpages/sqlerror.html</location>
</error-page>
</web-app>

Thanks for you help ..



Regards,
Reshma
Nikhil Jain
Ranch Hand

Joined: May 15, 2005
Posts: 385
Hi Reshma, I am actually could not understand your question. How do you define a Business Exception / Servlet Exception? I did not come across such questions till now. Thanks.


SCJP 1.4, SCWCD 1.4, SCBCD 1.5
Gaurav Gambhir
Ranch Hand

Joined: Mar 04, 2006
Posts: 254
If you have ServletException defined in DD then it will show the page mapped to ServletException and will not go to
<exception-type>java.sql.SQLException</exception-type>
Reshma Shanbhag
Ranch Hand

Joined: Sep 17, 2002
Posts: 203
Thanks for clarifying Gaurav.

Shashank, i came across this question on javaprepare.com web site. The business exception mentioned here means Custom Exception that the application can throw to alert some weird situation in application.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Exception Handling