Rule Definition
There are many cases where it is very useful to change an exception's class. But if you create a new exception using the original exception message (i.e. MyException(originalException.getMessage())) valuable information about the original exception will not be propagated.
As such, you may loss the original exception's class and stack trace. This lack of information will slow the investigation process when an issue occurs in validation or in production.
Remediation
Use Exception(String message, Throwable cause) or Exception(Throwable cause) signature to chain the exceptions. Exception chains contain information about the full call stack, thus allowing a change of type on the exception's way up the call stack without loss of stack information.
Violation Code Sample
private void init() throws InitException {
...
} catch( FileNotFoundException ex) {
throw new InitException(ex.getMessage()); // VIOLATION
}
}
Fixed Code Sample
private void init() throws InitException {
...
} catch( FileNotFoundException ex) {
throw new InitException("File Not Found", ex); // FIXED
}
}
Reference
http://www.developer.com/java/other/article.php/1431531/Chained-Exceptions-in-Java.htm
Related Technologies
JEE
Technical Criterion
CWE-778 - Insufficient Logging
About CAST Appmarq
CAST Appmarq is by far the biggest repository of data about real IT systems. It's built on thousands of analyzed applications, made of 35 different technologies, by over 300 business organizations across major verticals. It provides IT Leaders with factual key analytics to let them know if their applications are on track.