Java Reference
In-Depth Information
break;
case "security violation":
// Handle error
break;
default: throw e;
}
}
However, any change to the exception message literals involved will break the code.
For example, suppose this code is executed:
throw new Exception("cannot find file");
This exception should be handled by the first case clause, but it will instead be
rethrown because the string does not match any case clause.
Furthermore, exceptions may be thrown without a message.
This noncompliant code example falls under ERR08-EX0 of
The CERT
®
Oracle
®
Se-
cure Coding Standard for Java
™
[Long2012],“ERR08-J.Donotcatch
NullPointerEx-
ception
oranyofitsancestors,”becauseitcatchesgeneralexceptionsbutrethrowsthem.
Compliant Solution
Thiscompliantsolutionusesspecificexceptiontypesanddefinesnewspecialpurposeex-
ception types where required.
public class TimeoutException extends Exception {
TimeoutException () {
super();
}
TimeoutException (String msg) {
super(msg);
}
}
// ...
try {
doSomething();
} catch (FileNotFoundException e) {
// Handle error