nullable_ type_ in_ catch_ clause
A potentially nullable type can't be used in an 'on' clause because it isn't valid to throw a nullable expression.
Description
#
The analyzer produces this diagnostic when the type following
on
in a
catch
clause is a nullable type. It isn't valid to specify a nullable
type because it isn't possible to catch
null
(because it's a runtime
error to throw
null
).
Example
#
The following code produces this diagnostic because the exception type is
specified to allow
null
when
null
can't be thrown:
void f() {
try {
// ...
} on FormatException? {
}
}
Common fixes
#Remove the question mark from the type:
void f() {
try {
// ...
} on FormatException {
}
}
Unless stated otherwise, the documentation on this site reflects Dart 3.9.2. Page last updated on 2025-9-4. View source or report an issue.