zoukankan      html  css  js  c++  java
  • 在finally块中使用try catch,并且catch的时候抛出异常的一个问题

    在finally中使用try/catch,并且catch的时候抛出异常

    IDEA会提示警告

    Reports throw statements inside of finally blocks. While occasionally intended, such throw statements may mask exceptions thrown, and tremendously complicate debugging.

    大意是:这样可能会掩盖异常抛出

    做以下测试代码: 

    public static void main(String[] args) throws Exception {
        try{
            throw new Exception("异常1");
        }catch (Exception e){
            throw new Exception("异常2");
        }finally {
            try {
                throw new Exception("异常3");
            } catch (Exception e) {
                throw new Exception("异常4");
            }
        }
    }

      

    输出结果为:

    Exception in thread "main" java.lang.Exception: 异常4
      at cn.com.dataocean.cip.web.Test.main(Test.java:22)

    只抛出了一个异常4,并没有抛出异常2。所以以后不可以在finally块中的catch中抛出异常了。

    原创文章,欢迎转载,转载请注明出处!

  • 相关阅读:
    vs2015编译各种库
    回测分析软件
    交易平台
    策略框架
    期货homes平台以及仿ctp接口
    k线中转器
    期货数据服务器以及取数据接口
    c++ 二叉树的遍历(迭代,递归)
    c++实现单向链表的一些操作
    numpy操作
  • 原文地址:https://www.cnblogs.com/acm-bingzi/p/finallyCatch.html
Copyright © 2011-2022 走看看