异常机制

异常处理框架

Error

由java虚拟机生成抛出,大多数与代码编写者无关

Exception

在Exception分支中有重要的子类RuntimeException(运行时异常)

  • ArrayindexOutOfBoundsException数组下标越界
  • NullPointerException(空指针异常)
  • ArithmeticException(算数异常)
  • MissingResource(丢失资源)
  • ClassNotFound(找不到类)

程序中这些异常不是检查异常,可以选择捕获,也可以不处理

尽可能从逻辑角度避免发生

除了运行式异常其他称为检查异常

捕获和抛出

五个关键字

try catch finally throw throws

public class Demo01 {
    public static void main(String[] args) {
        int a = 1;
        int b = 0;
        try {//监控区域
            System.out.println(a/b);
        }catch(ArithmeticException e//想要捕获的异常类型 +名字e){//捕获异常
            System.out.println("程序出现异常,变量b不能为0");
        }finally {//善后区域
            System.out.println("finally");
        }//finally可以不写 但是try catch必须写

    }
}

假设要捕获多个异常 ,需要从小到大捕获

try {//监控区域
        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");
    }

}

自动生成try catch快捷键

ctrl +alt+T

try {
    System.out.println(a/b);
} catch (Exception e) {
    System.exit(0);
    e.printStackTrace();//打印错误的栈信息
} finally {
    System.out.println("finally");
}


Process finished with exit code 0

System.exit(0);为手动结束程序 ,不会执行finally。0为显示的code 能快速分辨哪部分catch了异常

主动抛出

try {
    if (b==0){//主动抛出异常 throw throws 不一样
        throw new ArithmeticException();
    }
    System.out.println(a/b);
} catch (Exception e) {

    //e.printStackTrace();//打印错误的栈信息
} finally {
    System.out.println("finally");
}



finally

Process finished with exit code 0

throw什么都不会做 一般用在方法中 在知道可能出错的地方

throws假设方法中处理不了异常 在方法上抛出异常

public void test(int a,int b) throws ArithmeticException{
    if (b==0){//主动抛出异常 throw throws 不一样
        throw new ArithmeticException();
    }
    System.out.println(a/b);
}

自定义异常

一般很少用,使用java内置异常类可以处理大部分异常

自定义异常类需要继承EXception

public class MyException extends Exception{
    //传递数字大于10 抛出
    private int detal;

    public MyException(int a) {
        this.detal =  a ;
    }
//tostring

    @Override
    public String toString() {
        return "MyException{" +
                "detal=" + detal +
                '}';
    }
}
public class test {
//    可能存在异常的方法
    static void  test(int a) throws MyException {
        System.out.println("传递的参数"+a);
        if (a>10){
           throw  new MyException(a);
        }
        System.out.println("ok");
    }

    public static void main(String[] args) {
        try {
            test(11);
        } catch (MyException e) {
            System.out.println("MyException"+e);
        }
    }
}

传递的参数
MyExceptionMyException{detal=11}

尽量处理异常

不要简单用默认printStackTrace();输出 可以增加一些处理异常的代码块

尽量添加finally 语句块去释放占用的资源

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值