创建用户定义的异常 class-获取 null 作为答案(JAVA)

Creating user defined exception class-Getting null as answer(JAVA)

我尝试实现以下问题,但在尝试将值传递给方法时得到的答案是 null。取款时计算新余额后好像不能传值给方法transfer

账户class:

package Number3;

public class Account {

private int balance;
private int maxTransfer;

public Account()
{
    balance = 0;
    maxTransfer = 0;
}

public Account(int bal,int maxT)
{
    balance = bal;
    maxTransfer = maxT;
}

public void setBalance(int b)
{
    balance = b;
}

public int getBalance()
{
    return balance;
}

public void setMaxTransfer(int m)
{
    this.maxTransfer = m;
}

public int getMaxTransfer()
{
    return maxTransfer;
}

public void withdraw(int amount) throws insufficientFundException
{
    if(!(amount < balance))
    {
        balance = balance - amount;
    }
    else
    {
        throw new insufficientFundException();
    }
}

public void transfer(int amount)
{
    balance = amount + balance;
}

public void display()
{
    System.out.println("Current balance: "+getBalance());
}
}

主测试账号:

package Number3;
import java.util.Scanner;

public class testAccount{

public static void main(String[] args) {

    try{
    Account a = new Account();
    a.setBalance(1000);
    a.setMaxTransfer(10000);
    int choice,amount,acc;

    Scanner input = new Scanner(System.in);

    a.display();

    System.out.println("1. Withdraw \t\t 2. Transfer");
    System.out.println("Choose either 1 or 2");
    choice = input.nextInt();

    switch(choice)
    {
    case 1:
    {
        System.out.println("Enter amount to withdraw: ");
        amount = input.nextInt();
        a.withdraw(amount); //where i assume the error is
        a.getBalance();
        break;
    }
    case 2:
    {
        System.out.println("Enter amount to transfer: ");
        amount = input.nextInt();
        a.transfer(amount);
        a.getBalance();
        break;
    }
    default:
    {
        System.out.print("Enter 1 or 2: ");
        choice = input.nextInt();
    }
    }


}
catch(insufficientFundException ife)
{
    System.out.print(ife.getMessage());
}

}
}

以及用户定义异常的 class:

package Number3;

public class insufficientFundException extends Exception{

public static void main(String[] args) {
    System.out.println("You don't have enough funds in your account.");

}

}

请告诉我我做错了什么。谢谢。

您错误地实现了自定义异常。您需要使用您的消息在异常 class 中调用超级构造函数,例如:

class MyCustomException extends Exception {

    MyCustomException(String message) {
        super(message);
    }
}

就像其他人所说的那样,您需要使用如下消息调用异常:

throw new MyCustomException("Error message");

首先,您正在使用 getter 获取 balance\maxTransfer 而不是打印它。

您无法执行转帐,因为在您撤回开关后,开关断开,执行结束。

另请注意,您正在自定义异常中实现另一个 main() 方法 class。

此主要方法永远不会被调用。在构造函数

中将你的异常添加到你的超级class
package Number3;

public class insufficientFundException extends Exception{

    insufficientFundException(String msg){
       super(msg);
    }

}

在您的代码中出现此异常时抛出此异常

if(balance-withdrawal<0){
   throw new insufficientFundException("Your error message");
}

你的 Class 应该是这样的

package Number3;

    public class InsufficientFundException extends Exception{

    public InsufficientFundException(String message){
      super(message);
    }
}

然后创建此异常的新实例class 传入要在运行时显示的字符串值

面值示例

public void withdraw(int amount) throws insufficientFundException
{
   if(!(amount < balance))
   {
       balance = balance - amount;
   }
   else
   {
       throw new InsufficientFundException("You do not have enough money in your account");
   }
}

另外我才意识到你的 if else 语句可能是错误的,因为你告诉程序如果金额不小于余额,当你应该说金额小于余额时从余额中取出金额比余额从帐户中取出钱

像这样

if(amount < balance)
   {
       balance = balance - amount;
   }
   else
   {
       throw new InsufficientFundException("You do not have enough money in your account");
   }