Skip to main content

prefer-public-exception-classes

added in: 1.8.0
preset: recommended

Warns when an exception class declaration is not public.

If a private exception is thrown, the external code will have to catch instances of the nearest accessible parent class. In this case, it becomes more difficult to handle specific exceptions, because the external code will not be able to clearly identify the problem that has arisen.

Lack of clear identification of exceptions poses an additional security risk because some specific exceptions may require specific handling rather than general handling.

Example

❌ Bad:

class _MyPrivateException implements Exception {} // LINT

class _MyPrivateException extends Exception {} // LINT

✅ Good:

class MyException implements Exception {}

class MyException extends Exception {}