Where exception may appear, use try-catch or throws or both. My judgment is: If you don't want to be externally (the caller) that may appear (the caller) you can do, you will drop this exception inside the try-catch in the method; if you want to know the outside, you will throw the Excetion directly after catch. Throw a custom Exception. First, the exception type Java exception can be divided into two categories: Exception and RuntimeException (although RuntimeException is inherited from Exception). Exception Exception Representative "Unavoidable exception" If the IO abnormality is often such exceptions due to external reasons, the program itself cannot guarantee that they do not happen, so such exceptions must be captured. If this exception cannot be processed inside the function (with the throws statement after the function), if anything does not do, there is a compilation error. Runtimexception refers to "avoidable exceptions", such as NULL reference exception, which is caused by internal reasons for the program, which is avoidable. For such an exception, they can ignore them, but once the program will be extremely terminated. This kind of exception is very helpful to Debug, of course, if you need to catch. In addition, some places do not have Exception, but from commercial logic errors, non-expected, you can throw User Exception. For example, the user inputs illegal, Bank Account illegally overdrah, and the like.
Second, the main principle is handled an important principle of accidents, that is, deal with, or then throw, can never eat (You Either handle it, or throw it. You don't et it.) This is to say that when you capture an exception After that, you must decide whether this exception is handled immediately, or continue to throw this exception (or another custom abnormality) to capture by the calling client. A similar judgment will continue after the client captures it. In general, the GUI end is to handle exceptions. For example, after the JSP captures an exception, it is necessary to give the user a friendly error message without giving an error message for the system. The error information of the system is not very friendly on the other hand, and there is too much system information, which is easy to be used by malicious users to attack the system. In other words, all exceptions must eventually have an ultimate processor, which is the GUI. As for the intermediate link, if the JavaBean running in the server is to handle the captured exception, continue to throw the captured exception, it needs to be processed depending on the situation. Unless you want to give an exception handling responsibility to the caller, it is generally no need to use ThROWS. For example, if you want to read some files, if you want to inform the caller, let the caller determine how to handle this exception, you will put this exception throws; if you know how to handle this exception, or you want to add an exception , You can catch them on the spot. This depends entirely on what you want to handle the exception's immediate or want to return the handle responsibility to the caller. Depending on the structure and requirements of your program. It is necessary to pay attention to: 1. If you can't handle an exception, don't capture it. 2. If you capture an exception, please don't worry about it. 3. Try to capture abnormalities near the abnormality thrown. 4. Record it to the log in the place where you capture, unless you plan to reap out it. 5. Follow your abnormality to construct your way. 6, need to use several types of exceptions, especially for application exceptions. Capturing appropriate anomalies correctly handle an exception is not a relaxed task, because abnormal processing sometimes causes other unknown behavior. However, the following three rules can help you avoid the risk that the error can be handled. Rule # 1: Always capture the type of abnormality instead of ignite an abnormal superclass. The code snippet in List B is described in an example manner from the error code and correctly encoded two angles. In order to comply with the usual code habits, you can use the uppercase of the Exception class as the variable name as follows: catch (filenotfoundexception fnfe) and catch (SQLException SQLE) rule # 2: Never let the catch block remain blank. In many cases, although the Try / Catch block is indeed written but did not do anything in the CATCH part of the code. Or, if you use a log API (Logging API), please write the code to write an exception to the log. Listing C shows the above-coded errors and correct ways. Rule # 3: Never throw an example of an Exception base class. Developers should always throw out the exception class created. The API throwing an abnormally is difficult to handle. In the case of a declaration method, all questions in the list A will be strong on the head of the API user so they cannot handle exceptions in a professional programming method. The API developers can reduce the user's burden by incorporation of subclasses of the API declaration Exception class. The two techniques mentioned above can also be used better and more appropriate when processing an exception.