One of the most important architectural decisions made by Java developers is how to use the Java exception model. Java Exception Handling has become one of the most discussed topics in the community. Some people think that the checked exceptions in the Java language are a failed attempt. This article assumes that the error does not lie in the Java model, but because the Java library designer does not recognize the two basic causes of method failure. This article advocates thinking about the nature of abnormal situations and describes the design patterns that help users design. Finally, this article discusses the situation of exception handling as a cross-cutting concern (crosscutting concern) in the aspect oriented programming model. If used properly, Java exceptions will be of great benefit to program developers. This article will help readers correctly use Java exceptions.
Why exceptions are important?
Exception Handling in Java applications tells users the architectural strength of building applications. Architecture refers to the decisions made at all levels of the application and always follow. One of the most important decisions is the way in which classes, subsystems, or layers of applications communicate with each other. The method can pass another result for the operation through Java exceptions, so the application architecture deserves our attention.
Determine the skill level of the Java architect and whether the development team is well-trained. The better method is to view the exception handling code in the application. First, you need to observe how much code is used to capture exceptions, record exceptions, and determine the events and abnormal conversions. Concise, compact, and organized Exception Handling indicate that the team has a consistent method for using Java exceptions. When the number of Exception Handling Code exceeds that of other code, it can be determined that the communication between team members has been broken (or such communication does not exist from the beginning ), everyone uses their own methods to handle exceptions.
The result of temporary exception handling is very predictable. If you ask why a team member discards, captures, or ignores an exception at a specific point in the Code, the answer is generally: "I don't know how to do this ." If they ask what happens when a code exception occurs, they usually frown and say, "I don't know. We have never tested it ."
To determine whether a Java component effectively exploits a Java exception, you can view the code of its client. If the client code contains a large number of computing operation failure times, causes, and processing method logic, almost all of the reasons are due to the component error report design. Defective reports generate a large number of log and forget code on the client without any use. The worst is distorted logical paths, nested try/catch/finally blocks, and other chaotic applications that cause vulnerabilities and management failures.
Exceptions in post-event processing (or not at all) are the main cause of software project confusion and delay. Exception Handling is related to various aspects of software design. Creating an architecture Convention for exceptions should be one of the first decisions to be made in the project. The rational use of the Java exception model will be of great help to keep the application simple, maintainability and correctness.
Exception challenge criteria
Correct use of the Java exception model has become a topic of much discussion. Java is not the first language that supports exception semantics. However, the Java compiler can use rules to control how to declare and handle specific exceptions. Many people think that exception checking during compilation is helpful for Accurate Software Design and can be well coordinated with other language features. Figure 1 shows the hierarchy of Java exceptions.
Generally, the Java compiler throws an exception based on the java. Lang. throwable mandatory method, including the exception of the "throw" clause in its declaration. Similarly, the compiler verifies whether the method's client captures the declared exception type or specifies whether to throw the exception type. These simple rules have a profound impact on Java developers around the world.
The compiler relaxed the exception check behavior for the two branches of the throwable inheritance tree. Subclasses of Java. Lang. Error and Java. Lang. runtimeexception are exempt from the compile-time check. In both cases, software designers are generally more interested in runtime exceptions. The term "unchecked exception" is usually used to distinguish all other "Checked exceptions" (checked exception)
Figure 1 Java exception hierarchy
I think exceptions that have been checked will be welcomed by those who pay attention to the strong types of Java language. After all, compiler constraints on data types encourage strict coding and accurate thinking. The compile-time type check helps prevent unexpected events that are hard to cope with during the runtime. During compilation, the exception check will have the same effect, reminding developers that the method may have other potential results to be processed.
At an early stage, we recommend that you use checked exceptions wherever possible to make full use of the compiler to produce error-free software. The Java API library designers are clear in favor of checked exception rules and are widely used to simulate any unexpected events in the library method. In the j2se 5.1 API specification, the checked exception type is still more than the unchecked exception type, and the proportion exceeds the second-to-second ratio.
For programmers, the vast majority of common methods in the Java class library seem to have declared checked exceptions for every possible failure. For example, the Java. Io package is highly dependent on ioexception checked. At least 63 Java library packages publish this exception either directly or indirectly through one of its dozens of subclasses.
I/O failure is very serious, but it is also rare. In addition, the program code is usually unable to recover from failure. Java programmers find that they must provide ioexception and events similar to the events that may occur during simple Java library method calls. Capturing these exceptions will only disrupt the original concise code, because capturing blocks does not improve this situation. Without capturing these exceptions, it may be worse because the compiler requires that these exceptions be added to the exception list thrown by the method. This exposes implementation details, which are naturally hidden by excellent object-oriented design.
This failure causes many exceptions that seriously violate the Java programming mode. Today's programmers are often warned to guard against these situations. Similarly, a large number of correct and wrong suggestions are generated when creating a workspace.
Some Java days began to question whether Java has checked whether the exception model is a failed attempt. It can be determined that a problem occurs somewhere, but this is irrelevant to the exception check in Java. The cause of the failure is the way Java API designers think, that is, they think that most failures are the same and can be communicated with exceptions of the same type.
Errors and incidents
The checkingaccount class in the hypothetical financial application software. The checkingaccount belongs to the customer, maintains the current balance, accepts deposits, accepts the termination payment order based on the check, and processes the checked. The checkingaccount object must coordinate concurrent thread access because each thread can change its status. The processcheck () method of checkingaccount uses the check object as a parameter, and the check amount is normally deducted from the account balance. However, the Check settlement client that calls processcheck () must be prepared for two types of incidents. First, the checkingaccount may have a termination payment command for the check registration. Second, the account may not have enough funds to pay the check amount.
Therefore, the processcheck () method uses three possible methods to respond to its callers. The normal response is that the check is processed, and the result declared in the method signature is returned to the calling service. The response to these two types of incidents represents a very real situation in the financial sector, and they need to communicate with the check and settlement client. All three processcheck () responses are carefully designed to mimic typical checked account behaviors.
In Java, two types of exceptions are defined, namely stoppaymentexception and insufficientfundsexception. It is incorrect for the client to ignore these two exceptions because these two exceptions will be thrown during normal operation of the application. These two exceptions help express all the actions of the method, which is as important as the method signature.
The client can easily handle these two types of exceptions. If you terminate the payment of a check, the client can obtain the check for special processing. If there is not enough funds, in order to pay this check, the client will transfer funds from the customer's savings account and try again.
These incidents are foreseeable as a natural result of using the checkingaccount API. They do not indicate a failure in the software or execution environment. Compare these exception conditions with the actual failure. The actual failure is caused by the internal implementation details of the checkingaccount class.
Assume that the checkingaccount maintains a persistent state in the database and uses the jdbc api for access. In this API, almost every database access method may fail, but the cause is irrelevant to the checkingaccount implementation. For example, someone may forget to open the database server, accidentally unplug the network cable, or change the password for accessing the database.
JDBC reports all possible errors by means of a separate sqlexception with checked exceptions. Most errors are related to database configuration, connection, and hardware devices. The processcheck () method cannot handle these exception conditions in a meaningful way. Unfortunately, processcheck () knows at least how it is implemented. Calling upstream methods in the stack is less likely to handle these problems.