📄 exceptiondemo.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 + -