J2EE application Exception Handling framework
1. Checked and unchecked exceptions
Fatal error, unchecked error, third-party package error wrapped as unchecked exception.
Abnormal business logic flow. The security exception is packaged as checked exception. Business exception exists in J2EE applications,
DB exception, security exception, confirmation exception
2. An exception policy is thrown.
2.1 centralized control and processing using the front-end controller of J2EE applications, developers can implement specific
During business logic, exceptions cannot be captured.
2.2 throws a base class exception in the method signature. In this way, the front-end controller only needs to capture exceptions of this base class.
You can define your own derived exception classes.
3. Process unchecked exceptions on the web layer
All unchecked exceptions should be processed at the web layer. You can configure
Shows the custom error page.
4. encapsulate all exceptions of a third-party package into an unchecked exception
5. Checked exception from database operations, such as employee not found and order not found.
Like a recordnotfoundexception. As for this recordnotfoundexception, it is because of either the employee or order.
As a context, the error code is composed of context and thrown to the front-end controller.
6. handle errors in the front-end Controller
Some codes of the basic class Exception Handling (Application Template Method Design Mode template method) and subclass (Business implementation) are as follows:
Take struts as an example.
Public abstract class baseappdispatchaction
Extends dispatchaction {
...
Protected static threadlocal
Expdisplaydetails = new threadlocal ();
Public actionforward execute (
Actionmapping mapping,
Actionform form,
Httpservletrequest request,
Httpservletresponse response) throws exception {
...
Try {
String actionmethod = request
. Getparameter (mapping. getparameter ());
Finaldestination = dispatchmethod (mapping,
Form, request, response, actionmethod );
} Catch (baseappexception ex ){
Predictiondisplaydto expdto =
(Exceptiondisplaydto) expdisplaydetails
. Get ();
Iexceptionhandler exphandler =
Exceptionhandlerfactory
. Getinstance (). Create ();
Predictiondto exdto = exphandler
. Handleexception (
Expdto. getcontext (), userid, ex );
Actionerrors errors = new actionerrors ();
Errors. Add (actionerrors. global_error,
New actionerror (exdto
. Getmessagecode ()));
Saveerrors (request, errors );
Return Mapping. findforward (expdto
. Getactionforwardname ());
} Catch (throwable ex ){
// Log the throwable
// Throw ex;
} Finally {
Expdisplaydetails. Set (null );
}
The code in the business subclass is as follows:
...
String predictionactionforward =
"Searchadjustmentpage ";
String predictioncontext =
"Divisionaction. searchdivision ";
Predictiondisplaydto expdto =
New exceptiondisplaydto (expactionforward,
Predictioncontext );
Expdisplaydetails. Set (expdto );
...
Divisiondto = divisionbusinessdelegate
. Getdivisionbynum (fromdivisionnum );
...
Predictiondisplaydto is used as a data transfer object in threadlocal to handle exceptions of the parent class.
Use
7. exception handler
The preceding centralized exception Control is handled in the inititon handler, including:
Obtains the Exception Code based on the exception type. The exception code is used to display the exception information. The ing between the exception type and the Exception Code
It can be put in an XML file. The resoucebundle can be used to obtain exception information based on the Exception Code. This solves the problem at the same time.
An international problem with exception information. The XML file for two abnormal configurations is as follows:
<Exception name = "employeeconfirmationexception">
<Messagecode> messagecode. employeeconfirmation </messagecode>
<Confirmationind> true </confirmationind>
<Loggingtype> nologging </loggingtype>
</Exception>
<Exception name = "recordnotfoundexception">
<Messagecode> messagecode. recordnotfound </messagecode>
<Confirmationind> false </confirmationind>
<Contextind> true </contextind>
<Loggingtype> error </loggingtype>
</Exception>
The second exception file considers the context problem we mentioned above. If the exception thrown is
Recordnotfoundexception, and the current context is order. getorder, the error code is
Yes: messagecode. recordnotfound. Order. getorder
To avoid parsing this XML file multiple times, you can implement a predictioninfocache to buffer the content of this XML file when starting an application.
Rong.
The code snippet of the handleexception method in exceptionhandler is as follows:
Public exceptiondto handleexception (string userid,
Baseappexception exp ){
Predictiondto exdto = new predictiondto ();
Predictioninfocache ecache =
Predictioninfocache. getinstance ();
Predictioninfo exinfo = ecache
. Getexceptioninfo (
Predictionhelper. getclassname (exp ));
String loggingtype = NULL;
If (exinfo! = NULL ){
Loggingtype = exinfo. getloggingtype ();
Exdto. setconfirmation (exinfo
. Isconfirmation ());
Exdto. setmessagecode (exinfo
. Getmessagecode ());
}
Filelogger logger = new fileloggerfactory ()
. Create ();
Logger. logexception (exp, loggingtype );