missing_throw
Reports an error or exception created as a statement of its own and dropped. Write throw before it.
- Tier
- blocking
- Scope
- Dart
- Lowest preset
- recommended
- Fix
- writes
throwbefore the creation
What it reports
Section titled “What it reports”A statement like ArgumentError.value(count);. The throw was forgotten, so nothing is thrown, and the code goes on as if the check had passed.
Only an object of the core Error or Exception, or of a subtype of one of them, counts, and only where its creation is the whole statement. Any other object created and dropped, such as a Timer or an object that registers itself, may be made for what its constructor does, and is not reported.
A guard that does not throw lets bad input through, to do damage later, far from the check that should have stopped it. The SDK lint unnecessary_statements does not report a creation, since a constructor may do work.
Example
Section titled “Example”void withdraw(int amount) { if (amount <= 0) { ArgumentError.value(amount, 'amount'); } _balance -= amount;}void withdraw(int amount) { if (amount <= 0) { throw ArgumentError.value(amount, 'amount'); } _balance -= amount;}Rule parameters
Section titled “Rule parameters”Set under the assay: key of the project’s root analysis_options.yaml.
| Rule parameter | Default | Effect |
|---|---|---|
enabled |
true |
false switches the rule off for the whole project. |
exclude |
[] |
Path globs where the rule is silent. |
Suppressing it
Section titled “Suppressing it”Write why on the line directly above the suppression:
// The error is made only to warm up its stack trace capture.// ignore: missing_throwStateError('warm up');// ignore_for_file: assay/missing_throw silences it in a whole file, and the prefixed // ignore: assay/missing_throw works too. See Suppressions.