Usage

Use the SDK to manually capture errors and other events.

Sentry's SDK hooks into your runtime environment and automatically reports errors, uncaught exceptions, and unhandled rejections as well as other types of errors depending on the platform.

Key terms:

  • An event is one instance of sending data to Sentry. Generally, this data is an error or exception.
  • An issue is a grouping of similar events.
  • The reporting of an event is called capturing. When an event is captured, it’s sent to Sentry.

The most common form of capturing is to capture errors. What can be captured as an error varies by platform. In general, if you have something that looks like an exception, it can be captured. For some SDKs, you can also omit the argument to capture_exception and Sentry will attempt to capture the current exception. It is also useful for manual reporting of errors or messages to Sentry.

While capturing an event, you can also record the breadcrumbs that lead up to that event. Breadcrumbs are different from events: they will not create an event in Sentry, but will be buffered until the next event is sent. Learn more about breadcrumbs in our Breadcrumbs documentation.

To capture an error or exception condition, create events containing an exception object:

Copied
#include <sentry.h>

sentry_value_t event = sentry_value_new_event();

sentry_value_t exc = sentry_value_new_exception("Exception", "Error message.");
sentry_event_add_exception(event, exc);

sentry_capture_event(event);

The above exception does not contain a stack trace, which must be added separately:

Copied
#include <sentry.h>

sentry_value_t event = sentry_value_new_event();

sentry_value_t exc = sentry_value_new_exception("Exception", "Error message.");
sentry_value_set_stacktrace(exc, NULL, 0);
sentry_event_add_exception(event, exc);

sentry_capture_event(event);

Another common operation is to capture a bare message. A message is textual information that should be sent to Sentry. Typically, our SDKs don't automatically capture messages, but you can capture them manually.

To simplify creating events, there are shorthand functions that construct prepopulated event objects. The most important one is sentry_value_new_message_event. The logger and message parameters are each optional.

Copied
#include <sentry.h>

sentry_value_t event = sentry_value_new_message_event(
  /*   level */ SENTRY_LEVEL_INFO,
  /*  logger */ "custom",
  /* message */ "It works!"
);

sentry_capture_event(event);

To create and capture a manual event, follow these steps:

  1. Create an event value using sentry_value_new_event. This internally creates an object value and initializes it with common event attributes, like a timestamp and event_id.
  2. Add custom attributes to the event, like a message or an exception.
  3. Send the event to Sentry by invoking sentry_capture_event.

In a more complex example, it looks like this:

Copied
sentry_value_t event = sentry_value_new_event();
sentry_value_set_by_key(event, "message", sentry_value_new_string("Hello!"));

sentry_value_t screen = sentry_value_new_object();
sentry_value_set_by_key(screen, "width", sentry_value_new_int32(1920));
sentry_value_set_by_key(screen, "height", sentry_value_new_int32(1080));

sentry_value_t contexts = sentry_value_new_object();
sentry_value_set_by_key(contexts, "screen_size", screen);

sentry_value_set_by_key(event, "contexts", contexts);
sentry_capture_event(event);

For the full list of supported values, see Event Payloads and linked documents.

Help improve this content
Our documentation is open source and available on GitHub. Your contributions are welcome, whether fixing a typo (drat!) or suggesting an update ("yeah, this would be better").