Skip to main content

prefer-public-exception-classes

added in: 1.8.0
🛠
Pro+
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:

// LINT: Prefer exception classes to be accessible to external classes. Try making it public.
class _MyPrivateException implements Exception {}

// LINT: Prefer exception classes to be accessible to external classes. Try making it public.
class _MyPrivateException extends Exception {}

✅ Good:

class MyException implements Exception {}

class MyException extends Exception {}