Checked exceptions for Dart. Declare which exceptions your functions can throw and get static analysis warnings when they aren't handled.
| Package | Description |
|---|---|
| throwable | Annotation for declaring thrown exception types |
| throwable_lints | Lint rules that enforce exception handling |
Add throwable to your pubspec.yaml:
dependencies:
throwable: ^1.0.0-alpha.2Enable the plugin in your analysis_options.yaml:
plugins:
throwable_lints:
path: path/to/throwable_lints
diagnostics:
unhandled_exception_call: true # warning/error
unhandled_throw_in_body: true # warning/error
Annotate functions with @Throws to declare their exceptions:
import 'package:throwable/throwable.dart';
@Throws([NetworkException])
Future<String> fetchData(String url) async {
// ...
}Callers must then handle or propagate the declared exceptions:
// OK: handled
void main() {
try {
fetchData('https://example.com');
} on NetworkException catch (_) {
// handle error
}
}
// OK: propagated
@Throws([NetworkException])
void main() {
fetchData('https://example.com');
}
// LINT: NetworkException not handled
void main() {
fetchData('https://example.com');
}unhandled_throw_in_body- Reportsthrow/rethrowexpressions not caught locally or declared via@Throwsunhandled_exception_call- Reports calls to@Throws-annotated functions where exceptions aren't handled or propagated