PMD规则之Strict Exception Rules

来源:互联网 发布:最好拆卸软件 编辑:程序博客网 时间:2024/05/11 09:50

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

翻译   避免catch Throwable对象:这是危险的因为覆盖的范围太广,它能够catch类似于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.

翻译   具体声明抛出的异常:不确定方法中能够抛出什么样的具体异常。为模糊的接口提供证明并理解它是很困难的。抛出的异常类要么从RuntimeException中继承或者抛出一个被检查的异常。

·  ExceptionAsFlowControl: Using Exceptions as flow control leads to GOTOish code and obscures true exceptions when debugging.

翻译   异常用作流程控制:使用异常来做流程控制会导致goto类型的代码,且使得调试的时候发生的真正的异常含糊化。

·  AvoidCatchingNPE: Code should never throw NPE under normal circumstances. A catch block may hide the original error, causing other more subtle errors in its wake.

翻译   避免捕获空指针异常:在正常情形下代码不应该捕获NullPointException。否则Catch块可能隐藏原始错误,导致其他更多微妙的错误。

·  AvoidThrowingRawExceptionTypes: Avoid throwing certain exception types. Rather than throw a raw RuntimeException, Throwable, Exception, or Error, use a subclassed exception or error instead.

翻译   避免抛出原始异常类型:避免抛出特定异常类型。与其抛出RuntimeExceptionThrowableExceptionError等原始类型,不如用他们的子异常或子错误类来替代。

·  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.

翻译   避免抛出空指针异常:避免抛出空指针异常——这会导致误解,因为大部分人认为这应该由虚拟机抛出。考虑用IllegalArgumentException代替,这对于开发者定义异常来说比较清晰。

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

翻译   避免重复抛出异常:catch块仅仅重新抛出一个已捕获的异常只会增加代码量和程序运行的复杂度。

·  DoNotExtendJavaLangError: Errors are system exceptions. Do not extend them.

翻译   不要继承java.lang.Error:Error是系统异常,不要继承它。

·  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"

翻译   不要在finally块中抛出异常:在finally块中抛出异常易造成迷惑。它将掩盖代码异常或缺陷,也促使代码不稳定。备注:这是PMDLint4j原则衍生实现的

·  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.

翻译    避免抛出同类异常的新实例:catch块仅仅重新抛出一个同类型捕获异常的新的实例只会增加代码量和运行复杂度。

原创粉丝点击