org.sonar.l10n.java.rules.java.S1193.html Maven / Gradle / Ivy
Why is this an issue?
A try-catch
block is used to handle exceptions or errors that may occur during the execution of a block of code. It allows you to
catch and handle exceptions gracefully, preventing your program from terminating abruptly.
The code that may throw an exception is enclosed within the try
block, while each catch
block specifies the type of
exception it can handle. The corresponding catch block is executed if the exception matches the type specified in any catch block. It is unnecessary
to manually check the types using instanceof
because Java automatically matches the exception type to the appropriate catch block based
on the declared exception type in the catch clauses.
How to fix it
Replace if
statements that check the exception type using instanceof
with corresponding catch
blocks.
Code examples
Noncompliant code example
try {
/* ... */
} catch (Exception e) {
if(e instanceof IOException) { /* ... */ } // Noncompliant
if(e instanceof NullPointerException{ /* ... */ } // Noncompliant
}
Compliant solution
try {
/* ... */
} catch (IOException e) { /* ... */ } // Compliant
} catch (NullPointerException e) { /* ... */ } // Compliant
Resources
- CERT,
ERR51-J. - Prefer user-defined exceptions over more general exception types
- Oracle - Exceptions