以下关于异常处理的代码有哪些问题?

 public static void start() throws IOException,RuntimeException {
        throw new RuntimeException("Not able to Start");
    }
    
    public static void main(String[] args) {
        BufferedReader br=null;
        try {
            String line;
            br=new BufferedReader(new FileReader("/tempFile"));
            while ((line=br.readLine())!=null){
                start();
            }
            return;
        }catch (Exception ex){
            ex.printStackTrace();
        }catch (RuntimeException re){
            re.printStackTrace();
        }finally {
            //是否会输出?
            System.out.println("1");
        }
    }

面试回答

  1. #start方法不会发生 IOException ,所以不需要 throw
  2. RuntimeException不需要显示的 throw
  3. catch的时候,要先从子类开始 catch,代码中 catch的顺序不对
  4. 没有关闭流
  5. return 之前的 finally block 是会被执行的

知识扩展

上诉代码,如何优化

    public static void main(String[] args) {
        try (BufferedReader br = new BufferedReader(new FileReader("tempFile"))) {
            String line;
            while ((line = br.readLine()) != null) {
                System.out.println(line);
            }
        } catch (Exception e) {
            // handle Exception
        }
    }

try-with-resource 的原理

javac 使用了语法糖进行优化

 public static void main(String args[])
    {
        BufferedReader br;
        Throwable throwable;
        br = new BufferedReader(new FileReader("tempFile"));
        throwable = null;
        String line;
        try
        {
            while((line = br.readLine()) != null) 
                System.out.println(line);
        }
        catch(Throwable throwable2)
        {
            throwable = throwable2;
            throw throwable2;
        }
        if(br != null)
            if(throwable != null)
                try
                {
                    br.close();
                }
                catch(Throwable throwable1)
                {
                    throwable.addSuppressed(throwable1);
                }
            else
                br.close();
        break MISSING_BLOCK_LABEL_113;
        Exception exception1;
        exception1;
        if(br != null)
            if(throwable != null)
                try
                {
                    br.close();
                }
                catch(Throwable throwable3)
                {
                    throwable.addSuppressed(throwable3);
                }
            else
                br.close();
        throw exception1;
        Exception exception;
        exception;
    }

Java 中异常的处理方式有哪几种?一般如何选择

异常的处理方式有两种:

  1. 自己处理。
  2. 向上抛,交给调用者处理。

异常,千万不能捕获了之后什么也不做。或者只是使用 e.printStackTrace();

具体的处理方式的选择原则其实比较简明:

  1. 自己明确的知道如何处理的,就要处理掉。
  2. 不知道如何处理的,就交给调用者处理。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

协享科技

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值