Movatterモバイル変換


[0]ホーム

URL:


CodeQL documentation
CodeQL resources

Catching by value

ID: cpp/catch-by-valueKind: problemSecurity severity: Severity: warningPrecision: very-highTags:   - efficiency   - correctness   - exceptionsQuery suites:   - cpp-security-and-quality.qls

Click to see the query in the CodeQL repository

Catching an exception by value will create a new local variable which is a copy of the originally thrown object. Creating the copy is slightly wasteful, but not catastrophic. More worrisome is the fact that if the type being caught is a strict supertype of the originally thrown type, then the copy might not contain as much information as the original exception.

Recommendation

The parameter to thecatch block should have its type changed fromT toT& orconstT&.

Example

voidbad(){try{/* ... */}catch(std::exceptiona_copy_of_the_thrown_exception){// Do something with a_copy_of_the_thrown_exception}}voidgood(){try{/* ... */}catch(conststd::exception&the_thrown_exception){// Do something with the_thrown_exception}}

References


[8]ページ先頭

©2009-2025 Movatter.jp