• C#中的异常处理


        今天我们研究学习一下C#中的异常处理机制。

        首先,异常处理是用来防止可以预测到却不能完全防止的错误。使用得当的异常处理机制,可以有效防止程序的崩溃。在这种意义上,程序其他的一些错误,诸如bugs, errors需要用应用程序来处理用户的错误。

        异常处理有以下几个字段:try,throw,catch。使用异常处理,要用System.Exception的命名空间。抛出的异常,会沿着类的继承链条延续下去,直到传到System.Exception中去(这种机制同C++和JAVA一样)。

     1 public class Test
     2 {
     3    public static void Main()
     4    {
     5       Console.WriteLine("Enter Main...");
     6       Test t = new Test();
     7       t.Func1();
     8       Console.WriteLine("Exit Main...");
     9    }
    10    public void Func1()
    11    {
    12        Console.WriteLine("Enter Func1...");
    13        Func2();
    14        Console.WriteLine("Exit Func1...");
    15     }
    16 
    17 public void Func2()
    18 {
    19   Console.WriteLine("Enter Func2...");
    20   throw new System.ApplicationException();
    21   Console.WriteLine("Exit Func2...");
    22   }
    23  }
    24 }
  • 相关阅读:
    青城的另一个一夜/情
    SystemProperties.get/set property_get/set
    锁——Java同步的基本思想
    CMUSphinx Learn
    猜数字
    我的音乐我的电影
    动态规划_钢条切割问题
    directdraw显示yuv420(YV12)
    Redis 命令参考
    HDU 3078 LCA转RMQ
  • 原文地址:https://www.cnblogs.com/Dukechris/p/4443000.html
Copyright © 2020-2023  润新知