zoukankan      html  css  js  c++  java
  • 异常Exception(二)

    上一篇:异常Exception(二)

    学习网址:https://docs.microsoft.com/zh-cn/dotnet/standard/exceptions/exception-class-and-properties

    了解Exception的常见属性。

    Data:Exception的键/值对数据。

    using System;
    using System.Collections;
    
    namespace ConsoleApp5
    {
        class Program
        {
            static void Main(string[] args)
            {
                try
                {
                    NestedRoutine1(true);
                }
                catch (Exception e)
                {
                    // 打印Data
                    if (e.Data.Count > 0)
                    {
                        foreach (DictionaryEntry de in e.Data)
                            Console.WriteLine("    Key: {0,-20}      Value: {1}",
                                              "'" + de.Key.ToString() + "'", de.Value);
                    }
                }
                Console.ReadLine();
            }
           
    
            public static void NestedRoutine1(bool displayDetails)
            {
                try
                {
                    NestedRoutine2(displayDetails);
                }
                catch (Exception e)
                {
                    // 给data添加异常信息
                    e.Data["ExtraInfo"] = "Information from NestedRoutine1.";
                    e.Data.Add("MoreExtraInfo", "More information from NestedRoutine1.");
                    // throw后面不加异常对象,默认是当前异常
                    throw;
                }
            }
    
            public static void NestedRoutine2(bool displayDetails)
            {
                Exception e = new Exception("This statement is the original exception message.");
                if (displayDetails)
                {
                    // 给data添加异常信息
                    string s = "Information from NestedRoutine2.";
                    int i = -903;
                    DateTime dt = DateTime.Now;
                    e.Data.Add("stringInfo", s);
                    e.Data["IntInfo"] = i;
                    e.Data["DateTimeInfo"] = dt;
                }
                throw e;
            }
        }
    }

    HelpLink:指向帮助文件的 URL,没有值就为null。

    InnerException:内部异常信息,当前异常可能是其他异常导致的,记录当前异常的原始异常。

    代码:

    using System;
    using System.Collections;
    
    namespace ConsoleApp5
    {
        class Program
        {
            static void Main(string[] args)
            {           
                try
                {
                    CatchInner();
                }
                catch (AppException e)
                {
                    Console.WriteLine("外层异常信息: {0}", e.Message);
                    if (e.InnerException != null)
                        Console.WriteLine("Inner exception: {0}", e.InnerException);
                }
                Console.ReadLine();
            }
    
            public static void ThrowInner()
            {
                throw new AppException("ThrowInner内部异常信息");
            }
    
            public static void CatchInner()
            {
                try
                {
                    ThrowInner();
                }
                catch (AppException e)
                {
                    throw new AppException("CatchInner外部异常信息", e);
                }
            }
        }
    
        public class AppException : Exception
        {
            public AppException(String message) : base(message)
            { }
    
            public AppException(String message, Exception inner) : base(message, inner) { }
        }
    }
    View Code

    结果:

    Message 提供有关异常原因的详细信息。一般是给用户看的。

     Source 获取或设置导致错误的应用程序或对象的名称,可以自己设置异常的Source,给source丰富一下dll、命名空间、方法等。

     StackTrace 错误位置的堆栈跟踪,精确到行。

  • 相关阅读:
    我的WCF之旅(1):创建一个简单的WCF程序
    网页设计中颜色的搭配
    CSS HACK:全面兼容IE6/IE7/IE8/FF的CSS HACK
    UVa 1326 Jurassic Remains
    UVa 10340 All in All
    UVa 673 Parentheses Balance
    UVa 442 Matrix Chain Multiplication
    UVa 10970 Big Chocolate
    UVa 679 Dropping Balls
    UVa 133 The Dole Queue
  • 原文地址:https://www.cnblogs.com/bibi-feiniaoyuan/p/Exception2.html
Copyright © 2011-2022 走看看