class FileChangedException extends Exception { }
main() { }
=>
Analyzing [test.dart]...
[error] The generative constructor 'Exception([dynamic message]) → Exception' expected, but factory found (/usr/local/google/home/ianh/dev/scratch/test.dart, line 1, col 7)
1 error found.
The error message makes no sense here. What factory is found?
I have no idea.
What it means is that the implicit zero argument constructor in FileChangedException has an implicit call to super(), but Exception doesn't have a zero argument constructor that matches.
doesn't Exception have a constructor with an optional argument?
Yes, it does. It's defined as
factory Exception([var message]) => new _ExceptionImplementation(message);
But the specification (section 10.6.1) says:
Let _C_ be the class in which the superinitializer appears and let _S_ be the superclass of _C_.
...
It is a compile-time error if class _S_ does not declare a generative constructor named _S_ (respectively _S.id_).
Invoking a factory constructor in the superclass won't initialize the instance of the subclass, it would create a new object that would immediately be discarded. Because that would be pointless it's disallowed.
The error message is horrible, and needs to be fixed.
That said, the lack of a generative constructor in Exception means that you can't extend it, you can only implement it:
class FileChangedException implements Exception { }
Turns out this is a duplicate of https://github.com/dart-lang/sdk/issues/22382 which I filed a year earlier.
I discovered this when I ran into this same exception again and Googled the message and found both bugs I'd filed before.
So I've now been completely confused by this same analyzer warning three times in 18 months.
So I've now been completely confused by this same analyzer warning three times in 18 months.
Still causing confusion 3 years later, this time on the Flutter Boring Show - immediately thought of this comment!
Most helpful comment
Turns out this is a duplicate of https://github.com/dart-lang/sdk/issues/22382 which I filed a year earlier.
I discovered this when I ran into this same exception again and Googled the message and found both bugs I'd filed before.
So I've now been completely confused by this same analyzer warning three times in 18 months.