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

huangapple go评论75阅读模式
英文:

Creating exceptions with super constructors

问题

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

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 exceeded", accountNo);
        this.overdraft = overdraft;
        this.amount = amount;
    }

    public double getAmount() {
        return amount;
    }

    public double getOverdraft() {
        return overdraft;
    }
}

现在问题在于 super 构造函数。我知道我需要将参数 amount 和 overdraft 也传递给它,但是当我尝试编译时,它提示参数长度不同。我该如何正确调用 super 构造函数,以便能够输出错误消息呢?谢谢!

编辑:我已经添加了构造函数实际的样子(抱歉,我有点笨手笨脚)。稍后在代码中当我实际抛出异常时,我需要传递构造函数的参数。

英文:

I have a custom exception class like the following :

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;
}

Now I need to extend this abstract class to a more specific exception:

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;
}

Now the problem here is the super constructor,I am aware that I need to give it the parameter amount and overdraft as well but when I try to compile it says that the arguments differentiate in length.How do I call the super constructor corectlly, so that I can give out the error message.Thank you!

EDIT:I have added how the constructor actually looks like (sorry I'm a bit clumsy).Later in the code when I actually throw the exception I need to give the parameters of the constructor.

答案1

得分: 5

在你的代码中,你调用了超级构造函数:

super("The overdraft limit has been exceded", accountNo, amount, overdraft);

这传递了4个参数:

  • "The overdraft limit has been exceded" (String)
  • accountNo (int)
  • amount (double)
  • overdraft (double)

你的超级构造函数写成了这样:

public AccountException(String message, int accountNo) {

这接受了2个参数:

  • 一个 String
  • 一个 int

你需要使这两个参数列表匹配起来。要么你需要编辑调用超级构造函数的部分为:

super("The overdraft limit has been exceded", accountNo);

要么 你需要编辑超级构造函数为:

public AccountException(String message, int accountNo, double amount, double overdraft) {

我怀疑你想使用前者,因为你已经在这段代码中使用了 amountoverdraft

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

另外,一个小错误,但是你在消息中拼错了单词 "exceeded"。

英文:

In your code you call the superconstructor with:

super("The overdraft limit has been exceded",accountNo,amount,overdraft);

This passes in 4 arguments:

  • "The overdraft limit has been exceded" (String)
  • accountNo (int)
  • amount (double)
  • overdraft (double)

Your superconstructor is written like this:

public AccountException(String message, int accountNo) {

This accepts 2 arguments:

  • A String
  • An int

You need to make the two argument lists match. Either you need to edit the call to super to be:

super("The overdraft limit has been exceded", accountNo);

OR you need to edit the superconstructor to be:

public AccountException(String message, int accountNo, double amount, double overdraft) {

I suspect you want to use the former because you already use amount and overdraft with this code:

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

Also, a minor mistake, but you misspelled the word "exceeded" in your message.

答案2

得分: 1

This call

super("The overdraft limit has been exceeded", accountNo , amount, overdraft);

won't work because your super constructor only has 2 parameters.

AccountException(String message, int accountNo)

I'm not sure what you wanted to do, but if you wanted to add the amount and overdraft in the message, you can do something like this, which only passes in a String and an int.

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

You can't expect your AccountException superclass to know about overdrafts - that's considered bad design. Instead, your OverdraftLimitReachedException class has to make its own specialized message.

英文:

This call

super("The overdraft limit has been exceded", accountNo , amount, overdraft);

won't work because your super constructor only has 2 parameters.

AccountException(String message, int accountNo)

I'm not sure what you wanted to do, but if you wanted to add the amount and overdraft in the message, you can do something like this, which only passes in a String and an int.

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

You can't expect your AccountException superclass to know about overdrafts - that's considered bad design. Instead, your OverdraftLimitReachedException class has to make its own specialized message.

huangapple
  • 本文由 发表于 2020年5月31日 06:56:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/62109610.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定