异常
Exception
三大类型的异常:
-
检查性异常
-
运行时异常
-
错误error
Error:error 类对象由Java虚拟机生成并抛出
Exception:一般由程序逻辑错误引起的
1.异常处理机制
public static void main(String[] args) {
int a = 1;
int b = 0;
//Ctrl + Alt + T
try{//监控区域
if(b==0){
throw new ArithmeticException();//主动抛出异常
}
System.out.println(a/b);
}catch (ArithmeticException e){//想要捕获的异常类型 捕获异常
System.out.println("错误");
}catch (Exception e){
System.out.println("Exception");
}catch (Throwable t){
System.out.println("Throwable");
}
finally {
System.out.println("finally");
}
}
public void test(int a,int b){
if(b==0){
throw new ArithmeticException();//主动抛出异常 一般在方法中使用
}
System.out.println(a/b);
}

浙公网安备 33010602011771号