---
title: "Capturing Errors"
description: "Learn how Sentry captures unhandled errors and handled errors to enrich your event data."
url: https://docs.sentry.io/product/sentry-basics/integrate-backend/capturing-errors/
---

# Capturing Errors

Once initialized in your code, the Sentry SDK will capture errors and various types of events to notify you about them in real-time, depending on the alert rules you've configured. With the Django app already running on your [localhost](http://localhost:8000/), let's try them out.

If you're using your own source code, follow the [Getting Started](https://docs.sentry.io/platforms/python/integrations/django.md) instructions to introduce an error into your app.

## [Unhandled Errors](https://docs.sentry.io/product/sentry-basics/integrate-backend/capturing-errors.md#unhandled-errors)

The Sentry SDK will automatically capture and report any *unhandled error* that occurs in your application runtime without any additional configuration or explicit handling. Generally, unhandled errors are errors that aren't caught by a `try/except` clause.

To trigger an unhandled error:

1. Run the local Django app.

2. Point your browser to `http://localhost:8000/unhandled`.

3. If you've set up an [alert rule](https://docs.sentry.io/product/alerts/create-alerts.md), you should be notified about the error. Otherwise, open the [**Issues**](https://sentry.io/orgredirect/organizations/:orgslug/issues/) page in your Sentry account.

4. You should see the unhandled exception in the list of issues.

5. Click on the issue, to open the **Issue Details** page.

6. Notice that the event:

   * Is tagged with the `environment` and `release` options we've set in the previous tutorial with the tag `handled:no`, marking this event as an unhandled error.

   * Contains a suspect commit, made possible because of the commit-tracking feature we enabled earlier.

   * Contains the custom breadcrumb we added through the SDK.

## [Handled Errors](https://docs.sentry.io/product/sentry-basics/integrate-backend/capturing-errors.md#handled-errors)

The Sentry SDK contains several methods that you can use to **explicitly** report errors, events, and custom messages in except clauses, critical areas of your code, and so on.

### [Capture Exception](https://docs.sentry.io/product/sentry-basics/integrate-backend/capturing-errors.md#capture-exception)

1. Open the `views.py` file. Notice that we import `sentry_sdk` lib which contains the `capture_exception` method:

   `myapp/views.py`

   ```python
   import sentry_sdk
   ```

2. The method is used to capture the exception handled by the except clause in `HandledErrorView`:

   `myapp/views.py`

   ```python
   class HandledErrorView(APIView):
      def get(self, request):
         ...
         try:
             '2' + 2
         except Exception as err:
             sentry_sdk.capture_exception(err)

         return Response()
   ```

3. To try it out on your localhost, trigger the following endpoint: `http://localhost:8000/handled`.

4. Similar to what we did with the unhandled error, open the new issue's detail page.

5. Notice that the event is tagged with the same `environment` and `release` configuration options. Hover over the "i" icon in the release tag to reveal the release information and the commits associated with it.

6. Click on the release's "i" icon to navigate to the release page.

### [Capture Message](https://docs.sentry.io/product/sentry-basics/integrate-backend/capturing-errors.md#capture-message)

Typically, `capture_message` is not emitted, but there are times when a developer may want to add a simple message within their app for debugging purposes, and `capture_message` is great for that.

1. In the `views.py` file, the `capture_message` method is made available through the `sentry_sdk` lib import.

2. You can use it anywhere within your app. In our example, we've created a dedicated view class, `CaptureMessageView`, to trigger and capture a message we want to track:

   `myapp/views.py`

   ```python
   sentry_sdk.capture_message("You caught me!")
   ```

3. To try it out on your localhost, trigger the following endpoint: `http://localhost:8000/message`.

4. As before, open the new issue’s detail page from the **Issues** page.

   You'll need to remove the `"issue.priority is high or medium"` filter from the "Custom Search" field on the [**Issues**](https://sentry.io/orgredirect/organizations/:orgslug/issues/) page to see all the issue types in the list.

   > By default, captured messages are marked with a severity level tag `level:info`, as reflected in the tags section. However, the `capture_message` methods accept an **optional** severity level parameter.

5. In the `views.py` file, change the `capture_message` method to:

   `myapp/views.py`

   ```python
   sentry_sdk.capture_message("You caught me!", "fatal")
   ```

6. Save the changes and trigger the `/message` endpoint again. Changes should be applied immediately through `StateReloader`.

7. Notice that the severity level tag on the new event now shows `level:fatal`.

## [Enriching your Event Data](https://docs.sentry.io/product/sentry-basics/integrate-backend/capturing-errors.md#enriching-your-event-data)

You can enrich your event and error data through the Sentry SDK by adding custom tags and user context attributes. In addition to providing more context to your errors, enriched data will expand your options to search, filter, and query through your event metadata. Learn more about the advantages of enriching your data in [Event Data](https://docs.sentry.io/product/sentry-basics/enrich-data.md).

To enrich the data of the message events we've captured with `capture_message`:

1. In the `views.py` file, locate the line that triggers `sentry_sdk.capture_message`.

2. Replace that line with the following code:

   `myapp/views.py`

   ```python
   sentry_sdk.set_tag("my-tag", "my value")
   sentry_sdk.user = { "email" : "my.email@your.domain.com" }
   sentry_sdk.set_extra("someVariable", "some data")

   sentry_sdk.capture_message("You caught me!", "fatal")
   ```

3. Save your changes and trigger the `/message` endpoint again.

4. Open the issue’s detail page from the [**Issues**](https://sentry.io/orgredirect/organizations/:orgslug/issues/) page.

5. Notice that:

   * The **user email** is now displayed on the details page and the number of unique users impacted by this event is reflected in the issue's header.

   * The **custom tag** is now available (and searchable) in the list of tags.

   * The **extra data** we set on the SDK is reflected in the "ADDITIONAL DATA" context.
