Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Capture and section state not reported correctly with manual exception handling followed by unhandled exception #179

Closed
cschreib opened this issue Aug 28, 2024 · 1 comment · Fixed by #183
Labels
bug:confirmed Something isn't working (confirmed)
Milestone

Comments

@cschreib
Copy link
Member

cschreib commented Aug 28, 2024

This is a bit of a niche edge case, but following changes in #178 (unreleased, so only affecting the main branch), the following doesn't behave as expected:

For captures:

try {
    int i = 1;
    CAPTURE(i);
    throw std::runtime_error("bad 1");
} catch (...) {
}

throw std::runtime_error("bad 2");

For sections:

try {
    SNITCH_SECTION("section 1") {
        throw std::runtime_error("bad 1");
    }
} catch (...) {
}

throw std::runtime_error("bad 2");

In both cases, the unhandled exception "bad 2" will be reported with the capture/section information of "bad 1".

This will only occur if no snitch macro is encountered between the two throw. So, for example, the following works again as expected:

try {
    int i = 1;
    CAPTURE(i);
    throw std::runtime_error("bad 1");
} catch (...) {
}

// If there is a capture...
int j = 2;
CAPTURE(j);
// ... or a check
CHECK(1 == 2);
// ... or a section
SECTION("subsection 1") { /* ... */ }
// ... then the reported information will be accurate again for unhandled exceptions.

throw std::runtime_error("bad 2");
@cschreib
Copy link
Member Author

cschreib commented Sep 28, 2024

Following #183, the problem can now be solved by adding a call to snitch::notify_exception_handled() like so:

try {
    int i = 1;
    CAPTURE(i);
    throw std::runtime_error("bad 1");
} catch (...) {
    snitch::notify_exception_handled();
}

throw std::runtime_error("bad 2");

To clarify, you only need this if all the following conditions are true:

  • You are manually catching exceptions in your test code (i.e., you are not using one of the snitch macros for testing exceptions, but rolling your own logic).
  • The code executed inside the try block uses snitch macros SECTION(), CAPTURE(), or INFO().
  • The code immediately after the try/catch block:
    • can throw, and
    • is not itself inside another explicit try/catch block, and
    • does not use any snitch macro (CHECK_*(), SECTION(), etc.),
  • You care that unhandled exceptions in that code are reported with exact contextual information.

This is a fairly niche case, and in most cases omitting the call to snitch::notify_exception_handled() will be fine.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug:confirmed Something isn't working (confirmed)
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant