Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to inherit a RuntimeException class?

I have two options:

public class SyntaxException extends RuntimeException {
  private String msg;
  public SyntaxException(String m) {
    this.msg = m;
  }
  public String getMessage() {
    return "Invalid syntax: " + this.msg;
  }
}

and

public class SyntaxException extends RuntimeException {
  public SyntaxException(String m) {
    super("Invalid syntax: " + m);
  }
}

Which one is preferred, if I have to think about code maintainability and extendability?

like image 961
yegor256 Avatar asked Jan 25 '26 05:01

yegor256


1 Answers

Use the second one. The argument to the constructor of both RuntimeException and your inherited class is the error message, so there's no reason to duplicate that functionality already given by RuntimeException in your code.

like image 103
FRotthowe Avatar answered Jan 27 '26 18:01

FRotthowe



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!