Displaying Exceptions Thrown or Caught in Managed Beans
- by Frank Nimphius
Just came a cross a sample written by Steve Muench, which somewhere deep in its implementation details uses the following code to route exceptions to the ADF binding layer to be handled by the ADF model error handler (which can be customized by overriding the DCErrorHandlerImpl class and configuring the custom class in DataBindings.cpx file)
To route an exception to the ADFm error handler, Steve used the following code
((DCBindingContainer)BindingContext.getCurrent().getCurrentBindingsEntry()).reportException(ex);
The same code however can be used in managed beans as well to enforce consistent error handling in ADF. As an example, lets assume a managed bean method hits an exception. To simulate this, let's use the following code:
public void onToolBarButtonAction(ActionEvent actionEvent) {
throw new JboException("Just to tease you !!!!!");
}
The exception shows at runtime as displayed in the following image:
Assuming a try-catch block is used to intercept the exception caused by a managed bean action, you can route the error message display to the ADF model error handler. Again, let's simulate the code that would need to go into a try-catch block
public void onToolBarButtonAction(ActionEvent actionEvent) {
JboException ex = new JboException("Just to tease you !!!!!");
BindingContext bctx = BindingContext.getCurrent();
((DCBindingContainer)bctx.getCurrentBindingsEntry()).reportException(ex);
}
The error now displays as shown in the image below
As you can see, the error is now handled by the ADFm Error handler, which - as mentioned before - could be a custom error handler. Using the ADF model error handling for displaying exceptions thrown in managed beans require the current ADF Faces page to have an associated PageDef file (which is the case if the page or view contains ADF bound components).
Note that to invoke methods exposed on the business service it is recommended to always work through the binding layer (method binding) so that in case of an error the ADF model error handler is automatically used.