CWE-397ΒΆ
Declaration of Throws for Generic Exception. [Error-Conditions, Insufficient-Control-Flow-Management]
Required inputs: IR
Demonstrative Examples
Example 1
The following method throws three types of exceptions.
Example Language:Java (Unsupported language for documentation only)
public void doExchange() throws IOException, InvocationTargetException, SQLException {
...
}
While it might seem tidier to write
public void doExchange() throws Exception {
...
}
doing so hampers the caller's ability to understand and handle the exceptions that occur. Further, if a later revision of doExchange() introduces a new type of exception that should be treated differently than previous exceptions, there is no easy way to enforce this requirement.
Example 2
Early versions of C++ (C++98, C++03, C++11) included a feature known as Dynamic Exception Specification. This allowed functions to declare what type of exceptions it may throw. It is possible to declare a general class of exception to cover any derived exceptions that may be throw.
int myfunction() throw(std::exception) {
if (0) throw out_of_range();
throw length_error();
}
In the example above, the code declares that myfunction() can throw an exception of type "std::exception" thus hiding details about the possible derived exceptions that could potentially be thrown.
Excerpts from CWE [https://cwe.mitre.org], Copyright (C) 2006-2026, the MITRE Corporation. See section 9.4. "3rd-Party Licenses" in the documentation for full details.Possible Messages
Key |
Text |
Severity |
Disabled |
|---|---|---|---|
generic_throws |
Throwing overly broad exceptions promotes complex error handling code that is more likely to contain security vulnerabilities. |
None |
False |
Options
This rule shares the following common options: exclude_in_macros, exclude_messages_in_system_headers, excludes, extend_exclude_to_macro_invocations, includes, justification_checker, languages, post_processing, provider, report_at, severity
The following places define options that affect this rule: Stylechecks, Analysis-GlobalOptions
This rule has no individual options.