抛出异常
捕获异常
IDEA 使用异常的快捷:Ctrl+Alt+t
异常处理的五个关键字
try
catch
try {
if (b==0){
throw new ArithmeticException();
}
System.out.println(a/b);
}catch ( Error e){
System.out.println("Error");
}catch (Exception e){
System.out.println("Exception");
}
catch (Throwable t){
System.out.println("Throwable");
}
finally {
System.out.println("finally");
}
finally
- 处理善后工作
- 使用finally,finally一定会被执行
- finally 可以不要finally 假设IO,资源,关闭!
throw
throws
public void test(int a,int b) throws ArithmeticException{
if (b==0){
throw new ArithmeticException();
}
}