Skip to content

Logging C++ exceptions inside try/catch block #1229

Closed

Description

@maxp1256

Hello,

I'm trying to log an exception from a catch block in our C++ app with sentry native 0.6.6

Currenty the catch block is declared with

catch(...) {
  /* set bad return code */
}

According to docs/AI i've changed it to:

catch (std::exception& ex) { 
  GetApp()->LogException("std::exception", ex.what(), NULL); 
  /* set bad return code */ 
}

and log the exceptions with:

void LogException(const char* szExceptionType, const char* szExceptionValue, const char* szEventMessage)
{
  auto event = sentry_value_new_message_event(SENTRY_LEVEL_FATAL, nullptr, szEventMessage);
  auto exception = sentry_value_new_exception(szExceptionType, szExceptionValue);
  sentry_event_add_exception(event, exception);
  sentry_value_set_stacktrace(exception, nullptr, 0);

  const auto uuid = sentry_capture_event(event);
}

Basically it's working, but I'm not sure if I'm collecting all types of exceptions?

for instance the following is not captured as std::execption and falls through the default catch (...) handler:

*((unsigned int*)0) = 0xDEAD01;

Activity

  1. moved this to Waiting for: Product Owner in GitHub Issues with 👀 3on May 8, 2025
  2. supervacuus commented on May 8, 2025

    @supervacuus
    Collaborator

    Basically it's working, but I'm not sure if I'm collecting all types of exceptions?

    In short: no.

    1. Also related to your other question: a C++ catch will only receive C++ exceptions, not all SEH exceptions. If you want to handle SEH exceptions, which includes hard faults like memory access errors and C++ exceptions, you need to use the SEH primitives.
    2. You will only catch exceptions in the type hierarchy of std::exception, thus guaranteeing a what() interface. In C++, you can throw almost anything (besides incomplete or abstract types), so even if you only consider catching C++ exceptions, you will ignore all that aren't a std::exception.

    for instance the following is not captured as std::execption and falls through the default catch (...) handler:

    Yes, a NULL-pointer dereference does not result in a C++ exception, and such a catch will be bypassed. While C++ exceptions are implemented in terms of SEH exceptions on Windows, SEH exceptions are a superset. If you want to handle all SEH exceptions, use the SEH primitives (__try, __except, __finally). However, these are at a much lower level (you'd have to manually filter the incoming SEH exception to identify it as a C++ exception) and have different semantics. Of course, these handlers are not portable to other platforms.

  3. moved this from Waiting for: Product Owner to No status in GitHub Issues with 👀 3on May 8, 2025
  4. locked and limited conversation to collaborators on May 8, 2025
  5. converted this issue into a discussion #1231 on May 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions