Monday, December 22, 2014

Custom Exception Handling in ADF - Model Layer Generic Framework Design

I have observed that there are lot of users who asks questions on OTN and have problem in designing the custom generic exception handling framework. So in this and next some of the posts I will be describing how we can lay down a generic way to report custom exceptions to the page. These custom exceptions may be mostly for Business Specific errors for which we need to display a custom user friendly message on the page to the user.

In this post we will be seeing how we can customize the error on Model layer and take error messages from a resource bundle defined in the application.

Lets get started. We will first create a generic framework project where we will add exception handling related classes. We give it a name as FrameworkUtil. Framework util project will be dependent on model project from where custom Exceptions will be thrown in the enterprise applications.

First lets set up FrameworkUtil project - ADF Model Project 

Created an ADF Model Project with below package structure. Added a resource bundle which will be having all the Business Errors which can be thrown from the applications model layer.  These errors will be in key value pairs in Error properties file.

Added two custom error codes and messages. One without any parameter CSS_BAD_EMPNO and another one CSS_EMP_NOT_IN_DEPT which takes two parameters as name of the Employee and Department.

Now we need to design our Exception classes. At this time we need to understand that the root exception which gets thrown by the framework is JboException. Its a runtime exception. JboException throws the default error codes and associated messages defined in the framework. So we will be extending our Root Exception Class from JboException class and will make to read the error codes and messages from our Error Resource Bundle.

Lets create a class. We can name it for now as ProjectException. This extends from JboException and has below source code.

As above, it overloads three constructors. One is for the String code to be looked up in ErrorBundle, second one is for error code as well as array of parameters which will be given to placeholders in message corresponding to Error Code and the third one which is the second one plus Exception object.

The point here is to see that inside all the three overloaded constructors, there is a call to getMessage(errorCodeInRB). If we see the implementation of this method, this actually gets the ErrorBundle and gets the Error Message defined in the Error Bundle corresponding to the Error Code sent as the parameter to the getMessage() method.

Now this Error message will be sent as the parameter to JboException constructor when super is called from ProjectException constructor.

Ok So we are done for this part to read error message from RB for the given code.

Lets create a class named ProjectBusinessException. This class extends the ProjectException. This is the exception which will be thrown from the business services layer.

Thats it. Now we need to test this by throwing the ProjectBusinessException from our Business Components layer.

So lets create a Model and ViewController project for ADF Application. Named them CommonModel and ConsumerViewController projects respectively.

We will add FrameworkUtil project as dependent to CommonModel project so that the exception classes which we created - ProjectException and ProjectBusinessException are available to this project.

So now we need to test by throwing ProjectBusinessException. Lets create a method in AmImpl class and expose it as service interface so that it can accessed by the page(ViewControllerProject).

Created a method testProjectException() in AmImpl class which does nothing but just throws ProjectBusinessException with Error code and optional parameters.

As this method is exposed as client interface, it is available in Data Controls. Just for testing purpose, dragged the method and created a button out of it on a jspx page testException.jspx in ViewController project.

Thats it. Lets run the page and see what happens on clicking the button (calling testProjectException) method of AmImpl class.

So our method testProjectException threw the ProjectBusinessException with ErrorCode CSS_EMP_NOT_IN_DEPT and parameters  new String[]{"Rohan","HR"} . ProjectException reads this error code from ErrorBundle and sets the message with parameters and is returned to the view.

Lets change the error code and see.

Lets again run the page and see the error.

So thats it. This design can be implemented to have exceptions to be thrown as Custom error which will be read from Resource Bundle. This can be very useful for  giving meaningful business errors to the users.

Next post will talk about how to extend DCErrorrHandlerImpl to massage particular exceptions and how to skip certain exceptions so that they are not reported to the client.

Thanks !!


  1. Most Welcome Pandu. Glad you liked it !!

  2. How to get Access to FacesContext in model

  3. I will implement this in my code :) :)
    Thanks Rohan

  4. Hey Rohan, can you please share the link to your next post on handling custom exceptions using DCErrorrHandlerImpl

  5. Hi Rohan, plz share the link to your next post using DCErrorrHandlerImpl. Thanks.

  6. Hey Rohan, your articles on ADF Framework has always been very helpful and have always increase my knowledge and interest towards ADF. As u mentioned in your this article u will share post abt how to extend DCErrorrHandlerImpl. Can u share u the link if u have alread6y posted it. Thanks :)

  7. Rohan, this is a mess of a solution.
    You are accessing FacesContext in your Model layer.
    In North Korea they shoot people for such ideas


  8. Given so much information in it. its very useful .perfect explanation about Dot net framework.Thanks for your valuable information. dot net training in chennai velachery | dot net training institute in velachery

  9. It is really a great work and the way in which u r sharing the knowledge is excellent.Thanks for helping me to understand basic concepts. As a beginner in Dot Net programming your post help me a lot.Thanks for your informative article.. dot net training and placement in chennai | best dot net training in chennai

  10. If you want your ex-girlfriend or ex-boyfriend to come crawling back to you on their knees (even if they're dating somebody else now) you have to watch this video
    right away...

    (VIDEO) Get your ex back with TEXT messages?

  11. nice post! I really like and appreciate your work, thank you for sharing such a useful information about human resource laws and oracle, keep updating the information, hear i prefer some more information about jobs for your career hr jobs in hyderabad .

  12. CIITN is located in Prime location in Noida having best connectivity via all modes of public transport. CIITN offer both weekend and weekdays courses to facilitate Hadoop aspirants. Among all Hadoop Training Institute in Noida , CIITN's Big Data and Hadoop Certification course is designed to prepare you to match all required knowledge for real time job assignment in the Big Data world with top level companies. CIITN puts more focus in project based training and facilitated with Hadoop 2.7 with Cloud Lab—a cloud-based Hadoop environment lab setup for hands-on experience.

    CIITNOIDA is the good choice for Big Data Hadoop Training in NOIDA in the final year. I have also completed my summer training from here. It provides high quality Hadoop training with Live projects. The best thing about CIITNOIDA is its experienced trainers and updated course content. They even provide you placement guidance and have their own development cell. You can attend their free demo class and then decide.

    Hadoop Training in Noida
    Big Data Hadoop Training in Noida