如何修复Java:26:错误:非法启动类型(抛出)

时间:2015-06-28 06:43:55

标签: java syntax-error throw throws

我是编写Java代码的新手,我很难理解投掷是如何工作的。 当我编译一部分与抛出有关的代码时,我一直都会遇到错误。我该如何解决这些问题?

我的代码:

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

我得到的错误是:

 BankAccount.java:26: error: illegal start of type
       throws NegativeAmountException
       ^
 BankAccount.java:26: error: ';' expected
       throws NegativeAmountException
             ^

3 个答案:

答案 0 :(得分:4)

您的deposit方法未正确定义。您的理解是正确的,它必须声明它抛出哪些已检查的异常,但您没有使用正确的语法。目前,您的代码如下所示:

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

throws子句是方法声明的一部分,因此之前实现:

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

答案 1 :(得分:1)

在第26行,您的声明无效。删除它。您可能忽略了它,因为它接近评论

答案 2 :(得分:0)

检查以下代码,删除了第26行并添加了所需的抛出。这应该适合你

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