Skip to main content

avoid-non-final-exception-class-fields

has auto-fix
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;

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

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);
}