getMessage() 没有覆盖异常

getMessage() without overriding in Exception

我有一个扩展 Exception 的 class,但不允许我从 Exception 覆盖 getMessage(),所以我必须找到解决它的方法。

为了任务的可视化,这里有一个例子:

public class NoCertificateException extends Exception {

    private Student[] students;

public NoCertificateException(Student[] students) {
        this.students = students;

public String getMessage() {
        String arrayToString = Arrays.toString(students);
        String s =  " has/have no certificate(s)";
        return arrayToString + s;
}

但在这种情况下 getMessage() 被覆盖

Exception has a constructor which takes a string,然后用作消息。

因此,在您的构造函数中调用该构造函数:

public NoCertificateException(Student[] students) {
  super(Arrays.toString(students) + " has/have no certificate(s)");
}