alwaysThrows top-level constant
- @Deprecated("Use a return type of 'Never' instead")
Used to annotate a function f
. Indicates that f
always throws an
exception. Any functions that override f
, in class inheritance, are also
expected to conform to this contract.
Tools, such as the analyzer, can use this to understand whether a block of code "exits". For example:
@alwaysThrows toss() { throw 'Thrown'; }
int fn(bool b) {
if (b) {
return 0;
} else {
toss();
print("Hello.");
}
}
Without the annotation on toss
, it would look as though fn
doesn't
always return a value. The annotation shows that fn
does always exit. In
addition, the annotation reveals that any statements following a call to
toss
(like the print
call) are dead code.
Tools, such as the analyzer, can also expect this contract to be enforced; that is, tools may emit warnings if a function with this annotation doesn't always throw.
Deprecated: This annotation is deprecated and will be
removed in a future release of package:meta
.
After Dart 2.9, you can instead specify a return type of Never
to indicate that a function never returns.
Implementation
@Deprecated("Use a return type of 'Never' instead")
const _AlwaysThrows alwaysThrows = _AlwaysThrows();