JavaException的使用
2021-06-16 03:03
标签:ack 模拟 main pac print ati 一个 ber this (第一个文件 MyException.java) package exception; //先创建自己的异常类继承与Exception的类 public class MyException extends Exception{ public double getAmount() { (第二个文件CheckingAccount .java) package exception; //模拟银行存取钱系统 public class CheckingAccount { public CheckingAccount(int number) { public void deposit(double amount) { public void withdraw(double amount) throws MyException{ } public double getBlance() { public int getNumber() { (第三个文件BankDemo.java) package exception; public class BankDemo { //实例的应用 public static void main(String[] args) { System.out.println("取600元。"); } JavaException的使用 标签:ack 模拟 main pac print ati 一个 ber this 原文地址:https://www.cnblogs.com/ZhuDianHui/p/9728565.html
import java.io.*;
private double amount;
public MyException(double amount) {
this.amount = amount;
}
return amount;
}
}
private double blance;
private int number;
this.number = number;
}
blance += amount;
}
if(amount blance -= amount;
}else {
double needs = amount - blance;
throw new MyException(needs);
}
return blance;
}
return number;
}
}
CheckingAccount c1 = new CheckingAccount(101);
System.out.println("存500元。");
c1.deposit(500);
System.out.println("取200元。");
try {
c1.withdraw(200);
double amount = c1.getBlance();
System.out.println("账户余额:"+amount);
c1.withdraw(600);
} catch (MyException e) {
System.out.println("您的余额不足:差"+e.getAmount());
}
}