← 返回首页
Unreachable catch clause — CodeQL query help documentation CodeQL docs
CodeQL documentation
CodeQL resources

Unreachable catch clause

ID: java/unreachable-catch-clause Kind: problem Security severity: Severity: warning Precision: high Tags: - quality - reliability - correctness - exceptions - external/cwe/cwe-561 Query suites: - java-code-quality.qls - java-security-and-quality.qls

Click to see the query in the CodeQL repository

An unreachable catch clause may indicate a logical mistake in the exception handling code or may simply be unnecessary.

Although certain unreachable catch clauses cause a compiler error, there are also unreachable catch clauses that do not cause a compiler error. A catch clause C is considered reachable by the compiler if both of the following conditions are true:

Recommendation

Ensure that unreachable catch clauses are removed or that further corrections are made to make them reachable.

Note that if a try-catch statement contains multiple catch clauses, and an exception that is thrown in the try block matches more than one of the catch clauses, only the first matching clause is executed.

Example

In the following example, the second catch clause is unreachable. The code is incomplete because a FileOutputStream is opened but no methods are called to write to the stream. Such methods typically throw IOExceptions, which would make the second catch clause reachable.

FileOutputStream fos = null; try { fos = new FileOutputStream(new File("may_not_exist.txt")); } catch (FileNotFoundException e) { // ask the user and try again } catch (IOException e) { // more serious, abort } finally { if (fos!=null) { try { fos.close(); } catch (IOException e) { /*ignore*/ } } }

References