创建用户定义的异常类 - 将null作为答案(JAVA)

时间:2015-03-01 19:05:12

标签: java exception-handling

我尝试实现以下问题,但在尝试将值传递给方法时,我得到null作为答案。在撤销时计算新余额后,我似乎无法将值传递给方法转移。

enter image description here

帐户类:

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

testAccount main:

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

}
}

用户定义的异常类:

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.");

}

}

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

4 个答案:

答案 0 :(得分:4)

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

class MyCustomException extends Exception {

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

与其他人所说的一样,您需要使用如下消息来调用您的异常:

throw new MyCustomException("Error message");

答案 1 :(得分:4)

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

中将您的异常添加到超类中
package Number3;

public class insufficientFundException extends Exception{

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

}
代码中的

会在发生此异常时抛出

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

答案 2 :(得分:2)

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

您无法执行转移,因为撤销切换后,执行结束。

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

答案 3 :(得分:2)

你的班级应该是这样的

package Number3;

    public class InsufficientFundException extends Exception{

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

然后创建此Exception类的新实例,传入要在运行时显示的String值

Par示例

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