Effective Java Reading notes eight exceptions

Source: Internet
Author: User

First, use exceptions only for exceptions

1. When the class has a state-related method, the state test method and the recognizable return value two policies can be used.

Second, use the exception for the recoverable situation, use run-time exception for programming error

1. If the caller is expected to recover properly, the exception to be inspected should be used.

2. Use runtime exceptions to indicate programming errors.

Third, the code unnecessary use of the exception to be inspected

1, the use of the exception of the two conditions:

A, the proper use of the API does not prevent the occurrence of such abnormal conditions

b, after an exception occurs, programmers using the API can take useful actions immediately.

Iv. priority use of standard exceptions

1, IllegalArgumentException

2, IllegalStateException

3, NullPointerException

4, Indexoutofboundsexception

5, Concurrentmodificationexception

V. Throwing exceptions corresponding to the level of abstraction

1, the implementation of each layer should be transferred to the bottom of the underlying exception to translate.

2, if the bottom of the exception to debug high-level exception to help group, can use the form of abnormal chain translation.

3, high-level strategy is the priority to prevent or deal with the underlying exception, do not make exception translation.

Six, each method throws the exception to have the document

Vii. include information that can capture failures in the detail message

1, the abnormal details should be captured failure, easy to analyze later.

2, Indexoutofboundsexception is a better example.

Viii. efforts to maintain the atomicity of failure

1. A failed method call should keep the object in the state before the call, and this method has atomic failure.

2. Several strategies to maintain the atomic nature of failure

A. Objects that use immutable classes

B. Check the validity of the parameters before performing the operation

C, adjust the processing process, let the judgment of possible failure occur before changing the state of the object

D. Write a recovery code

E, perform a temporary copy operation

Nine, do not flicker abnormal

1. Exceptions are FileInputStream

Effective Java Reading notes eight exceptions

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.