• Java基础:异常捕获顺序


     转载请注明出处:jiq•钦's technical Blog 


    public voidtestException(){
    int a[] = {1,2,3};int q = 0;
    try{
    for(int i=0;i<a.length;i++){a[i] /= q;}
    }catch(ArithmeticException h){
    System.out.print("ArithmeticException
    ");        //运行
    }catch(Exception e){
    System.out.print("Exception
    ");        //不会运行。且必须放在ArithmeticException后面
    /**
     * 范围更大的Exception不但必须放在后面
     * 并且放在后面还不会被运行(被前面的范围更小的
     * 异常拦截了),那这样还有什意义呢???
     */
    }finally{System.out.print("finally
    ");}
    }
    //<span style="color:#3333ff;">output</span>
    ArithmeticException
    finally

     *        要点1尽管ArithmeticException继承自Exception。可是当发生ArithmeticException异常

     *                        并捕获的时候,就仅仅会捕获实际发生的这个异常。并不会由于Exception是其父类而

     *                        运行Exception那个catch子句。

     *        要点2可是假设你尝试将范围更大的Exceptioncatch语句放到的catch语句的前面,那么就会发生

     *                        catch子句不可到达的错误“Unreachablecatch block for ArithmeticException.

     *                        Itis already handled by the catch block for Exception

     *                        即范围更大的异常(父类)必须放在后面,假设没有继承关系,比方ClassNotFoundException

     *                        ArithmeticExceptioncatch子句之间就无所谓先后关系。

  • 相关阅读:
    webpack debug
    linux下光标操作
    windows 下 基于express搭建 https协议的网站
    js里的null 与undefined
    scrollIntoView 前的元素滚动到浏览器窗口的可视区域内 不止垂直滚动,还有水平滚动
    js 四舍五入实现
    react 踩坑记
    String.slice
    sublime 常用插件
    springcloud 自己挖坑 @ConfigurationProperties不生效
  • 原文地址:https://www.cnblogs.com/wzjhoutai/p/6922517.html
Copyright © 2020-2023  润新知