runtime
运行的时候出现的异常,程序本身没有问题。
- error:跟系统本身有关的异常
- Arithmetic:与运算法则相关的问题
- Nullpointer:对象没有初始化,对象所应用的内容不存在
- IndexOutOfBounds:下标超出了数组的范围
- IllegalArgument:语法没有符合要求,比如1/2输出0,与规则相关。
checked exceptions VS unchecked exceptions
能否被编译器查出来的
checked—error
unchecked—runtimeException
- try-catch
try {
cash=Long.parseLong(in.nextLine());
}
catch(NumberFormatException nfe) {
System.out.println("输入金额不正确,请重新输入取款金额");
}
- 在两个方法里的 try-catch
public void withDraw(long cash) throws InterruptedException{
if(..)
throw new InterruptedException();
else{
...
}
}
public void B() {
try {
Bank bank=new Bank();
bank.withDraw(cash);
}
catch (InterruptedException ie) {
System.out.println("异常对象中保存的错误消息");
}
}