Caught exception is thrown again instead of rethrown

ID

dart.correctness.use_rethrow_when_possible

Severity

low

Remediation Complexity

trivial

Remediation Risk

low

Remediation Effort

low

Resource

Error Handling

Language

Dart

Description

Reports a catch clause whose body throws the very object it caught, where the rethrow statement would do the same thing.

Rationale

throw e starts a new throw: the stack trace is replaced by one that begins at the catch clause, so every frame between the original failure and the handler is gone. That is exactly the part of the trace that identifies where the problem happened, and losing it turns a one-line diagnosis into a search. rethrow continues the original throw with its trace intact, and it also states the intent — this handler observed the failure and is passing it on.

The following code illustrates the pattern detected by this rule:

class Client {
  Future<String> fetch(String url) async {
    // FLAGGED: Caught exception is thrown again instead of rethrown
    try {
      return await get(url);
    } catch (e) {
      log('fetch failed: $e');
      throw e;
    }
  }

  Future<String> get(String url) async => url;
  void log(String m) {}
  void schedule(void Function() task) {}
}

Remediation

Replace throw e; with rethrow;. Any logging or cleanup in the handler can stay exactly as it is; only the throw statement changes. When the intent is to report a different failure, throw a new object that wraps the original instead of the original itself.

Configuration

This detector does not need any configuration.

© 2026 Xygeni. All rights reserved