Exception Handling in Web Applications

Error Handling is an important part of any implementation. An application without Error Handling is like a patient with all door closed for diagnosis. It is a perfect time killer for developers to debug the application issues without proper error handling. Hence it is an important topic to discuss. However here we are concentrating more on error handling in web applications (using JSP, Servlets).

Motive of error handling is to capture any error generated in the application, log it to any logging system and propagate it to right point so that it can be directed to the user with all useful information. So the basic flow of error handling should be
  • Apply try-catch block with right exception type in catch around the code which may generate the error
  • Log the error using any logging mechanism. One of the popular logging system is log4j using commons logging. Ensure to use right logging level i.e. debug, info, warn, error etc
  • Apply business logic whatever is required to handle the erroneous case as per application requirement
  • If error is not manageable and condition should be propagated to user, throw the appropriate error further after wrapping the original error. Make sure that new exception should carry all user centric details which can help them to understand the cause of error, and developer centric information which can help them to debug the issue. 
  • As a general mechanism, error codes can be set to exception which can describes the reason of problem at Application and UI level. These error codes can also be used to show the localized messages on UI
  • Application Framework should provide the facility to handle any exception originated from the application and redirect it to a designated UI which can show the error details to user with all the details
  • The UI can also have the mechanism which can facilitate the user to log the issue directly in Issue Tracking system or to send email to the support staff with all the details

Catching the exception, logging it and throwing it further is a general flow. However point is how to show the exceptions to user. A general mechanism to do this is have a exception handler block at a point which is the gateway for every request to application. It will help to catch every error generated from any request/interaction with the system. Once the error is captured, then it can be redirected to desired UI element with all the information. UI element for error should be designed in a way so that it can render the error information in user friendly way. One part of the UI can show the error information which is relevant to user. Other part of UI can show the detailed information, which if sent to development team, can help them to understand the issue.

If we take example of a Servlet based web application, the right gateway is a 'Servlet Filter'. We can install a servlet filter, which in general can be used for many generic work like for enforcing the security parameters and logging various important information. We can catch the exception coming from filter chain invocation. It will give us all the errors raised from application also. Now in catch block, you can check the type of exceptions raised by filter chain. Here we can distinguish various kind of exceptions like Servlet System exception or Application Exceptions. Based on the type of exceptions, now we can set the proper error code to Servlet Response. Please refer to various kind of HTTP Error code for setting the right error code depending upon the error you faces. Further, base exception can be wrapped in servlet exception and can throw further. Servlet engine will catch the error and try to find the error page mapping for given error code. If a mapping is found, it will load the page and will forward the request to this page. On this page, we can access the error by using the 'exception' parameter, however make sure, that this JSP page should be set as error page. Now access the error and print all the details in desired format.

You can provide various features on this page to make it more and more user friendly. A few options are:
  • Get the error code and show the user specific message
  • Show all the messages of error and nested errors on the screen.
  • Provide a button on screen, like mail error details, which if pressed can send all error details to a designated email id for support.
  • Provide a button to log the support call, which if pressed, can log the issue in desired issue tracking system.
  • And so on.. 
Keep in mind, a beautifully designed error page with all relevant information for user and development team can help in overall issue tracking process a lot without being much annoying to user. Software may not be 100% bug free, but software can be good enough to provide a decent error page for user with all relevant details which can make them comfortable to understand the issue. This will encourage them to log the issue with all details, and in turn, will shorten the overall issue life cycle.

People who read this post also read :


Mohit Gupta said...

Here is a scenario specific to JSP/Servlets and Tomcat. If you are not throwing a ServletException from filter, and accidently any runtime exception has been thrown from filter, Tomcat engine treat this error as Error 505, means an exception which is not handled and the application gets crashed. So if you are implementing the filters and doing the exception handling, be careful to catch every exception and transform these to ServletException with proper error code and also provides the error mapping for all relevant HTTP Error code in web.xml. Otherwise sometime Tomcat engine does not show the error even in logs and your application stops working. This case is hard to debug.

Post a Comment