Skip to main content

avoid-nested-try-statements

configurable
pro+

Warns when a try statement contains another try statement.

Multiple nested try statements can significantly reduce readability.

⚙️ Config

Set acceptable-level (default is 2) to configure the acceptable nesting level.

analysis_options.yaml
dart_code_metrics:
rules:
- avoid-nested-try-statements:
acceptable-level: 2

Example

❌ Bad:

try {
// ...

try {
// ...

// LINT: Avoid nested try statements. Try rewriting the code to remove nesting.
try {
throw Error();
} catch (_) {}
} catch (_) {}
} catch (_) {}

✅ Good:

try {
try {
throw Error();
} catch (_) {}
} catch (_) {}