prefer_correct_error_name
This rule 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 is visible at the call site — a reader deciding whether to write a catch should not have to open the declaration to find out which kind it is.
A class that is neither is never reported. When a class is both, Error wins: the stricter reading of “do not catch this” is the one worth naming for.
This rule is in the pedantic preset.
class NotFound implements Exception {}class BadState extends Error {}class NotFoundException implements Exception {}class BadStateError extends Error {}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”many_lints: rules: prefer_correct_error_name: exception_suffix: Exception error_suffix: Error allow_suffixes: []rules: prefer_correct_error_name: exception_suffix: Exception error_suffix: Error allow_suffixes: []| 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 default (Failure is the common one) |
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.