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

captures with unexpected exceptions #748

Closed
dvirtz opened this issue Nov 21, 2016 · 4 comments
Closed

captures with unexpected exceptions #748

dvirtz opened this issue Nov 21, 2016 · 4 comments

Comments

@dvirtz
Copy link
Contributor

dvirtz commented Nov 21, 2016

Is it possible to make CAPTURE print its value when an unexpected exception occurs?
Currently, for the following code

#define CATCH_CONFIG_MAIN
#include <catch.hpp>
#include <stdexcept>

void foo()
{
    throw std::logic_error("error");
}

TEST_CASE("test")
{
    int i = 2;
    CAPTURE(i);
    foo();
    REQUIRE(false);
}

I get

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Test.exe is a Catch v1.1 b3 host application.
Run with -? for options

-------------------------------------------------------------------------------
test
-------------------------------------------------------------------------------
\giant_nas\dviry\my documents\visual studio 2015\projects\test\test\source.cpp(1
0)
...............................................................................

\giant_nas\dviry\my documents\visual studio 2015\projects\test\test\source.cpp(1
0): FAILED:
due to unexpected exception with message:
  error

===============================================================================
test cases: 1 | 1 failed
assertions: 1 | 1 failed

Press any key to continue . . .

While without calling foo() it's

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Test.exe is a Catch v1.1 b3 host application.
Run with -? for options

-------------------------------------------------------------------------------
test
-------------------------------------------------------------------------------
\giant_nas\dviry\my documents\visual studio 2015\projects\test\test\source.cpp(1
0)
...............................................................................

\giant_nas\dviry\my documents\visual studio 2015\projects\test\test\source.cpp(1
5): FAILED:
  REQUIRE( false )
with message:
  i := 2

===============================================================================
test cases: 1 | 1 failed
assertions: 1 | 1 failed

Press any key to continue . . .
@lightmare
Copy link
Contributor

lightmare commented Nov 21, 2016

No it's not possible, because captures are local to the scope they appear in. When an unCHECKed exception occurs, that scope is finished (local objects holding CAPTURE destroyed) before entering the handler (the one that prints "FAILED: due to unexpected exception").

@onqtam
Copy link

onqtam commented Nov 21, 2016

I think it could be possible with the use of C++17's std::uncaught_exceptions to do something when the local objects (the capture stuff) are destroyed during stack unwinding due to an exception...

or maybe just logging in globals in the destructor of the local capture stuff

@dvirtz
Copy link
Contributor Author

dvirtz commented Mar 30, 2017

I was beaten by this again so I thought to give it a go and made a PR

@dvirtz
Copy link
Contributor Author

dvirtz commented Apr 4, 2017 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants