⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 exceptiondemo.java

📁 针对越来越多的用户喜欢JAVA
💻 JAVA
字号:
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 + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -