All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.sonar.l10n.java.rules.squid.S1165.html Maven / Gradle / Ivy

There is a newer version: 8.6.0.37351
Show newest version

Exceptions are meant to represent the application's state at which an error occurred.

Making all fields final ensures that this state:

  • Will be fully defined at the same time the exception is instantiated.
  • Won't be updated or corrupted by some bogus error handler.

This will enable developers to quickly understand what went wrong.

The following code:

public class MyException extends Exception {

  private int status;                               // Noncompliant

  public MyException(String message) {
    super(message);
  }

  public int getStatus() {
    return status;
  }

  public void setStatus(int status) {
    this.status = status;
  }

}

should be refactored into:

public class MyException extends Exception {

  private final int status;                         // Compliant

  public MyException(String message, int status) {
    super(message);
    this.status = status;
  }

  public int getStatus() {
    return status;
  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy