Personal Java Code Specification 4

Source: Internet
Author: User
Tags exception handling

1. Objects that need to use end close () for database operations, IO operations, and so on must be closed in try-catch-finally finally (), and if there are multiple IO objects that require closing (), the method is try-catch to prevent an Io object from shutting down and other IO objects are not closed.

2. If the exception is not processed, the log should be logged after the abnormal capture of the system is not running normally.

3. The exception you throw must be filled in with detailed descriptive information.

4. Run-time exceptions are represented by RuntimeException subclasses, without the addition of throws clauses on method declarations that might throw exceptions. Non-run-time exceptions are inherited from exception and must be added throws clauses on the method declaration.

5. Use exception handling in the program or use error return code processing, depending on whether the program structure to determine, and the exception and error code should not be mixed, recommended use of exceptions.

6. Note the precedence of the operator and use the parentheses to clear the order of the expressions to avoid using the default precedence.

7. Avoid using numbers that are difficult to understand and replace them with meaningful logos. Constants involving physical states or physical meanings should not be used directly, and must be replaced with meaningful static variables or enumerations. Use exceptions to represent method execution errors instead of using C + + ERROR-return code.

8. Use int[] Index when declaring the array, instead of using int index[];

9. Do not use System.out and System.err for console printing, you should use the tool class (such as: Log tool) for uniform recording or printing.

10. Use the debugging switch to switch the debug and official versions of the software, rather than having both the official version and the debug version of the different source files to reduce the difficulty of maintenance.

11. The collection must specify the template type map<string,string> maps = new hashmap<string,string> ();

12. A file does not define two classes (not internal classes).

13. All data classes must overwrite ToString (), Hashcode (), Equals () methods, and the ToString () method returns meaningful content for that class.

14. Judge the statement not to use "* = = true" to judge the true

15. Do not write unnecessary upward forced transformation. int m = 10; float n = (float) m;

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.