Skip to content

private_exception_thrown

Reports a private exception class that its library throws but never tells apart by type. Make it public, so callers can catch it.

Tier
advisory
Scope
Dart
Lowest preset
strict
Fix
none

A private class that implements Exception of dart:core, that its library creates, and that no file of the library tells apart by its type: no on _X catch clause, no is _X test, no pattern that names _X. The violation covers the class name.

It does not report:

  • a private exception that also extends or implements a public exception type (a StoreException of its package, SocketException), which code outside can catch by that type;
  • a subclass of Error, which marks a mistake in the program and is not meant to be caught by type;
  • a class that is never created.

Every file of the library is searched, its parts read from their text, and creations and tests are matched by the class name. Where a part cannot be read, nothing is reported.

A thrown object leaves the library; a private name does not. Code outside can only catch everything and read the error’s text, so a failure the library took the trouble to describe gets no type where it is handled. An exception the library catches by its own type stays inside, for its own control flow, and is not reported.

Violating
class _QuotaExceeded implements Exception {}
void upload(int size) {
if (size > limit) throw _QuotaExceeded();
}
Corrected
class QuotaExceeded implements Exception {}
void upload(int size) {
if (size > limit) throw QuotaExceeded();
}

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.

Write why on the line directly above the suppression:

// Why it stays as written here.
// ignore: private_exception_thrown

// ignore_for_file: assay/private_exception_thrown silences it in a whole file, and the prefixed // ignore: assay/private_exception_thrown works too. See Suppressions.