Strict exception rules

Source: Internet
Author: User
Tags finally block

· Avoidcatchingthrowable: This is dangerous because it casts too wide a net; it can catch things like outofmemoryerror.

Translation avoids catch throwable objects: This is dangerous because the coverage is too wide and it can catch errors similar to outofmemoryerror.

· Signaturedeclarethrowsexception: it is unclear which exceptions that can be thrown from the methods. It might be difficult to document and understand the vague interfaces. Use either a class derived from runtimeexception or a checked exception.

An exception thrown by a specific statement: you are not sure what type of exception can be thrown in the method. It is very difficult to prove and understand a fuzzy interface. The thrown exception class either inherits from runtimeexception or throws a checked exception.

· Exceptionasflowcontrol: using exceptions as flow control leads to gotoish code and obscures true exceptions when debugging.

Translation exceptions are used as process control: using exceptions for process control will lead to goto-type code and cause real exceptions to be vague during debugging.

· Avoidcatchingnpe: Code shocould never throw NPE under normal circumstances. A Catch Block may hide the original error, causing other more subtle errors in its wake.

Translation to avoid NULL pointer capturing exceptions: under normal circumstances, the Code should not capture nullpointexception. Otherwise, the catch block may hide the original error, resulting in more subtle errors.

· Avoidthrowingrawexceptiontypes: avoid throwing certain exception types. Rather than throw a raw runtimeexception, throwable, exception, or error, use a subclassed exception or error instead.

Avoid throwing the original exception type: avoid throwing a specific exception type. Instead of throwing the original types such as runtimeexception, throwable, exception, and error, it is better to use their subexceptions or suberror classes.

· Avoidthrowingnullpointerexception: avoid throwing a nullpointerexception-it's confusing because most people will assume that the virtual machine threw it. consider using an illegalargumentexception instead; this will be clearly seen as a programmer-initiated exception.

Avoid throwing a null pointer exception: avoid throwing a null pointer exception-this can cause misunderstanding, because most people think this should be thrown by the virtual machine. Consider using illegalargumentexception instead, which is clear for developer definition exceptions.

· Avoidrethrowingexception: Catch blocks that merely rethrow a caught exception only add to code size and runtime complexity.

Avoid repeated throwing exceptions in Translation: simply throwing a caught exception in a catch block will increase the amount of code and the complexity of program running.

· Donotextendjavalangerror: errors are system exceptions. do not extend them.

Do not inherit java. Lang. Error: error is a system exception. do not inherit it.

· Donotthrowexceptioninfinally: throwing exception in a Finally block is confusing. it may mask exception or a defect of the Code, it also render code cleanup uninstable. note: This is a pmd implementation of the lint4j rule "a throw in a Finally block"

Do not throw an exception in the Finally block: it is confusing to throw an exception in the Finally block. It can mask code exceptions or defects and cause code instability. Note: This is implemented based on the lint4j principle.

· Avoidthrowingnewinstanceofsameexception: Catch blocks that merely rethrow a caught exception wrapped inside a new instance of the same type only add to code size and runtime complexity.

Avoid throwing new instances with similar exceptions: The Catch Block only resends a new instance with the same type of caught exceptions, which only increases the code volume and operation complexity.

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.