InsufficientFundsException

InsufficientFundsException

Closed. This question is off-topic。它当前不接受答案。
                            
                        
                    
                
                            
                                
                
                        
                            
                        
                    
                        
                            想改善这个问题吗? Update the question,所以它是on-topic,用于堆栈溢出。
                        
                        5年前关闭。
                                                                                            
                
        
我试图做所有的日食快速修复,但由于某些原因,它仍然
当我尝试使用我的测试文件夹进行编译时,给我一条错误消息

它一直告诉我“ Syntax error on token "InsufficientFundsException", VariableDeclaratorId expected after this

而且我不知道这意味着什么,我所需要做的例外就是测试我的余额是否变为负数,以及吐出我所欠的金额是否为负数。

public class BankAccount
{
    // Variables that are private so there is no recounts
    private static int nextID = 100;
    private static int newID() { return nextID++; }
    // Instance variables
    private double balance;
    private int id;
    // Constructors
    public BankAccount()
    {
        this(0);
    }
    public BankAccount(int initBal)
    {
        balance = initBal;
        id      = newID();
    }
    // Instance (non-static) methods
    public double    getBalance()
    {
        return balance;
    }
    public int    getAccountNumber()
    {
        return id;
    }
    public double getAmount()
    {
        return testAmount;
    }
    public void   deposit (double depositAmount)
    {
        balance += depositAmount;
    }
    public void   withdraw(double amount) throws InsufficientFundsException
    {
        balance -= amount;
        double difference;
        if (balance < balance-amount)
        difference = amount - balance;
        throw new InsufficientFundsException();
        try
        {
            double nbalance = (balance - amount);
        }
        catch (InsufficientFundsException)
        {
            System.out.println("You do not have enough for this transaction , you are short by " + difference);
        }
    }
}
public class InsufficientFundsException extends Exception
{
    public InsufficientFundsException() throws InsufficientFundsException
    {
        throw new InsufficientFundsException();
    }
    public double getAmount()
    {
        return getAmount();
    }
}

最佳答案

之后的代码

throw new InsufficientFundsException();

是无效代码。

另外其他代码完全是错误的

例如。

InsufficientFundsException构造函数自行抛出。

catch (InsufficientFundsException)是错误的。它需要一个变量。

double nbalance = (balance - amount);永远不会抛出InsufficientFundsException异常。

getAmount()只是递归地调用自身,没有逻辑。

关于java - 为什么我的自定义异常(exception)无效? Java ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22031838/

10-11 21:28