异常处理机制
- 抛出异常
- 捕获异常
- 异常处理的五个关键字:try,catch,finally,throw,throws
package com.exception;
public class Test {
public static void main(String[] args) {
int a = 1;
int b = 0;
try{
if(b==0){
throw new ArithmeticException();
}
System.out.println(a/b);
} catch (ArithmeticException e) {
System.out.println("程序出现异常,变量b不能为0");
e.printStackTrace();
} catch(Error e){
System.out.println("Error");
}catch(Exception e){
System.out.println("Exception");
}catch(Throwable e){
System.out.println("Throwable");
}finally{
System.out.println("finally");
}
}
public void a(){
b();
}
public void b(){
a();
}
}