throws 1.0.0-alpha.5
throws: ^1.0.0-alpha.5 copied to clipboard
An annotation for documenting and enforcing throwing functions.
throws #
Annotations for documenting and enforcing throwing functions.
Install #
Add the dependency to your pubspec.yaml.
Usage #
Annotate functions that can throw:
@Throws(reason: 'Parsing input failed', errors: {FormatException, RangeError})
int parsePositiveInt(String input) {
final value = int.parse(input);
if (value < 0) {
throw RangeError('Value must be non-negative');
}
return value;
}
You can also use the shorthand constant:
@throws
void mightThrow() {
throw Exception('x');
}
Expected errors #
Use errors to declare the error types callers should handle. This is a set of Type literals.
@Throws(reason: 'Reason', errors: {StateError})
int singleValue(Iterable<int> values) => values.single;