java:异常,File,递归

异常

异常:指的是程序出现不正常的情况

对应到java中异常的分类 java中的异常:Throwable 严重的问题:Error,我们不处理,这样的问题一般都是很严重的,比如说内存溢出(OOM) 问题:Exception(异常) 编译时期异常:除了RuntimeException都是编译时期异常,必须要处理,如果不处理,程序编译无法通过,无法运行。 运行时期异常:RuntimeException,这样的问题,一般情况下,我们不会对问题本身处理,因为这样的问题是自己在写代码 过程中由于逻辑的不严谨导致的。

如果程序出现了问题,我们并且没有做任何处理,最终JVM会做出默认的处理 把异常的名称,相关的原因,以及出现问题的信息输出在控制台上 同时程序会停止,而且后续的代码不会执行。

public class ExceptionDemo1 {
    public static void main(String[] args) {
    int a = 10;
//        int b = 20;
    int b = 0;
    System.out.println(a/b); //ArithmeticException
//        if(b!=0){
//            System.out.println(a/b);
//        }
    System.out.println(10);


}
}

 

 
如何处理异常

1、try…catch…finally 2、throws

try...catch...finally的处理格式:

try{ 可能会出现问题的代码 }

catch(异常类名 变量名){ 针对出现问题后的处理代码 }

finally{ 只要是程序顺利进行,finally里面的内容无论如何都会执行 }

变形格式:

try{ 可能会出现问题的代码 }

catch(异常类名 变量名){ 针对出现问题后的处理代码 }

注意事项:

1、try里面的代码越少越好

2、catch必须要有内容,哪怕是一句简单的提示

public class ExceptionDemo2 {
    public static void main(String[] args) {
        int a = 10;
        int b = 0;

        try{
            System.out.println(a/b);
        }catch (ArithmeticException e){
            System.out.println("除数为0.。。。");
        }

        System.out.println("over");
    }
}

处理不同异常的情况的方法 

1、处理一个异常的情况

2、处理2个及两个以上的异常的情况

1) 每一个异常写一个try...catch

2) 写一个try,多个catch

try{ 可能会出现问题的代码 }

catch(异常类名1 变量1){ 针对于异常1做的处理方案 }

catch(异常类名2 变量2){ 针对于异常2做的处理方案 }...

当处理多个异常使用第二种方案的时候需要注意的事项:

1、catch只能匹配对应的异常,并且只能匹配一个异常

2、平级关系的异常顺序无所谓,如果出现了父子继承关系,父类必须放在最后

3、try里面如果出错了,try中后面的代码不会执行,就会匹配catch中的异常, 匹配到后执行解决方案,后面的代码正常执行

4、catch中的异常类名,尽量明确,不要用大的异常去处理

public class ExceptionDemo3 {
    public static void main(String[] args) {
//        fun();
//        fun1();
        fun2();
    }

    public static void fun2(){
        int a = 10;
        int b = 5;
        int[] arr ={1,2,3};

        try {
            System.out.println(a/b); //走到这里,报错了,try后面的代码不会继续执行,直接去匹配catch中的异常
            System.out.println(arr[4]);
        }catch (ArrayIndexOutOfBoundsException a1){//
            System.out.println("除数为0.。。");
        }catch (ArithmeticException a2){
            System.out.println("数组下标索引越界。。。。");
        }
        System.out.println("over3");

    }

    public static void fun1(){
        int a = 10;
        int b = 0;

        try{
            System.out.println(a/b);
        }catch (ArithmeticException e){
            System.out.println("除数为0.。。。");
        }

        System.out.println("over");

        System.out.println("==============================");
        int[] arr ={1,2,3};

        try {
            System.out.println(arr[4]); //ArrayIndexOutOfBoundsException
        }catch (ArrayIndexOutOfBoundsException q){
            System.out.println("数组下标越界。。");
        }

        System.out.println("over2");


    }

    public static void fun(){
        int a = 10;
        int b = 0;

        try{
            System.out.println(a/b);
        }catch (ArithmeticException e){
            System.out.println("除数为0.。。。");
        }

        System.out.println("over");
    }
}

 

 JDK1,7后出现的处理多个异常的方式:

try

{ 可能会出现问题的代码 }

catch(ArrayIndexOutOfBoundsException | ArithmeticException | ...)

{ 处理问题的方案 }

注意事项:

1、处理方式是一致的,这个方式虽然简洁,但是也不够好,针对多种数据类型的异常,只给出了一种解决方案

2、这样的方式,catch中只能写平级关系的异常类!!!

public class ExceptionDemo4 {
    public static void main(String[] args) {
    fun();
}


    public static void fun() {
        int a = 10;
        int b = 5;
        int[] arr = {1, 2, 3};

//        try {
//            System.out.println(a/b); //走到这里,报错了,try后面的代码不会继续执行,直接去匹配catch中的异常
//            System.out.println(arr[4]);
//        }catch (ArrayIndexOutOfBoundsException a1){//
//            System.out.println("除数为0.。。");
//        }catch (ArithmeticException a2){
//            System.out.println("数组下标索引越界。。。。");
//        }

        try {
            System.out.println(a / b);
            System.out.println(arr[4]);
        }catch (ArrayIndexOutOfBoundsException|ArithmeticException e){
            System.out.println("出错了。。。。");
        }


        System.out.println("over3");
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值