如何访问异常类变量?

时间:2018-04-08 19:38:51

标签: java exception exception-handling

我正在为Employee Service类实现自定义Exception。现在想要Exception类变量的getter函数。

package com.sentienz.service.exception;

public class EmployeeServiceCustomException extends Exception {

    private final int code;
    private final String genericmessage;

    public EmployeeServiceCustomException(int code, String genericmessage) {
        super();
        this.code = code;
        this.genericmessage = genericmessage;
    }

    public EmployeeServiceCustomException(String message, Throwable cause, int code, String genericmessage) {
        super(message, cause);
        this.code = code;
        this.genericmessage = genericmessage;
    }

    public EmployeeServiceCustomException(String message, int code, String genericmessage) {
        super(message);
        this.code = code;
        this.genericmessage = genericmessage;
    }

    public EmployeeServiceCustomException(Throwable cause, int code, String genericmessage) {
        super(cause);
        this.code = code;
        this.genericmessage = genericmessage;
    }

    public int getCode() {
        return this.code;
    }

    public String getGenericmessage() {
        return this.genericmessage;
    }



}

实际上,我想要这个,但我无法做到这一点。是java异常类有这个问题的所有私有变量或其他一些原因。

 public int getMessage() {
        return this.message;
    }

1 个答案:

答案 0 :(得分:2)

没有必要编写自己的getMessage,因为父类Throwable.getMessage()中已有一个 - 您无论如何都会继承它。