In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "what are the misunderstandings of Java exception handling". In daily operation, I believe that many people have doubts about the misunderstanding of Java exception handling. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "what are the misunderstandings of Java exception handling?" Next, please follow the editor to study!
Misunderstanding 1. Abnormal choice
Figure 1. Anomaly classification
Figure 1 depicts the structure of anomalies. In fact, we all know that anomalies can be divided into detected anomalies and undetected anomalies, but the application of these two anomalies is confused in practice. Because non-detection exceptions are easy to use, many developers think that detecting exceptions is useless. In fact, abnormal application scenarios can be summarized as follows:
First, the calling code cannot continue execution and needs to be terminated immediately. There are too many possibilities for this, such as failure to connect to the server, incorrect parameters, and so on. These times are suitable for undetected exceptions, there is no need for explicit capture and handling of the calling code, and the code is simple and clear.
Second, the calling code needs further processing and recovery. If SQLException is defined as a non-detection exception, so when operating data, developers take it for granted that SQLException does not need to explicitly capture and handle the calling code, which will lead to serious cases such as Connection does not close, Transaction does not roll back, dirty data appears in DB, and so on. It is precisely because SQLException is defined as detecting exceptions that developers are driven to explicitly capture and clean up resources after the code generates an exception. Of course, after cleaning up the resources, you can continue to throw undetected exceptions to prevent the execution of the program. According to observation and understanding, detecting anomalies can mostly be applied to tool classes.
Misunderstanding two, display the exception directly on the page or client.
It is not uncommon to print exceptions directly to the client. Take JSP as an example, once an exception occurs when the code runs, the container prints the exception stack information directly on the page by default. In fact, from the customer's point of view, any exception has no practical significance, the vast majority of customers simply do not understand the exception information, software development should also try to avoid presenting the exception directly to the user.
Listing 1
Package com.ibm.dw.sample.exception; / * Custom RuntimeException * add error code attribute * / public class RuntimeException extends java.lang.RuntimeException {/ / default error code public static final Integer GENERIC = 1000000; / / error code private Integer errorCode; public RuntimeException (Integer errorCode, Throwable cause) {this (errorCode, null, cause) } public RuntimeException (String message, Throwable cause) {/ / use common error codes this (GENERIC, message, cause);} public RuntimeException (Integer errorCode, String message, Throwable cause) {super (message, cause); this.errorCode = errorCode;} public Integer getErrorCode () {return errorCode;}}
As shown in the sample code, an error code is introduced into an exception, and once an exception occurs, we only need to present the error code of the exception to the user, or convert the error code into a more easy-to-understand prompt. In fact, the error code here also contains another function, and developers can also know exactly what type of exception occurred according to the error code.
Misunderstanding 3. Pollution of code hierarchy
We often divide the code into different hierarchies such as Service, Business Logic, DAO, and so on, and the DAO layer contains methods to throw exceptions, as shown in listing 2:
Listing 2
Public Customer retrieveCustomerById (Long id) throw SQLException {/ / query the database according to ID}
The above code looks fine at first glance, but considering carefully from the design coupling point of view, the SQLException here is contaminated to the upper-level calling code, and the call layer needs to explicitly use try-catch capture, or throw it further to a higher level. According to the principle of design isolation, we can modify it as follows:
Listing 3
Public Customer retrieveCustomerById (Long id) {
Try {
/ / query the database according to ID
} catch (SQLException e) {
/ / use non-detection exception encapsulation to detect anomalies and reduce hierarchical coupling
Throw new RuntimeException (SQLErrorCode, e)
} finally {
/ / close the connection and clean up the resources
}
}
Misunderstanding 4. Ignore anomalies
The following exception handling simply outputs the exception to the console and does not make any sense. And the exception here does not interrupt the program, and the call code continues to execute, resulting in more exceptions.
Listing 4
Public void retrieveObjectById (Long id) {try {/.. some code that throws SQLException} catch (SQLException ex) {/ * anyone who knows that the exception printing here is meaningless, just outputs the error stack to the console. * in a Production environment, you need to output the error stack to the log. * and here the program continues to execute after catch processing, which will lead to further problems * / ex.printStacktrace ();}}
Can be reconstituted:
Listing 5
Public void retrieveObjectById (Long id) {try {/.. some code that throws SQLException} catch (SQLException ex) {throw new RuntimeException ("Exception in retieveObjectById", ex);} finally {/ / clean up resultset, statement, connection etc}}
This misunderstanding is relatively basic, under normal circumstances will not make this low-level mistake .
Misunderstanding 5. Include exceptions in looping statement blocks
As shown in the following code, the exception is contained in the block of for loop statements.
Listing 6
For (int item0; I
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.