这篇文章简单的回顾一下java中的异常机制
java可以把异常当成对象处理
 异常分为两大类 Error和Exception
 异常处理的五个关键字 try catch finally throw throws
请看代码
 catch小括号里面是想要捕获的异常(捕获的是try语句块里的)
 大括号里面是处理这个异常
 可以写多个catch
 finally最终一定会执行
		int a = 1;
        int b = 0;
        try{
            System.out.println(a/b);
        }catch (ArithmeticException e){
            System.out.println("程序出现异常");
        }finally {
            System.out.println("finally");
        }
 
idea快捷键 ctrl + alt + t 生成代码块
 throw关键字是手动抛出一个具体的异常 在方法中使用
 如果方法中处理不了这个异常 可以选择上抛给调用它的方法 使用throws关键字
    public static void test(int a , int b) throws ArithmeticException{
        if (b == 0){
            throw new ArithmeticException();        
        }
        System.out.println(a/b);
    }
 
自定义异常 需要继承Exception类 重写toString方法
class MyException extends Exception{
    private int num;
    public MyException(int num) {
        this.num = num;
    }
    @Override
    public String toString() {
        return "MyException{" +
                "num=" + num +
                '}';
    }
}
 
它本身是一个未被处理的异常 需要加入try catch关键字或者上抛这个异常
public static void main(String[] args) throws MyException {
        throw new MyException(10);
    }










