Files
codeql/java/ql/src/Advisory/Statements/TerminateIfElseIfWithElse.qhelp
Marcono1234 e21cbe82a9 Update Java documentation links to Java 11
Where possible update Java documentation links to Java 11.
Additionally update some other links to use HTTPS.
2021-02-26 00:43:51 +01:00

49 lines
1.3 KiB
XML

<!DOCTYPE qhelp PUBLIC
"-//Semmle//qhelp//EN"
"qhelp.dtd">
<qhelp>
<overview>
<p>
An <code>if-else-if</code> statement without a terminating <code>else</code>
clause may allow execution to 'fall through' silently, if none of the <code>if</code> clauses are
matched.
</p>
</overview>
<recommendation>
<p>
Include a terminating <code>else</code> clause to <code>if-else-if</code> statements
to prevent execution from falling through silently. If the terminating <code>else</code> clause is
intended to be unreachable code, it is advisable that it throws a <code>RuntimeException</code> to
alert the user of an internal error.
</p>
</recommendation>
<example>
<p>In the following example, the <code>if</code> statement outputs the grade that is achieved depending on the
test score. However, if the score is less than 60, execution falls through silently.</p>
<sample src="TerminateIfElseIfWithElse.java" />
<p>In the following modified example, the <code>if</code> statement includes a terminating
<code>else</code> clause, to allow for scores that are less than 60.</p>
<sample src="TerminateIfElseIfWithElseGood.java" />
</example>
<references>
<li>
Java SE Documentation:
<a href="https://www.oracle.com/java/technologies/javase/codeconventions-statements.html#449">7.4 if, if-else, if else-if else Statements</a>.
</li>
</references>
</qhelp>