异常机制
异常概念
异常处理机制
- 抛出异常
- 捕获异常
异常处理五个关键字
- try catch finally throw throws
package exception;
public class Test {
public static void main(String[] args) {
int a=1;
int b=0;
//假设要捕获多个异常,要从小到大去捕获
try{//try监控区域
System.out.println(a/b);
}catch (ArithmeticException e){//catch 捕获异常
System.out.println("ArithmeticException");
}catch (Exception e){
System.out.println("Exception");
}
catch (Throwable e){
System.out.println("Throwable");
}
finally {//处理善后工作
System.out.println("finally");
}
}
}
主动的抛出异常,一般在方法中使用。