Skip to content

Commit 0a5b643

Browse files
geoffw0Copilot
andcommitted
C++: Test catching exceptions by value
Cover class exceptions caught by value, with reference and pointer catches excluded from cpp/catch-by-value. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent ab77d68 commit 0a5b643

3 files changed

Lines changed: 33 additions & 0 deletions

File tree

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
| test.cpp:12:32:12:34 | { ... } | This should catch a DerivedException by (const) reference rather than by value. |
2+
| test.cpp:16:29:16:31 | { ... } | This should catch a BaseException by (const) reference rather than by value. |
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
query: Best Practices/Exceptions/CatchingByValue.ql
2+
postprocess: utils/test/InlineExpectationsTestQuery.ql
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class BaseException {
2+
public:
3+
virtual ~BaseException() {}
4+
};
5+
6+
class DerivedException : public BaseException {
7+
};
8+
9+
void catchByValueDerived() {
10+
try {
11+
throw DerivedException();
12+
} catch (DerivedException e) { } // $ Alert
13+
14+
try {
15+
throw BaseException();
16+
} catch (BaseException e) { } // $ Alert
17+
18+
try {
19+
throw DerivedException();
20+
} catch (DerivedException &e) { }
21+
22+
try {
23+
throw new DerivedException();
24+
} catch (DerivedException *e) { }
25+
26+
try {
27+
throw DerivedException();
28+
} catch (...) { }
29+
}

0 commit comments

Comments
 (0)