如何修复Java:26:error:类型的非法开始(抛出)

如何修复Java:26:error:类型的非法开始(抛出),java,syntax-error,throw,throws,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;

我刚开始编写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());
    }
}

检查以下代码删除了第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());
           }
        }
class银行账户{
公共字符串名称;
公共双平衡;
public BankAccount(字符串名称,双倍余额)引发异常
{
this.name=名称;
this.balance=balance;//设置名称和余额
如果(余额<0){//请确保余额不是负值
抛出新的NegativeMountException(“无法创建金额为负数的帐户”);//如果余额为负数,则抛出异常
}
}
public BankAccount(字符串名称)引发异常
{
此(名称,0);//设置名称并使用0余额
}
//通过添加存款金额更新余额
//确保存款金额不为负数
//如果存款为负,则抛出异常
公共无效存款(双倍金额)引发异常{
如果(金额>0){
此余额+=金额;
}否则{
抛出新的NegativeAmountException(“存款金额不得为负”);
}
}
//通过减去取款金额更新余额
//如果资金不足,抛出异常
//确保取款金额不为负数
//如果金额为负,则抛出NegativeAmountException
//如果余额<金额,则抛出资金不足异常
公共作废取款(双倍金额)引发资金不足异常,NegativeAmountException{
如果(金额>getBalance()){
抛出新的InsufficientFundsException(“您没有足够的资金进行此操作”);
}否则,如果(金额<0){
抛出新的NegativeAmountException(“取款金额不得为负数”);
}否则{
此余额-=金额;
}
}
//返回当前余额
公共双getBalance(){
还这个。结余;
}
//打印银行对账单,包括客户姓名
//经常账户余额
公共声明(){
System.out.println(“Balance for”+this.name+:“+getBalance());
}
}

第26行的语句无效。删除那个。您可能忽略了它,因为它靠近注释

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

public void deposit(double amount) {
    // Implementation of the method
}
throws NegativeAmountException
throws
子句是方法声明的一部分,因此应该在其实现之前出现:

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

然后重新格式化代码,使其更易于阅读;必须捕获或声明抛出新的NegativeAmountException(“存款金额不得为负”);什么是<代码>在无处抛出负的异常异常/代码?所以也要修复它。说真的,伙计,如果你一直要求别人帮你修复编译错误,你就什么也学不到。如果我删除第26行,新的错误是get says:BankAccount.java:23:error:unreported exception NegativeAmountException;必须捕获或声明抛出新的NegativeAmountException(“存款金额不得为负”);
public void deposit(double amount) throws NegativeAmountException {
    // Implementation of the method
}