https://github.com/abitofevrything/checked_exceptions
https://github.com/abitofevrything/checked_exceptions
Last synced: 5 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/abitofevrything/checked_exceptions
- Owner: abitofevrything
- Created: 2024-03-02T17:11:02.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2025-01-07T20:28:24.000Z (6 months ago)
- Last Synced: 2025-01-07T21:26:59.346Z (6 months ago)
- Language: Dart
- Size: 102 KB
- Stars: 1
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# checked_exceptions
An analyzer plugin adding support for checked exceptions to Dart.
Checked exceptions are a way to statically declare which errors might be thrown by a function and check that they are handled at compile time. Doing so can allow developers to know in advance if their code may crash unexpectedly.
## Installation
Install `checked_exceptions_annotations` as a normal dependency, then install `checked_exceptions` and `custom_lint` as dev dependencies:
```
$ dart pub add checked_exceptions_annotations
$ dart pub add -d checked_exceptions custom_lint
```Finally, add `custom_lint` to your analyzer plugins by adding the following to your `analysis_options.yaml`:
```yaml
analyzer:
plugins:
- custom_lint
```## Usage
Most developers will want to ensure their functions to _not_ throw any errors. For that, they can annotate the function with `@safe`, and `checked_exceptions` will warn them if their code throws any errors:
```dart
@safe
void mySafeFunction() {
throw Exception(); // LINT
}
```If your function intentionally throws exceptions, you can annotate it with `@Throws` to indicate which types of exceptions it may throw:
```dart
@Throws()
void checkFormat(String source) {
if (!hasCorrectFormat(source)) {
throw FormatException();
}
}
```