1、StuCard.java
public class StuCard {
public static void TransMoney(int source, int money, int target) {
money -= target;
if(money < 0) {
throw new NotEnoughMoneyException("余额不足");
}
System.out.println("商家的余额为:" + source);
System.out.println("学生的余额为:" + money);
}
public static void main(String[] args) {
try {
TransMoney(500, 20, 21);
}catch(RuntimeException e){
e.printStackTrace();
}
}
}
2、NotEoughMoneyException.java
public class NotEnoughMoneyException extends RuntimeException{
public NotEnoughMoneyException() {
super();
// TODO Auto-generated constructor stub
}
public NotEnoughMoneyException(String message, Throwable cause, boolean enableSuppression,
boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
// TODO Auto-generated constructor stub
}
public NotEnoughMoneyException(String message, Throwable cause) {
super(message, cause);
// TODO Auto-generated constructor stub
}
public NotEnoughMoneyException(String message) {
super(message);
// TODO Auto-generated constructor stub
}
public NotEnoughMoneyException(Throwable cause) {
super(cause);
// TODO Auto-generated constructor stub
}
}