How to inherit class RuntimeException?

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 preferable if I need to think about code support and extensibility?

+3
source share
4 answers

Use the second one. The argument to the constructor of both RuntimeExceptionyou and your inherited class is the error message, so there is no reason to duplicate this functionality already specified RuntimeExceptionin your code.

+6
source

Im going with number 2. Number one looks like reinventing the wheel.

, YAGNI KISS, , getMessage() msg . , - , , " / ".

getMessage(), .

+2

.

" :" , :

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

, i18n .

+1

. , .

0

Source: https://habr.com/ru/post/1775216/


All Articles