Do Not Throw java.lang.Throwable
ID |
java.no_throw_throwable |
Severity |
info |
Remediation Complexity |
trivial |
Remediation Risk |
low |
Remediation Effort |
low |
Resource |
Reliability |
Language |
Java |
Tags |
CWE:397, reliability |
Description
Reports throw new Throwable(…) statements where the raw java.lang.Throwable is instantiated and thrown directly.
Rationale
java.lang.Throwable is the root of the entire exception hierarchy. Throwing it forces callers to catch (Throwable) to intercept it, which also catches Error and its subclasses (e.g. OutOfMemoryError, StackOverflowError). This makes error handling fragile and defeats the purpose of the type hierarchy.
// Bad -- callers must catch Throwable, swallowing Errors too
public void process() throws Throwable {
throw new Throwable("something failed");
}