prefer_correct_error_name
Flags a class implementing Exception or extending Error that is not named with the matching suffix.
Dart draws a real line between the two: an Exception is a condition the caller is expected to handle, an Error is a bug the caller should not catch. The name is where that distinction shows up at the catch site.
This rule is in the pedantic preset, and works with its defaults — no configuration needed.
class NotFound implements Exception { // LINT: implements Exception, does not end in `Exception` const NotFound(this.id);
final String id;}
class BadState extends Error {} // LINT: extends Error, does not end in `Error`class NotFoundException implements Exception { const NotFoundException(this.id);
final String id;}
class BadStateError extends Error {}Examples
Section titled “Examples”An indirect ancestor still counts
Section titled “An indirect ancestor still counts”A class that reaches Exception through your own base class is checked the same way:
class ApiException implements Exception {}
// Don'tclass RateLimited extends ApiException {} // LINT
// Doclass RateLimitedException extends ApiException {}A class that is neither is left alone
Section titled “A class that is neither is left alone”The rule only looks at classes that actually reach Exception or Error. A plain result type is never reported, however it is named:
// Not reported — implements neitherclass NotFound { const NotFound(this.id);
final String id;}Error wins when a class is both
Section titled “Error wins when a class is both”A class can implement Exception and extend Error. The rule asks for the Error suffix, because “do not catch this” is the stricter reading:
// Acceptedclass WeirdError extends Error implements Exception {}Accepting a house vocabulary
Section titled “Accepting a house vocabulary”Many codebases name their handled conditions Failure rather than Exception. Add it to allow_suffixes and both spellings pass, without giving up the check on classes that end in neither:
many_lints: rules: prefer_correct_error_name: allow_suffixes: [Failure]rules: prefer_correct_error_name: allow_suffixes: [Failure]// Both acceptedclass NetworkFailure implements Exception {}class NotFoundException implements Exception {}
// Still reported — ends in neitherclass Timeout implements Exception {} // LINTReplacing the suffix outright
Section titled “Replacing the suffix outright”Set exception_suffix when the house word is the only one you want:
rules: prefer_correct_error_name: exception_suffix: Failure// Don't — `Exception` is no longer the accepted endingclass NotFoundException implements Exception {} // LINT
// Doclass NotFoundFailure implements Exception {}Enabling this rule
Section titled “Enabling this rule”This rule is in the pedantic preset, so it is enabled by preset: pedantic or by name:
rules: prefer_correct_error_name: enabled: trueOptions
Section titled “Options”| Option | Type | Default | Description |
|---|---|---|---|
exception_suffix |
string | Exception |
Required ending for an Exception class |
error_suffix |
string | Error |
Required ending for an Error class |
allow_suffixes |
list | [] |
Additional endings accepted alongside the required one, for both kinds |
Known limitations
Section titled “Known limitations”Classes only. A mixin or extension type implementing Exception is not checked.
No quick fix. Renaming a thrown type touches every catch and every on clause that names it, which is a rename refactoring rather than a one-file edit.
Turning this rule off
Section titled “Turning this rule off”To disable this rule:
rules: prefer_correct_error_name: falseTo keep the rule on but skip certain paths, use per-rule exclude.
Related rules
Section titled “Related rules”prefer_correct_callback_field_name— Name callbacks onSomething, the way Flutter does.prefer_correct_handler_name— Name event handlers after the event they answer.prefer_correct_setter_parameter_name— Use one parameter name in every setter.prefer_boolean_prefixes— Name booleans as questions.