Maintaining a consistent link between the caught variable and the exception thrown in the corresponding try block brings clarity and predictability. Reassigning the caught variable disrupts this expectation and can lead to confusion. By refraining from these reassignments, developers can know that the variable encapsulates the essence of the original exception.
Non-Compliant Code Examples
publicclassFoo{publicvoidfoo(){try{// do something}catch(Exceptione){e=newNullPointerException();// should not reassign here}}}
Compliant Code Examples
publicclassFoo{publicvoidfoo(){try{// do something}catch(MyExceptione){newError=newRuntimeException();// created a new error variable}}}
シームレスな統合。 Datadog Code Security をお試しください
Datadog Code Security
このルールを試し、Datadog Code Security でコードを解析する
このルールの使用方法
1
2
rulesets:- java-best-practices # Rules to enforce Java best practices.