Skip to content

Commit 5de2078

Browse files
committed
Document a couple footguns, closes #245
1 parent b0d0417 commit 5de2078

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

README.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -572,6 +572,36 @@ CPPTRACE_TRY {
572572
}
573573
```
574574

575+
> [!CAUTION]
576+
> There is a footgun with `return` statements in these try/catch macros: The implementation on Windows requires wrapping
577+
> the try body in an immediately-invoked lambda and and as such `return` statements return from the lambda not the
578+
> enclosing function. If you're writing code that will be compiled on windows, it's important to not write `return`
579+
> statements within CPPTRACE_TRY. E.g., this does not work as expected on windows:
580+
> ```cpp
581+
> CPPTRACE_TRY {
582+
> if(condition) return 40; // does not return from the enclosing function on windows
583+
> } CPPTRACE_CATCH(const std::exception& e) {
584+
> ...
585+
> }
586+
> return 20;
587+
> ```
588+
589+
> [!WARNING]
590+
> There is one other footgun which is mainly relevant for code that was written on an older version of cpptrace: It's
591+
> possible to write the following without getting errors
592+
> ```cpp
593+
> CPPTRACE_TRY {
594+
> ...
595+
> } CPPTRACE_CATCH(const std::runtime_error& e) {
596+
> ...
597+
> } catch(const std::exception& e) {
598+
> ...
599+
> }
600+
> ```
601+
> This code will compile and in some sense work as expected as the second catch handler will work, however, cpptrace
602+
> won't know about the handler and as such it would be able to correctly collect a trace on a non-`std::runtime_error`
603+
> that is thrown. No run-time errors will occur, however, `from_current_exception` may report a misleading trace.
604+
575605
### Removing the `CPPTRACE_` prefix
576606
577607
`CPPTRACE_TRY` is a little cumbersome to type. To remove the `CPPTRACE_` prefix you can use the

0 commit comments

Comments
 (0)