For AI agents: A markdown version of this page is available at https://docs.datadoghq.com/security/code_security/static_analysis/static_analysis_rules/dart-code-quality/empty-catches.md. A documentation index is available at /llms.txt.
This product is not supported for your selected Datadog site. ().

Metadata

ID: dart-code-quality/empty-catches

Language: Dart

Severity: Warning

Category: Code Style

Description

When a catch block has no code, errors are swallowed and nothing shows up in logs or stack traces. That makes real bugs very hard to fix.

If you need to ignore an error on purpose, say so explicitly: log the exception, or add a short comment in the catch block explaining why it is safe to skip. A completely empty catch block should be avoided.

Non-Compliant Code Examples

void main() {
  try {
    risky();
  } catch (e) {}

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

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

void risky() {}

Compliant Code Examples

import 'dart:io';

void main() {
  try {
    risky();
  } catch (e) {
    print(e);
  }

  try {
    risky();
  } on IOException catch (e) {
    print(e);
  }

  try {
    risky();
  } catch (e) {
    // Intentionally ignored: cleanup is best-effort.
  }

  try {
    risky();
  } catch (_) {}
}
https://static.datadoghq.com/static/images/logos/github_avatar.svg https://static.datadoghq.com/static/images/logos/vscode_avatar.svg jetbrains

Seamless integrations. Try Datadog Code Security