Skip to main content

prefer-correct-throws

has auto-fix
pro+

Warns when a declaration is missing or has an unnecessary @Throws() annotation.

info

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

And to get warnings for unhandled exceptions thrown by invocations with the @Throws() annotation, enable the handle-throwing-invocations rule.

Example

❌ Bad:

// LINT: This declaration is annotated with @Throws, but does not call any throwing expression.
// Try removing this annotation.
()
void fn() {
print('hi');
}

// LINT: This declaration throws an exception, but is not annotated with @Throws.
// Try adding the annotation.
void withThrow() {
throw Error();
}

// LINT: This declaration throws an exception, but is not annotated with @Throws.
// Try adding the annotation.
void withRethrow() {
try {
...
} catch (_) {
rethrow;
}
}

✅ Good:

void fn() {
print('hi');
}

()
void withThrow() {
throw Error();
}

()
void withRethrow() {
try {
...
} catch (_) {
rethrow;
}
}