异常处理

try catch
1.将可能抛出FileNotFoundException 文件不存在异常的代码放在try里
2.如果文件存在,就会顺序往下执行,并且不执行catch块中的代码
3. 如果文件不存在,try 里的代码会立即终止,程序流程会运行到对应的catch块中
4. e.printStackTrace(); 会打印出方法的调用痕迹,如此例,会打印出异常开始于TestException的第16行,这样就便于定位和分析到底哪里出了异常。 便于定位

public class TestException1 {
    public static void main(String[] args) {
        File f=new File("d:/lo.txt");

        try {
            System.out.println("试图打开d:/lo.txt");
            new FileInputStream(f);
            System.out.println("成功打开");
        } catch (FileNotFoundException e) {
            System.out.println("d;/l.txt不存在");
            e.printStackTrace();
        }
    }
}

在这里插入图片描述
FileNotFoundException是Exception的子类,使用Exception也可以catch住FileNotFoundException。
多异常捕捉办法1

//?不管打开文件成功还是失败,都不会捕获这个日期格式异常?
public class TestException2 {
        public static void main(String[] args) {

            File f = new File("d:/lol.txt");

            try {
                System.out.println("试图打开 d:/lol.txt");
                new FileInputStream(f);
                System.out.println("成功打开");
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                Date d = sdf.parse("2016-6-03");
            } catch (FileNotFoundException e) {
                System.out.println("d:/lol.txt不存在");
                e.printStackTrace();
            } catch (ParseException e) {
                System.out.println("日期格式解析错误");
                e.printStackTrace();
            }
        }
    }

多异常捕捉办法2
另一个种办法是把多个异常,放在一个catch里统一捕捉

catch (FileNotFoundException | ParseException e) {

这种方式从 JDK7开始支持,好处是捕捉的代码更紧凑,不足之处是,一旦发生异常,不能确定到底是哪种异常,需要通过instanceof 进行判断具体的异常类型

if (e instanceof FileNotFoundException)
System.out.println(“d:/LOL.exe不存在”);
if (e instanceof ParseException)
System.out.println(“日期格式解析错误”);

public class TestException3 {
    public static void main(String[] args) {
        File f=new File("d:l.txt");
        System.out.println("试图打开文件l.txt");
        try {
            new FileInputStream(f);
            System.out.println("成功打开");
            SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
            Date d=sdf.parse("2020-12-02");
        } catch (FileNotFoundException | ParseException e) {
            if(e instanceof FileNotFoundException){
                System.out.println("文件找不到");

            }if(e instanceof ParseException){
                System.out.println("日期格式错误");

            }
            e.printStackTrace();
        }

    }

}
public class TestException3 {
    public static void main(String[] args) {
        File f=new File("d:lol.txt");
        System.out.println("试图打开文件lol.txt");
        try {
            new FileInputStream(f);
            System.out.println("成功打开");
            SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
            Date d=sdf.parse("10:00");
            System.out.println("日期格式正确");
        } catch (FileNotFoundException | ParseException e) {
            if(e instanceof FileNotFoundException){
                System.out.println("文件找不到");

            }if(e instanceof ParseException){
                System.out.println("日期格式错误");

            }
            e.printStackTrace();
        }

    }

}

在这里插入图片描述
finally
无论是否出现异常,finally中的代码都会被执行

public class TestException5 {
    public static void main(String[] args) {
        File f=new File("d:/lol.txt");
        System.out.println("试图打开d:/lol.txt");
        try {
            new FileInputStream(f);
            System.out.println("成功打开");
        } catch (FileNotFoundException e) {
            System.out.println("打开失败");
            e.printStackTrace();
        }finally {
            System.out.println("无论怎样都会被执行");
        }
    }
}

throws
考虑如下情况:
主方法调用method1
method1调用method2
method2中打开文件

method2中需要进行异常处理
但是method2不打算处理,而是把这个异常通过throws抛出去
那么method1就会接到该异常。 处理办法也是两种,要么是try catch处理掉,要么也是抛出去。
method1选择本地try catch住 一旦try catch住了,就相当于把这个异常消化掉了,主方法在调用method1的时候,就不需要进行异常处理了

public class TestException6 {
    public static void main(String[] args) {
        method1();
    }

    private static void method1() {
        try{
            method2();
        }catch (FileNotFoundException e){
            e.printStackTrace();
        }
    }

    private static void method2() throws FileNotFoundException {
        File f=new File("d:/l.txt");
        System.out.println("试图打开d:/l.txt");
        new FileInputStream(f);
        System.out.println("成功打开");
    }
}

throws与throw这两个关键字接近,不过意义不一样,有如下区别:

  1. throws 出现在方法声明上,而throw通常都出现在方法体内。
  2. throws 表示出现异常的一种可能性,并不一定会发生这些异常;throw则是抛出了异常,执行throw则一定抛出了某个异常对象。
    无论try代码块中是否有异常,finally里的代码都会执行
    当try和catch代码块中有return语句时,finally仍然会执行
    如果try…catch…finally都有return语句,则等待try catch执行完之后,跳过try catch里的return语句只执行finally中的return语句
    如果仅仅是try…catch里有return语句,那么在执行return语句之前会先执行finally代码块里的内容
public class Demo01 {
    //try catch也执行了
    //带返回类型的绝不应该在finally中带有返回值,否则try catch将毫无意义。
    public static void main(String[] args) {
        int result=method();
        System.out.println(result);



    }
    public static int method(){
        try {
            return  1;

        }catch(Exception e){
            return  2;

        }finally{

        }
    }

}

该方法必须有返回值,try/catch块里必须要有return语句。
finally块里如果有返回语句,输出的结果就是finally里边的值,但是try和catch里边也执行了,但是如果finally里边没有return语句,输出结果是try里边的语句。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值