Accidental rethrow¶
ID: cpp/rethrow-no-exceptionKind: problemSecurity severity: Severity: warningPrecision: highTags: - reliability - correctness - exceptionsQuery suites: - cpp-security-and-quality.qls
Click to see the query in the CodeQL repository
The C++throw expression can take several forms. One form throws a new exception, whereas the other re-throws the current exception. In the latter case, if there is no current exception, then the program will be terminated. Presence of a re-throw outside of an exception handling context is often caused by the programmer not knowing what kind of exception to throw.
Recommendation¶
Thethrow expression should be changed to throw a particular type of exception.
Example¶
voidbad(){/* ... */if(error_condition)throw;}voidgood(){/* ... */if(error_condition)throwstd::exception("Something went wrong.");}
References¶
Open Standards:Standard for Programming Language C++, draft n3337 [except.throw], clause 9, page 380.