aspose file tools*
The moose likes Servlets and the fly likes Exception Handling...For Marty Hall..and All 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 » Java » Servlets
Bookmark "Exception Handling...For Marty Hall..and All" Watch "Exception Handling...For Marty Hall..and All" New topic
Author

Exception Handling...For Marty Hall..and All

Mishra Anshu
Ranch Hand

Joined: Sep 16, 2003
Posts: 224
Instead of handling exceptions in our code and then calling sendError(), we can define error pages in the deployment descriptor and then the rest of the headache is handled by the container after making the required mapping for the exception classes and the corresponding error codes. Then, why should one ever use the first approach to handle the exception? :roll:


"Ignorance is bliss"
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8919

The sendError accepts a error code which means somethiing. For Example 404 means Page Not Found whereas an excpetion is an abnormal condition which occurs during the execution of a program.


Groovy
Marty Hall
Author
Ranch Hand

Joined: Jan 02, 2003
Posts: 111
Instead of handling exceptions in our code and then calling sendError(), we can define error pages in the deployment descriptor and then the rest of the headache is handled by the container after making the required mapping for the exception classes and the corresponding error codes. Then, why should one ever use the first approach to handle the exception?

You use reponse.sendError to send a particular HTTP status code to the client. In particular, you can send a 404 status code along with a Web page explaining why the page could not be found (e.g., that the request failed to provide the information needed to do a redirect, or some such).
You can indeed use error-page and error-code in web.xml to create Web-application-wide 404 pages. This is most definitely a good idea, but is different than using response.sendError for a page-specific 404 error page.
However, IMHO the idea of using error-page and exception-type to provide error pages for uncaught Java exceptions is not a very good one. There are two reasons why I am very dubious about this idea except as a warning for developers (eg for NullPointException) during initial development:
  • The error page is too general: it cannot fix the specific problems. Instead, plan ahead for missing and malformed data, use try/catch blocks and never have uncaught exceptions! For example, always check that an HTTP request header is non-null before calling a method on the value, and always catch NumberFormatException when doing Integer.parseInt on user-supplied values.
  • If the error occurs far down in the page, the response might already have been committed, and it is too late to transfer to an error page.


  • Cheers-
    - Marty


    Java training and consulting
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Exception Handling...For Marty Hall..and All