zoukankan      html  css  js  c++  java
  • C#各种异常处理方式

    .NET的异常处理机制用来发现、处理运行时错误。如果开发人员没有提供异常的处理机制,就默认采用.NET的机制。

     

    通常使用try...catch...finally捕获异常。

    try
    
    {
    
        //有可能发生异常
    
    }
    
    catch(Exception ex)
    
    {
    
        //处理异常
    
    }
    
    finally
    
    {
    
        //清理
    
    }

    ○ 如果没有异常发生,就直接到finally语句块中。
    ○ finally语句块是必须执行的
    ○ 这里的catch和finally语句块是可选的。try语句块后面可以跟1个或多个catch语句块,try语句块后面可以直接跟finally语句块。
    ○ Exception是所有异常的基类

     

    □ 使用.NET默认异常处理机制捕获异常

        class Program
    
        {
    
            static void Main(string[] args)
    
            {
    
                int a = 0;
    
                int result = 100/a;
    
                Console.WriteLine(result);
    
                Console.ReadKey();
    
            }
    
        }

    1

     

    □ 使用try...catch手动捕获异常

        class Program
    
        {
    
            static void Main(string[] args)
    
            {
    
                int a = 0;
    
                int result = 0;
    
                try
    
                {
    
                    result = 100/a;
    
                    Console.WriteLine("这里不会执行");
    
                }
    
                catch (DivideByZeroException exception)
    
                {
    
                    Console.WriteLine("出现异常");
    
                }
    
                Console.WriteLine(result);
    
                Console.ReadKey();
    
            }
    
        }
    

    2

    □ 使用try...catch...finally手动捕获异常

       class Program
    
        {
    
            static void Main(string[] args)
    
            {
    
                int a = 0;
    
                int result = 0;
    
                try
    
                {
    
                    result = 100/a;
    
                    Console.WriteLine("这里不会执行");
    
                }
    
                catch (DivideByZeroException exception)
    
                {
    
                    Console.WriteLine("出现异常");
    
                }
    
                finally
    
                {
    
                    Console.WriteLine("放行吧,肯定会执行到我这里的~~");
    
                }
    
                Console.WriteLine(result);
    
                Console.ReadKey();
    
            }
    
        }
    

    3

    可见,finally语句块中的内容一定会被执行。

     

    □ 使用try...多个catch...finally手动捕获异常

        class Program
    
        {
    
            static void Main(string[] args)
    
            {
    
                int a = 0;
    
                int result = 0;
    
                try
    
                {
    
                    result = 100/a;
    
                    Console.WriteLine("这里不会执行");
    
                }
    
                catch (DivideByZeroException exception)
    
                {
    
                    Console.WriteLine("不能被0除的异常");
    
                }
    
                catch (Exception ex)
    
                {
    
                    Console.WriteLine("异常");
    
                }
    
                finally
    
                {
    
                    Console.WriteLine("放行吧,肯定会执行到我这里的~~");
    
                }
    
                Console.WriteLine(result);
    
                Console.ReadKey();
    
            }
    
        }
    

    4

    可见,只要有一个catch语句块捕获到异常,其它catch语句块不执行。

     

    □ 使用try...catch(不带括号,不带参数)手动捕获异常

        class Program
    
        {
    
            static void Main(string[] args)
    
            {
    
                int a = 0;
    
                int result = 0;
    
                try
    
                {
    
                    result = 100/a;
    
                    Console.WriteLine("这里不会执行");
    
                }
    
                catch
    
                {
    
                    Console.WriteLine("异常");
    
                }
    
                Console.WriteLine(result);
    
                Console.ReadKey();
    
            }
    
        }
    

    5

    通过以上方法,可以捕获任何异常。

     

    □ try...catch手动捕获抛出的异常

        class Program
    
        {
    
            static void Main(string[] args)
    
            {
    
                try
    
                {
    
                    throw new DivideByZeroException("除数不能为零");
    
                }
    
                catch (DivideByZeroException e)
    
                {
    
                    Console.WriteLine("异常");
    
                }
    
                Console.WriteLine("最后想说的");
    
                Console.ReadKey();
    
            }
    
        }
    

    6

    抛出异常本身并没有显示。

     

    □ 较高层次上下文捕获较低抛出的异常

       class Program
    
        {
    
            static void Main(string[] args)
    
            {
    
                Calculate c = new Calculate();
    
                try
    
                {
    
                    c.Divide();
    
                }
    
                catch (Exception e)
    
                {
    
                    Console.WriteLine("捕获异常");
    
                }
    
                Console.WriteLine("最后想说的");
    
                Console.ReadKey();
    
            }
    
        }
    
        public class Calculate
    
        {
    
            public void Divide()
    
            {
    
                try
    
                {
    
                    int a = 0;
    
                    int result = 100/a;
    
                }
    
                catch (DivideByZeroException e)
    
                {
    
                    throw;
    
                }
    
            }
    
        }
    

    7

    在Calculate内部抛出的异常,被更高层次的客户端捕获。

     

    □ 自定义异常

       class Program
    
        {
    
            static void Main(string[] args)
    
            {
    
                try
    
                {
    
                    throw new MyException("i am exception");
    
                }
    
                catch (Exception e)
    
                {
    
                    Console.WriteLine("捕获到自定义异常了~~");
    
                }
    
                Console.WriteLine("最后想说的");
    
                Console.ReadKey();
    
            }
    
        }
    
        public class MyException : Exception
    
        {
    
            public MyException(string str)
    
            {
    
                Console.WriteLine("这是我自定义的异常:" + str);
    
            }
    
        }
    

    8

    总结:
    ○ .NET异常处理并不是标准的try...catch...finally,可以是很灵活的。
    ○ 尽量在较低层次抛异常,在较高层次捕获异常。

  • 相关阅读:
    【转】配置BT5中文环境
    Jaspersoft iReport Designer 4.7.0 导出pdf 中文不显示的解决办法
    JS通过get、post向jsp传递中文出现乱码的问题的解决
    从 相机 或者相册 获取图片显示在ImageView 上
    简单几段代码实现窗口抖动
    android 塔防游戏汇总 及android 游戏开发索引
    android 音乐播放器汇总
    android Style应用
    android手机控制电脑源码
    【Android通过手势实现的缩放处理】
  • 原文地址:https://www.cnblogs.com/darrenji/p/3965443.html
Copyright © 2011-2022 走看看