Sunday, 19 February 2017

Exception Handling in Websphere Commerce

There are two types of exceptions:
1. System Exception

This exception is thrown if a runtime exception or any WebSphere configuration error occurs. Examples of this exception are Remote Exception, Create Exception or other EJB exceptions. When this exception is thrown, the solution controller retries the command if the command is retriable and the exception was caused by either a database dead lock or database rollback. 
e.g.:
try {
      // your business logic
}
catch(FinderException e) {
     throw new ECSystemException (ECMessage._ERR_FINDER_EXCEPTION,
      className, methodName, new Object [] {e.toString()}, e);
}

 2. Application Exception 

This  exception is thrown when  the error is related to user input. When the user enters invalid parameter, this exception is thrown. When this exception is thrown, the solution controller does not retry the command, even if the command is specified as retriable. 

// catch some new type of application exception
catch(//your new exception)
{
       throw new ECApplicationException (MyMessages._ERR_CUSTOMER_INVALID,
       className, methodName, errorTaskName, someNVPs);
}

In order to throw one of these exceptions, the following information must be specified:

Error view name
The view that will be used to display the error. For Web requests, the Web controller looks up this name in the Struts configuration files.
ECMessage object
ECMessage defines the static information related to the reason for the exception. This value corresponds to the message text contained within a properties file.
ECParameter
ECParameter returns the errors associated with the application. The exception can indicate multiple errors since each part of an application exception represent an error.
Error parameters
These name-value pairs are used to substitute information into the error message. For example, a message may contain a parameter to hold the name of the method which threw the exception. This parameter is set when the exception is thrown, then when the error message is logged, the log file contains the actual method name.
Error data
These are optional attributes that can be made available to the JSP page through the error data bean.

Samples are given below.

Throwing an application exception from a command without any  parameters

throw new ECApplicationException(EXTMessage._ERR_RESTRICTED_PROD,CLASSNAME,methodName,"TestView", property);

No comments:

Post a Comment