2

I'm new to writing Java code and I'm having a hard time understand how throwing works. I keep getting errors when I compile a portion of my code that has to do with throwing. How do I fix these?

My code:

class BankAccount {
    public String name;
    public double balance;

    public BankAccount(String name, double balance) throws NegativeAmountException {
        this.name = name;
        this.balance = balance; // set name and balance
        // throw exception if balance is negative
        if (balance < 0) { // make sure balance is not negative
            throw new NegativeAmountException(
                    "Can not create an account with a negative amount.");
        }
    }

    public BankAccount(String name) throws NegativeAmountException {
        this(name, 0); // set name and use 0 balance
    }

    // update balance by adding deposit amount
    // make sure deposit amount is not negative
    // throw exception if deposit is negative
    public void deposit(double amount) {
        if (amount > 0) {
            this.balance += amount;
        } else {
             throw new NegativeAmountException("Deposit amount must not be negative");
        }
    }

     throws NegativeAmountException
    // update balance by subtracting withdrawal amount
    // throw exception if funds are not sufficient
    // make sure withdrawal amount is not negative
    // throw NegativeAmountException if amount is negative
    // throw InsufficientFundsException if balance < amount
    public void withdraw(double amount) throws InsufficientFundsException,
            NegativeAmountException {
        if (amount > getBalance()) {
            throw new InsufficientFundsException(
                    "You do not have sufficient funds for this operation.");
        } else if (amount < 0) {
            throw new NegativeAmountException(
                    "Withdrawal amount must not be negative.");
        } else {
            this.balance -= amount;
        }
    }

    // return current balance
    public double getBalance() {
        return this.balance;
    }

    // print bank statement including customer name
    // and current account balance
    public void printStatement() {
        System.out.println("Balance for " + this.name + ": " + getBalance());
    }
}

The errors I get are:

 BankAccount.java:26: error: illegal start of type
       throws NegativeAmountException
       ^
 BankAccount.java:26: error: ';' expected
       throws NegativeAmountException
             ^
Turing85
  • 18,217
  • 7
  • 33
  • 58
  • 1
    And then reformat your code to make it a lot easier to read. – Jon Skeet Jun 28 '15 at 06:46
  • If I delete line 26 I get a new error that says: – BeachLover8 Jun 28 '15 at 06:47
  • BankAccount.java:23: error: unreported exception NegativeAmountException; must be caught or declared to be thrown throw new NegativeAmountException("Deposit amount must not be negative"); – BeachLover8 Jun 28 '15 at 06:47
  • 2
    What is `throws NegativeAmountException` in the middle of nowhere? – Maroun Jun 28 '15 at 06:48
  • 2
    So fix that one too. Seriously dude, you won't learn anything if you keep on asking other people to fix your compilation errors for you. – Stephen C Jun 28 '15 at 06:49
  • Why do you use Exceptions, for normal program results? – Jurrian Fahner Jun 28 '15 at 06:50
  • If you're learning Java, you should start small -- smaller than this. Write a few lines. Then add an exception and a throws clause. Then add another method. Work bit by bit, and as soon as you hit a compilation error, stop right there and figure out how to fix it. Otherwise you're looking at a big mess of code and thinking, "_something's_ wrong, but what is it?" If you work in small bits, the answer is easy: "whatever the small bit that I just changed is, that's what's broken." – yshavit Jun 28 '15 at 07:06

3 Answers3

4

Your deposit method is not defined correctly. You are right in your understanding that it must declare which checked exceptions it throws, but you are not using the correct syntax. Currently, your code looks like this:

public void deposit(double amount) {
    // Implementation of the method
}
throws NegativeAmountException

The throws clause is part of the method's declaration, and as such should come before its implementation:

public void deposit(double amount) throws NegativeAmountException {
    // Implementation of the method
}
Mureinik
  • 297,002
  • 52
  • 306
  • 350
1

On line 26 you have an invalid statement. Delete that. You may have overlooked it because it was near comments

Logan M
  • 348
  • 2
  • 12
  • If I remove line 26 the new error is get says: BankAccount.java:23: error: unreported exception NegativeAmountException; must be caught or declared to be thrown throw new NegativeAmountException("Deposit amount must not be negative"); – BeachLover8 Jun 28 '15 at 06:49
0

check the following code removed the line 26 and added the required throws .This should work for you

        class BankAccount {
            public String name;
            public double balance;
            public BankAccount(String name, double balance) throws NegativeAmountException 
            {
                this.name = name; 
        this.balance = balance; // set name and balance 
                if (balance < 0) { // make sure balance is not negative
                    throw new NegativeAmountException("Can not create an account with a negative amount."); // throw exception if balance is negative
                        }
            }
            public BankAccount(String name) throws NegativeAmountException 
            {
                this(name, 0); // set name and use 0 balance 
            }
            // update balance by adding deposit amount
            // make sure deposit amount is not negative 
            // throw exception if deposit is negative 
            public void deposit(double amount) throws NegativeAmountException {
            if ( amount > 0) {
            this.balance += amount;
        } else {
            throw new NegativeAmountException("Deposit amount must not be negative");
        }
        }
            // update balance by subtracting withdrawal amount
        // throw exception if funds are not sufficient
        // make sure withdrawal amount is not negative 
        // throw NegativeAmountException if amount is negative 
        // throw InsufficientFundsException if balance < amount
            public void withdraw(double amount) throws InsufficientFundsException, NegativeAmountException {
            if (amount > getBalance()) {
                throw new InsufficientFundsException("You do not have sufficient funds for this operation.");
        } else if (amount < 0) {
            throw new NegativeAmountException("Withdrawal amount must not be negative.");
        } else {
            this.balance -= amount;
        }
        }
            // return current balance
            public double getBalance() {
                return this.balance;
        }
            // print bank statement including customer name
        // and current account balance
            public void printStatement() {
                System.out.println("Balance for " + this.name + ": " + getBalance());
           }
        }
KDP
  • 1,481
  • 7
  • 13