public ATMException(String message) {
super(message);
}
}
//银行取款机
class ATM {
//取款方法
public static int getMoney(int money) throws ATMException{
if (money>3000) {
throw new ATMException("余款不足,请重新输入!");
} else {
System.out.println("正在出款:"+money);
return money;
}
}
}
public class ExceptionTest {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
System.out.println("请输入取款金额:");
int money = sc.nextInt();
//银行ATM取款
try {
int result = ATM.getMoney(money);
System.out.println("取到金额:"+result);
} catch (ATMException e) {
e.printStackTrace();
}
}
}