Skip to main content

handle-throwing-invocations

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() {
// LINT: This invocation is annotated with @Throws and can throw an exception.
// Try handing this exception or annotating the outer declaration with @Throws.
someFunction();

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

({IndexError})
final local = someFunction;

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

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

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

({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