Skip to content

prefer_correct_error_name

v1.0.0WarningConfigurableClass Naming

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

This rule is in the pedantic preset, so it is enabled by preset: pedantic or by name:

many_lints.yaml
rules:
prefer_correct_error_name:
enabled: true
analysis_options.yaml
many_lints:
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)

To disable this rule:

many_lints.yaml
rules:
prefer_correct_error_name: false

To keep the rule on but skip certain paths, use per-rule exclude.