Skip to main content

avoid-non-final-exception-class-fields

added in: 1.22.0
🛠
Pro+

Warns when an exception class declaration has non-final fields.

Exception objects should not be modified as this may result in losing the error context/data for later debugging and logging.

Example

❌ Bad:

class CaughtException implements Exception {
final Object exception;

final String? message;

StackTrace stackTrace; // LINT: Avoid non-final fields in exceptions. Try making it final.

CaughtException(Object exception, StackTrace stackTrace)
: this.withMessage(null, exception, stackTrace);

CaughtException.withMessage(this.message, this.exception, this.stackTrace);
}

✅ Good:

class CaughtException implements Exception {
final Object exception;

final String? message;

final StackTrace stackTrace; // Correct, now final

CaughtException(Object exception, StackTrace stackTrace)
: this.withMessage(null, exception, stackTrace);

CaughtException.withMessage(this.message, this.exception, this.stackTrace);
}