Java 使用超级构造函数创建异常

Java 使用超级构造函数创建异常,java,exception,super,Java,Exception,Super,我有一个自定义异常类,如下所示: public abstract class AccountException extends BankServiceException { private int accountNo; public AccountException(String message, int accountNo) { super(message); this.accountNo = accountNo; } public int getAccountNo()

我有一个自定义异常类,如下所示:

public abstract class AccountException extends BankServiceException {

private int accountNo;

public AccountException(String message, int accountNo) {
    super(message);
    this.accountNo = accountNo;

}

public int getAccountNo() {
    return accountNo;
}
现在我需要将这个抽象类扩展到一个更具体的异常:

public class  OverdraftLimitReachedException extends AccountException {

private double amount;
private double overdraft;

public OverdraftLimitReachedException(int accountNo,double amount,double overdraft) {
 super("The overdraft limit has been exceded",accountNo,amount,overdraft);
    this.overdraft = overdraft;
    this.amount = amount;
}

public double getAmount() {
    return amount;
}

public double getOverdraft() {
    return overdraft;
}
现在这里的问题是超级构造函数,我知道我需要给它参数数量和透支,但当我试图编译它时,它说参数在长度上不同。我如何正确调用超级构造函数,以便我可以给出错误消息。谢谢


编辑:我已经添加了构造函数的实际外观(抱歉,我有点笨拙)。在代码的后面,当我实际抛出异常时,我需要给出构造函数的参数。

在代码中,您使用以下命令调用超级构造函数:

super("The overdraft limit has been exceded",accountNo,amount,overdraft);
这将传递4个参数:

  • “透支限额已超出”
    字符串
  • accountNo
    int
  • 金额
    双倍
  • 透支
    双倍
您的超级构造函数是这样编写的:

public AccountException(String message, int accountNo) {
这接受两个参数:

  • 一个
    字符串
  • 一个
    int
您需要使两个参数列表匹配。您需要将对super的调用编辑为:

super("The overdraft limit has been exceded", accountNo);
您需要将超级构造函数编辑为:

public AccountException(String message, int accountNo, double amount, double overdraft) {
我怀疑您想使用前者,因为您已经将
金额
透支
与此代码一起使用:

this.overdraft = overdraft;
this.amount = amount;

还有一个小错误,但您在邮件中拼错了单词“excelled”。

在您的代码中,您使用以下命令调用超级构造函数:

super("The overdraft limit has been exceded",accountNo,amount,overdraft);
这将传递4个参数:

  • “透支限额已超出”
    字符串
  • accountNo
    int
  • 金额
    双倍
  • 透支
    双倍
您的超级构造函数是这样编写的:

public AccountException(String message, int accountNo) {
这接受两个参数:

  • 一个
    字符串
  • 一个
    int
您需要使两个参数列表匹配。您需要将对super的调用编辑为:

super("The overdraft limit has been exceded", accountNo);
您需要将超级构造函数编辑为:

public AccountException(String message, int accountNo, double amount, double overdraft) {
我怀疑您想使用前者,因为您已经将
金额
透支
与此代码一起使用:

this.overdraft = overdraft;
this.amount = amount;
还有一个小错误,但您在邮件中拼错了“超出”一词。

此呼叫

super("The overdraft limit has been exceded", accountNo , amount, overdraft);
无法工作,因为超级构造函数只有2个参数

AccountException(String message, int accountNo)
我不确定您想做什么,但是如果您想在消息中添加金额和透支,您可以这样做,它只传入
字符串和
int

super("The overdraft limit has been exceded by " + 
      overdraft + ". You had " + amount + 
      " dollars left in your account.", 
      accountNo);
你不能指望你的
AccountException
超类知道透支——这被认为是糟糕的设计。相反,您的
OverdraftLimitReacheException
类必须生成自己的专用消息。

此调用

super("The overdraft limit has been exceded", accountNo , amount, overdraft);
无法工作,因为超级构造函数只有2个参数

AccountException(String message, int accountNo)
我不确定您想做什么,但是如果您想在消息中添加金额和透支,您可以这样做,它只传入
字符串和
int

super("The overdraft limit has been exceded by " + 
      overdraft + ". You had " + amount + 
      " dollars left in your account.", 
      accountNo);

你不能指望你的
AccountException
超类知道透支——这被认为是糟糕的设计。相反,您的
OverdraftLimitReachedException
类必须生成自己的专用消息。

您的代码似乎工作正常-您正在给超级构造函数,它包含2个参数,2个参数。您可以发布编译器错误的确切文本吗?这是给您带来问题的代码吗,或者实际的构造函数调用类似于
super(“透支限制已超出”,accountNo,overdraft)
?好吧,对不起,伙计们,我实际上只复制了我的构造函数的一半。我现在已经发布了整个构造函数。错误表明找到了2个额外的双精度值。您的代码似乎工作正常-您正在给超级构造函数,它包含2个参数,2个参数。您可以发布编译器错误的确切文本吗?这是给您带来问题的代码,还是实际的构造函数调用类似于
super(“透支限额已超出”,账户号,透支)
?好的,很抱歉,伙计们,我实际上只复制了我的构造函数的一半。我现在发布了整个构造函数。错误显示找到了两个额外的双精度值。实际上,我只想在屏幕上显示的消息中添加透支和金额。我希望用户能够在错误中看到透支和金额值信息。感谢语法提示,英语不是我的第一语言,总是需要帮助。干杯!实际上我只想在屏幕上显示的信息中添加透支和金额。我想让用户能够在错误信息中看到透支和金额值。感谢语法提示,英语不是我的fir永远需要st语言和帮助。干杯!