Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/abitofevrything/checked_exceptions
https://github.com/abitofevrything/checked_exceptions
Last synced: 25 days ago
JSON representation
- Host: GitHub
- URL: https://github.com/abitofevrything/checked_exceptions
- Owner: abitofevrything
- Created: 2024-03-02T17:11:02.000Z (10 months ago)
- Default Branch: main
- Last Pushed: 2024-05-15T13:25:40.000Z (7 months ago)
- Last Synced: 2024-05-16T02:39:15.503Z (7 months ago)
- Language: Dart
- Size: 102 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- 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();
}
}
```