---
title: Use rethrow to rethrow a caught exception
description: Datadog, the leading service for cloud-scale monitoring.
breadcrumbs: >-
  Docs > Datadog Security > Code Security > Static Code Analysis (SAST) > SAST
  Rules > Use rethrow to rethrow a caught exception
---

> For the complete documentation index, see [llms.txt](https://docs.datadoghq.com/llms.txt).

# Use rethrow to rethrow a caught exception

{% callout %}
# Important note for users on the following Datadog sites: app.ddog-gov.com, us2.ddog-gov.com

{% alert level="danger" %}
This product is not supported for your selected [Datadog site](https://docs.datadoghq.com/getting_started/site.md). ({% placeholder "user-datadog-site-name" /%}).
{% /alert %}

{% /callout %}

## Metadata{% #metadata %}

**ID:** `dart-code-quality/use-rethrow-when-possible`

**Language:** Dart

**Severity:** Notice

**Category:** Code Style

## Description{% #description %}

Throwing the caught exception object directly (for example `throw e`) inside its own catch block resets the stack trace and discards the original error origin, which makes debugging harder. Use rethrow instead to preserve the original exception and its stack trace.

## Non-Compliant Code Examples{% #non-compliant-code-examples %}

```dart
void main() {
  try {
    risky();
  } catch (e) {
    throw e;
  }

  try {
    risky();
  } on Exception catch (e, s) {
    throw e;
  }
}

void risky() {}
```

## Compliant Code Examples{% #compliant-code-examples %}

```dart
void main() {
  try {
    risky();
  } catch (e) {
    rethrow;
  }

  try {
    risky();
  } catch (e) {
    throw StateError('x');
  }

  try {
    risky();
  } catch (e) {
    throw other;
  }

  try {
    risky();
  } on Exception catch (e, s) {
    throw e.inner;
  }
}

var other = Exception('boom');

void topLevel() {
  throw e;
}

void risky() {}
```
  Seamless integrations. Try Datadog Code SecurityDatadog Code Security 
{% icon name="icon-external-link" /%}
 