Skip to content

Log explanatory string of std::exception when logging uncaught C++ exceptions #860

Open

Description

@triplef

When an unhandled C++ exception is logged, it would in some cases be very helpful to also see the exception message returned by std::exception::what().

Would it be possible for Sentry in its exception handler to check the exception type, and if it’s of type std::exception log its explanatory string as the error message?

Activity

  1. moved this to Waiting for: Product Owner in GitHub Issues with 👀on Jun 30, 2023
  2. supervacuus commented on Jul 1, 2023

    @supervacuus
    Collaborator

    Hi @triplef, the best way to achieve this is to implement a terminate_handler in the SDK and allow users to enable it via the options. This is a C++-specific handler where we can be sure that we are processing a C++ exception instead of any abort() (which the default terminate_handler uses to close the program).

    It should be relatively easy to implement this for the inproc backend (I guess you are asking for Android?). I would love to have this in all backends, but we can start with one. I'll look into it, but I can't promise how soon we'll tackle this.

  3. triplef commented on Jul 1, 2023

    @triplef
    Author

    Sounds great, thank you @supervacuus! We'd be interested in this on Android and Windows (particularly on Windows as we have more 3rd party C++ code there which might throw exceptions like that).

  4. self-assigned this
    on Aug 4, 2023
  5. triplef commented on Oct 24, 2023

    @triplef
    Author

    If we implemented this on the app side would something like this work?

    std::set_terminate([](){
    	sentry_value_t stacktrace = sentry_value_new_stacktrace(NULL, 0);
    
    	std::exception_ptr e = std::current_exception();
    	const char *message = "Unknown exception";
    	try {
    		std::rethrow_exception(e);
    	} catch (std::exception &e) {
    		message = e.what();
    #if defined(_WIN32)
    	} catch (winrt::hresult_error const& e) {
    		message = winrt::to_string(e.message()).c_str();
    #endif
    	}
    
    	sentry_value_t exc = sentry_value_new_exception(typeid(e).name(), message);
    	sentry_value_set_by_key(exc, "stacktrace", stacktrace);
    	sentry_value_t event = sentry_value_new_event();
    	sentry_event_add_exception(event, exc);
    	sentry_capture_event(event);
    	sentry_close();
    
    	std::abort();
    });
  6. 42 remaining items

  7. added 6 commits that reference this issue on Sep 4, 2026
    40904ca
    0925fe9
    d5a329a
    fcb7d7e
    faaea15
    5bfc21a
  8. added a parent issue on Sep 21, 2026
  9. removed a parent issue on Oct 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Errorsissues relates to the error reporting productGood First Issuegood for newcomersImprovementimprovement for existing featuresNativeplatform label

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions