• C# try catch嵌套


    try catch嵌套之后出现异常执行顺序:

    static void Main(string[] args)
            {
                try
                {
                    Console.WriteLine("----------------------外层try------------------------------");
                    errorMethod();               
                }
                catch (Exception ex)
                {
                    Console.WriteLine("----------------------外层catch" + ex.Message + "------------------------------");
                }
                finally {
                    Console.WriteLine("----------------------外层finally------------------------------");
                }
    
                Console.ReadKey();
    
    
            }
    
            private static void errorMethod()
            {           
                try
                {
                    Console.WriteLine("----------------------内层try------------------------------");
                    int i = 0;
                    int a = 100/i;
                }
                catch (Exception ex)
                {
                    Console.WriteLine("----------------------内层catch" + ex.Message + "------------------------------");
                }
                finally {
                    Console.WriteLine("----------------------内层finally------------------------------");
                }
            }

    输出结果:

    内层catch处理了异常,所以没有执行外层catch,把内层的catch注释掉,再试下:

    static void Main(string[] args)
            {
                try
                {
                    Console.WriteLine("----------------------外层try------------------------------");
                    errorMethod();               
                }
                catch (Exception ex)
                {
                    Console.WriteLine("----------------------外层catch" + ex.Message + "------------------------------");
                }
                finally {
                    Console.WriteLine("----------------------外层finally------------------------------");
                }
    
                Console.ReadKey();
    
    
            }
    
            private static void errorMethod()
            {           
                try
                {
                    Console.WriteLine("----------------------内层try------------------------------");
                    int i = 0;
                    int a = 100/i;
                }
                //catch (Exception ex)
                //{
                //    Console.WriteLine("----------------------内层catch" + ex.Message + "------------------------------");
                //}
                finally {
                    Console.WriteLine("----------------------内层finally------------------------------");
                }
            }

    输出结果:

    总结:try catch嵌套,内层不能捕获时,会考虑外层内否捕获,内层能捕获,则外层catch不执行。

  • 相关阅读:
    编写好代码的10条戒律
    [Project] 基开放云平台
    [Project] HUSTOJ随笔
    编码规范:大家都应该做的事情
    ural 1167. Bicolored Horses 夜
    1709. PenguinAvia 夜
    hdu 1011 Starship Troopers 夜
    hdu 2571 命运 夜
    hdu 1561 The more, The Better 夜
    hdu 1598 find the most comfortable road 夜
  • 原文地址:https://www.cnblogs.com/stilldream/p/10599818.html
Copyright © 2020-2023  润新知