Portlet Factory, Version 6.1.2


 

Catch and handle an error in a model

There are several ways you can handle errors and exceptions generated by an application.

  • You can use the Error Handler builder to catch and handle exceptions in a model.

  • You can use the Java language try and catch error handling mechanisms to handle exceptions generated by methods and linked java objects included in your application.

 

Handle an error in Java code

You can handle errors in LJO methods using the standard try and catch mechanism. In the catch code, you extract the error information that the IBM® WebSphere Portlet Factory servlet stores in the request. After you process the error information, you can perform one or more actions to allow processing to proceed or you can simply return an error page.

For example, if the method tries to open a file and fails because the file does not exist, the catch actions can store the error in a variable to be used to supply an error message and display a page in which the user can name another file to open. Unhandled, a file not found error results in the application stopping and the display of the default unhandled error page. See Examples.

 

Examples

The following code sample shows a method that tries to open a file, and, if it is not found, stores the error message in a variable and displays a page:
catch (IOException ioe) {
   HttpServletRequest request = webAppAccess.getHttpServletRequest();
   Throwable ex = (Throwable)request.getAttribute("bowstreet.errorhandler.Exception");
   String actionName = (String)request.getAttribute("bowstreet.errorhandler.ActionName");
   String errorMessage = ex.toString() + " occurred in: " + actionName;
   //set a the value of a variable to the error message
   webAppAccess.getVariables().setString("ErrorVariable", errorMessage);
   //display a page prompting for new information
   webAppAccess.processPage("ErrorPage");
}
You can write the above error to the log with the logError() method on the webAppAccess object as shown below:
//log the error
   webAppAccess.logError(actionName, ex);

The logError() method takes a string and a throwable object. The actionName variable is the value of the bowstreet.errorhandler.ActionName attribute in the request and ex is the value of the bowstreet.errorhandler.Exception attribute in the request.

Parent topic: Advanced error handling techniques


Library | Support |