exceptiondemo.java

来自「针对越来越多的用户喜欢JAVA」· Java 代码 · 共 48 行

JAVA
48 行
字号
class InsufficientFundsException extends Exception
{
   InsufficientFundsException(Bank bank, double amount)
 {
    super(excepMessage(bank, amount));
  }
  public static String excepMessage(Bank bank, double amount)
{
    String str = "The balance was " + bank.getBalance() + ", and the withdrawal was " + amount;
    return str;
  }
}
 class Bank
{
  double balance,dAmount; 
  public void deposite(double amount)
{ 
    if (amount > 0.0) balance += dAmount;
  }
  public void withdrawal(double amount)throws InsufficientFundsException
{
if ( balance < amount )
{
      throw new InsufficientFundsException(this, amount);
    }  
    balance = balance-dAmount;
  }
  public double getBalance()
 { 
    return balance;
  }
}
public class ExceptionDemo
{
  public static void main(String args[])
{
    try {
      Bank bank = new Bank();
      bank.deposite(50);
      bank.withdrawal(100);
      System.out.println("Withdrawal successful!");
    }
    catch(InsufficientFundsException e){
      System.out.println(e.toString());
    } 
  }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?