Common exception handling of hands-on brain 5JAVA project

      Several principles of Java exception handling is as follows.
    (1) Do not discard abnormal, need to be related to treatment after catching exceptions. If you think you can not deal well with the exception, let it continue to spread, spread to other places to deal with, or put into a low-level exceptions into application-level exception to re-thrown.
      (2) catch statement should specify a particular type of exception. Do not capture exceptions should be captured
      (3) release of resources in the finally inside. If finally there will be an exception is thrown, the same need to use try..catch process.
      (4) Do not plug a lot of code inside a try ... catch block, separating each statement and may appear abnormal, respectively catch the exception.
      (5) due to abnormal output may result in incomplete data, so the user needs to take appropriate action, at least of the data to be presented is incomplete
       

1, Java class libraries do not capture inherit from RuntimeException defined runtime exception classes, such as: IndexOutOfBoundsException / NullPointerException, this type of abnormal pre-checked by the programmer to circumvent the law, to ensure the robustness of the program.

2, abnormal not used for process control, condition control, since the processing efficiency is lower than the abnormal condition branch. The pit we should pay attention.

3, for a large section of code try-catch, this is irresponsible. Please distinguish catch when the code stable and unstable code and the code refers to the stable is in any case not go wrong code. For non-stationary catch code as possible to distinguish the type of exception, the corresponding exception handling do.

4, catch the exception to handle it, do not capture the process, but nothing abandon it, if you do not deal with it, the abnormal thrown to its caller. Outermost service user must handle the exception, the converted content to the user can be appreciated.
5, there is placed a try block transaction code, after the catch exception, if the need to roll back the transaction, we must pay attention to manually roll back the transaction.

6, finally block must resource object, stream object is closed, there is abnormality do try-catch. Note: If JDK7, you can use try-with-resources way.
7, return can not be used in a finally block, the finally block the return end of the execution method returns, the return statement will not execute the try block.

8, catch the exception and throw exceptions, you must be an exact match or capture parent exceptions are thrown exception. Note: If the other party is expected to throw a hydrangea, the actual shot is received, it will have surprises.

9, the return value can be null, not forced to return empty set, or an empty object, etc., you must add a comment fully explain under what circumstances would return a null value. The caller needs to be null judge to prevent the NPE problem.
Description: The statute expressly prevent NPE is the responsibility of the caller. Even if the called method returns an empty collection or an empty object to the caller, it is not sit back and relax, we must take into account the remote call failure, abnormal returns null, the scene of running.

10, using the code "throw an exception" or "return error code" for the company outside of http / api open interface must use the "error code"; and internal applications recommended exception is thrown; RPC calls between cross-application priority to use Result way, the package isSuccess, "error code", "error brief message."

11, the distinction between the definition of unchecked / checked exceptions, avoid direct use RuntimeException thrown, but not allowed to throw Exception or Throwable, should have business meaning using custom exception. Recommended industry is already defined custom exceptions, such as: DAOException / ServiceException and so on.
12, for runtime exceptions, we do not use try ... catch to capture processing, but in program development and debugging phase, try to avoid this exception, which, the correct approach would be to improve the programming of the code and if it is found implementation, modification errors in the program, in order to avoid such anomalies. Abnormal is a good solution when capturing and processing operation, because you can achieve by improving the code to avoid this kind of exception occurred. Inspected for abnormalities, did not say, honestly go according to the method of exception handling to deal with, either with a try ... catch caught and resolved, either with throws throw! For Error (runtime error), do not require any treatment program, the issue that you should find out the problem at the local program, and then solve.

Guess you like

Origin www.cnblogs.com/sunhongbin/p/11780757.html