Today we'll talk about Java exception issues. In fact, in the development process of Java exception processing is not as simple as the imagination, simply say Java deficiencies, then the exception handling mechanism can also be counted as a (personal point of view). Why do I think so? Below I will use my own way of thinking in the development process to explain the abnormal use.
The Java exception is roughly divided into 3 categories:
Exception class, RuntimeException class and the error class, (concrete meaning I do not say more, the internet has a lot of information to carry on the related introduction).
The top layer of these 3 classes is the Throwable class.
In general, Java exceptions are caused by errors that need to be captured for processing. But in some scenarios it is not a mistake, but in order for the Java program to handle correctly, the Java unification is handled in an unusual way. In addition, in terms of the Java language itself, the use of many occasions (especially with the bottom of the system interaction) is an active processing mechanism, can not be passive processing mechanism (simply called the event Monitoring mechanism).
For example: Java blocking socket, you must read or write to know whether the socket connection is normal (simply that the socket is read or write before the server has been closed, but the client before reading or writing is not known). But is this close operation a mistake? I personally think that is not counted, like the transmission process in the resolution of the error, is the socket connection error. And there is the socket read timeout is the socket connection error. But in the program if you catch IOException is not clear is that type of error. In addition, Java for the socket easy to use, has basically been encapsulated into the application layer, many times the Java program can only carry out the configuration of the relevant parameters (such as the receive/send buffer size, connection timeout, read timeout, etc.), but not more management processing.
Another example is a database query scenario, if you use JDBC to query a table, but this table does not exist in the database, throws a SqlException exception, and throws a SqlException exception if there is a problem with the connection just before the query. At this time, after the Java program capture, the general exception processing steps: Statement/preparedstatement, resultset shutdown, and finally the connection shutdown operation. In fact, the first case connection is not a problem, is not required to close the operation. But the Java exception itself is a mechanism problem, which causes us to distinguish between writing programs.
In addition, if you catch every detail error, sometimes the program will be flooded with a large number of catch statements, it seems that the program's readability is not very good (and exception handling is also a very time-consuming Java processing). So personally think that the original Java in order to do so, just to unify the exception handling convenience, because the specific situation too much can not do one by one distinction.