Skip to main content

handle-throwing-invocations

added in: 1.19.0
Pro+

Warns when an invocation's declaration is annotated with @Throws() but the potential exception is not handled.

info

To use the @Throws() annotation, install the dart-code-metrics-annotations package.

And to get warnings for missing or unnecessary @Throws() annotations, enable the prefer-correct-throws rule.

Example

❌ Bad:

void fn() {
someFunction(); // LINT: This invocation is annotated with @Throws and can throw an exception. Try handing this exception or annotating the outer declaration with @Throws.

try {
someFunction(); // LINT: This invocation is annotated with @Throws and can throw an exception. Try handing this exception or annotating the outer declaration with @Throws.
} on Exception catch (_) {}

({IndexError})
final local = someFunction;

local(); // LINT: This invocation is annotated with @Throws and can throw an exception. Try handing this exception or annotating the outer declaration with @Throws.
}

({TypeError})
void nestedIncorrect() {
someFunction(); // LINT: This invocation is annotated with @Throws and can throw an exception. Try handing this exception or annotating the outer declaration with @Throws.
}

void fn(() void Function() callback) {
callback(); // LINT: This invocation is annotated with @Throws and can throw an exception. Try handing this exception or annotating the outer declaration with @Throws.
}

({IndexError})
void someFunction() {
...
}

✅ Good:

void fn() {
try {
someFunction();
} on IndexError catch (_) {}

try {
someFunction();
} catch (_) {}
}

()
void nested() {
someFunction();
}

void fn(() void Function() callback) {
try {
callback();
} catch (_) {}
}

({IndexError})
void someFunction() {
...
}

Additional Resources