Build a better exception handling framework

xiaoxiao2021-03-06  19

Enterprise applications often focus on abnormalities in terms of exceptions, which will cause excessive dependence on low-level abnormalities such as java.rmi.RemoteException and javax.naming.namingexception. In this column article in EJB best practices, Brett McLaughlin explains why it will bring us great help to us, and show you two simple technologies, they will help you correctly. Build a more robust and useful exception handling framework.

In the previous skills in this series, exception handling is not within the core range of discussion. However, you may find a little, that is, we have always avoided the low level of abnormalities from the web layer. We provide an exception such as ApplicationException and InvalidDataException, without allowing web layers to process an exception such as java.rmi.RemoteException or javax.naming.namingexception.

Remote and naming exceptions are system-level exceptions, while applications and illegal data exceptions are business-level exceptions because they submit more applicable business information. When deciding which type of exception is thrown, you should always consider the layers that will be handled will be handled. The web layer is usually driven by the end user of the executive business task, so it is best to use it to handle the business level exception. However, in the EJB layer, you are performing system-level tasks, such as using JNDI or databases. Although these tasks will eventually be merged into business logic, it is preferred to represent them with system-level exceptions such as RemoteException.

In theory, you can let all Web layer methods expect to process and respond to a single application exception, just as we do in some of the previous examples. But this method is not suitable for long run. Let your delegate method throw more specific exceptions, this is a much better abnormal handling program, fundamentally, this is more useful for receiving clients. In this skill article, we will discuss two technologies, which will help you create more information, more specific, and don't generate a lot of unnecessary code.

Nested exception

When designing a reliable anomaly treatment plan, the first thing to consider is to abnormally abnormally. These core Java exceptions typically report errors, JNDI, or RMI issues in network traffic, or other technical issues in the application. RemoteException, EJBEXCEPTION and NAMINGEXCEPTION are common examples of low anomalies in enterprise Java programming.

These exceptions have no significiance, especially easier to confused by clients in the web layer. If the client calls purchase () and receives Namingexception, then it will fight for this exception. At the same time, the application code may need to access these exceptions, so it cannot be easily abandoned or ignored.

The answer is to provide a more useful exception, which also contains low-level exceptions. Listing 1 demonstrates a simple ApplicationException for this design:

Listing 1. Nested exception package com.ibm;

import java.io.PrintStream; import java.io.PrintWriter; public class ApplicationException extends Exception {/ ** A wrapped Throwable * / protected Throwable cause; public ApplicationException () {super ( "Error occurred in application.");} public ApplicationException (String message) {super (message);} public ApplicationException (String message, Throwable cause) {super (message); this.cause = cause;}. // Created to match the JDK 1.4 Throwable method public Throwable initCause (Throwable cause) {this.cause = cause; return cause;} public String getMessage () {// Get this exception's message String msg = super.getMessage ();. Throwable parent = this; Throwable child; // Look for nested exceptions. While ((Child = getNestedException (PARENT))! = null) {// Get the child's message. String msg2 = child.getMessage (); // if We found a message for the child Exception, // We append it. If (msg2! = Null) {if (msg! = Null) {msg = ":" msg2;} else {msg = msg2;}} // Any nested ApplicationException will append its own // children, so we need to break out of here if (child instanceof ApplicationException) {break;.} parent = child;

.} // Return the completed message return msg;.} Public void printStackTrace () {// Print the stack trace for this exception super.printStackTrace (); Throwable parent = this; Throwable child; // Print the stack trace for each . nested exception while (! (child = getNestedException (parent)) = null) {if (child = null!) {System.err.print ( "Caused by:"); child.printStackTrace (); if (child instanceof ApplicationException ) {break;} parent = child;.}}} public void printStackTrace (PrintStream s) {// Print the stack trace for this exception super.printStackTrace (s); Throwable parent = this; Throwable child; // Print the stack TRACE for Each Nested Exception. While ((Child = Getnest) EdException (PARENT))! = NULL) {if (child! = null) {s.print ("Caused by:"); child.printStackTrace (s); if (child instanceof applicationException) {breaf;} Parent = child; }}} public void printStackTrace (PrintWriter w) {// Print the stack trace for this exception super.printStackTrace (w);. Throwable parent = this; Throwable child; // Print the stack trace for each nested exception while ((. Child = getnestedException (PARENT))! =

NULL) {if (child! = null) {w.print ("caused by:"); child.printStackTrace (w); if (child instanceof applicationException) {Break;} Parent = child;}}} public throwable getcause The code in the list 1 is simple; we have simply put multiple exception "strings" to create a single, nested exception. However, the real benefit is to use this technology as a starting point to create an application-specific anomaly hierarchy. The exception hierarchy will allow the EJB client to receive both business-specific information, without having to write a large amount of extra code.

Abnormal hierarchy

The abnormal hierarchy should start from some very robust and universal anomalies, such as ApplicationException. If you make the top exceptions too specific, then the result is that you will have to restructuring the hierarchy in the future to adapt to certain more general conditions.

So let's assume that your application requires NosuchbookException, InsufficientFundSexception and SystemunavailableException. You don't have to create these three exceptions, allowing them to inherit ApplicationException, and then only provide very few of several must-have constructor to create formatted messages. Listing 2 is an example of such an exception hierarchy:

Listing 2. Abnormal hierarchy package com.ibm.library;

import com.ibm.ApplicationException; ' ". library" public class NoSuchBookException extends ApplicationException {public NoSuchBookException (String bookName, String libraryName) {super ( "The book'" bookName " 'was not found in the" libraryName ' }}

When you need to write a lot of dedicated anomalies, the abnormal hierarchy is greatly simplified. For an exception, add one or two constructors for each anomaly class, which takes a few minutes. You often need to give these more specific exceptions (these exceptions are also subclasses of the primary application exception) to provide a more specific exception. For example, you may need InvalidTitleException and BackordEdException to inherit NosuchbookException.

Enterprise applications usually do not pay attention to exception processing during construction. Although it is easy to rely on low-level abnormalities (such as RemoteException and Namingexception) (sometimes very attractive), if you create a reliable, well-thought-proximity model, you will have a lot of effort on your application. Create a nested, hierarchical exception framework will improve the readability of the code and its availability.

About author

转载请注明原文地址:https://www.9cbs.com/read-46251.html

New Post(0)