zoukankan      html  css  js  c++  java
  • try-catch-finally

    try catch finally与return的执行顺序 

    测试1:    
    public static int test1()    
    {         int i = 1;     
        try        
      {       
          return ++i;      
       }      
       finally       
      {      
           ++i;           
      Console.WriteLine("finally:" + i);     
       } 

     }

        static void Main(string[] args)    
    {        
    Console.WriteLine("Main:" + test1());  
      }
    结果:
    finally:3
    Main:2

    测试2:     public static int test2()  
       {   
          int i = 1;         try       
      {           
      throw new Exception();    
         }        
    catch     

        {        

         return ++i;   

          }        

    finally        

    {           

      ++i;            

    Console.WriteLine("finally:" + i);       

      }    

    }    

    static void Main(string[] args)    

    {        

    Console.WriteLine("Main:" + test2());    

    }

    结果:

    finally:3

    Main:2

    测试3:    

    public static int test3()    

    {       

      try{}      

       finally      

       {          

       return 1;      

       }   

      }

    结果: 编译错误,控制不能离开 finally 子句主体。

    结论:

    1.不管出没出现异常,finally块中的语句都会执行;

    2.当try或catch块中有return语句时,finally块中的语句仍会执行;

    3.finally块中的语句是在函数返回前执行的,但函数返回值是在finally块中语句执行前确定的;

    4.finally块中不能包含return语句。

  • 相关阅读:
    hive.exec.parallel参数
    MySQL FEDERATED 提示
    mapreduce作业单元测试
    linux 更改mysql的数据库目录
    SQL Server 2008数据库邮件配置及应用
    mysql主键大小写不敏感的解决办法
    java遍历hashMap、hashSet、Hashtable
    Linux下命令行显示当前全路径方法
    通过SQL Server操作MySQL的步骤和方法
    Linux shell获取时间和时间间隔(ms级别)
  • 原文地址:https://www.cnblogs.com/hobby0524/p/3251316.html
Copyright © 2011-2022 走看看