首页 > 代码库 > 捕获异常

捕获异常

 

            while (true)
            {
                try
                {
                    string str = Console.ReadLine();
                    if (str == "")
                    {
                        return;//这里退出也会执行finally语句
                    }
                    if (str == "1")
                    {
                        //实例化异常类,并设置其属性
                        Exception myexception = new IndexOutOfRangeException();
                        myexception.Source = "Source";
                        myexception.HelpLink = "http://www.baidu.com";
                        myexception.Data["errordate"] = DateTime.Now;
                        myexception.Data.Add("DataAdd", "MyDataAdd");
                        throw myexception;
                    }
                    Console.WriteLine(str);
                }
                catch (IndexOutOfRangeException ex)//catch出现的顺序要合理(派生类在前面,基类在后面)
                {
                    System.Diagnostics.Process.Start(ex.HelpLink);//跳转错误连接
                    Console.WriteLine(ex.Message);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
                finally
                {
                    Console.WriteLine("finally");
                }
            }

  一般情况下,异 常没有特定的名称空间,异 常类应放在生成异常的类所在的名称空间中,因 此与 Io相 关的异常就在 system。 Io名称空间中。在许多基类名称空间中都有异常类

wKioL1RePlzjLzIxAAMFFAxfZNw146.jpg

本文出自 “程序猿的家--Hunter” 博客,请务必保留此出处http://962410314.blog.51cto.com/7563109/1574488

捕获异常