1

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.

4
  • 1
    Your code seems to work fine - you're giving the super constructor, which takes 2 parameters, 2 arguments
    – user
    May 30, 2020 at 22:58
  • Can you post the exact text of the compiler error?
    – Abra
    May 30, 2020 at 23:00
  • Is this the code that gives you a problem, or is the actual constructor call something like super("The overdraft limit has been excedeed",accountNo, overdraft)?
    – user
    May 30, 2020 at 23:03
  • Okay I'm sorry guys I actually copied only half of my constructor.I've posted the entire constructor now. The error says that 2 extra double values have been found.
    – cody1
    May 30, 2020 at 23:04

2 Answers 2

5

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.

1
  • Actually all I wanted is to add overdraft and amount to the message that comes out on the screen. I wanted to be able the user to see the overdraft and amount value in the error message. And thanks for the grammar tip, english is not my first language and help is always needed. Cheers!
    – cody1
    May 30, 2020 at 23:24
1

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.

0

Not the answer you're looking for? Browse other questions tagged or ask your own question.