zoukankan      html  css  js  c++  java
  • Java异常之finally和多重捕获·11

    • 一个try代码块后面跟着多个catch代码块的情况就叫多重捕获
      • 语法

    try{
        //可能发生异常的代码块
    }catch(ExceptionName1 e1){
        // 出异常时候处理
    }catch(ExceptionName2 e2){
        // 出异常时候处理
    }
    • 代码中发生异常,异常被抛给第一个catch块,如果不匹配则继续往下一个catch进行传递
    • finally关键字
      • 用来创建在try代码块后面执行的代码块
      • finally代码块中的代码总会被执行
      • 一般用于资源回收释放等操作
      • 语法:
        try{
            // 可能发生异常的代码
        }catch(Exception e){
            // 出现异常时的处理
        }finally{
            //肯定执行的代码
        }
        • 或者
        • try{
             // 可能发生异常的代码块 
          }finally{
              // 肯定执行的代码
          }

         

        • 面试注意点、返回结果最终是以fianlly为准

      • public static int divide(int num1, int num2) {
            try {
                int result = num1 / num2;
                return result;
            } catch (Exception e) {
                System.out.println("出异常了......");
            } finally {
                System.out.println("finally代码块被执行了......");
                return -2;
            }
        

          

    Bug? 不存在的!
  • 相关阅读:
    [ZOJ1610]Count the Colors
    浅谈算法——线段树之Lazy标记
    浅谈算法——线段树
    [HEOI2013]Segment
    [JSOI2008]Blue Mary开公司
    [JSOI2016]扭动的回文串
    [BZOJ3790]神奇项链
    [BZOJ2565]最长双回文串
    [BZOJ2160]拉拉队排练
    [POI2010]Antisymmetry
  • 原文地址:https://www.cnblogs.com/mrchenyushen/p/15028211.html
Copyright © 2011-2022 走看看