C# 什么是内部异常

C# 什么是内部异常,c#,.net,exception,inner-exception,C#,.net,Exception,Inner Exception,我已经阅读了MSDN,但我无法理解这个概念 如果我错了,请纠正我 innerexception将与当前异常一起使用 首先会发生内部异常,然后会发生当前异常(如果存在异常),这就是为什么会根据null检查InnerException。为了保留内部异常,我们必须将其作为参数传递 我说的对吗?内部异常是导致当前异常的异常 当您希望呈现与代码捕获的异常不同的异常,但又不想丢弃原始上下文时,可以使用它 如您所说,为了使新异常具有关于前一个异常的信息,您需要将其作为构造函数参数传递给新异常 通常,空内部异常

我已经阅读了MSDN,但我无法理解这个概念

如果我错了,请纠正我

innerexception将与当前异常一起使用

首先会发生内部异常,然后会发生当前异常(如果存在异常),这就是为什么会根据
null
检查
InnerException
。为了保留内部异常,我们必须将其作为参数传递


我说的对吗?

内部异常是导致当前异常的异常

当您希望呈现与代码捕获的异常不同的异常,但又不想丢弃原始上下文时,可以使用它

如您所说,为了使新异常具有关于前一个异常的信息,您需要将其作为构造函数参数传递给新异常


通常,空内部异常意味着当前异常是异常情况的根本原因。

当您到达
catch
块时,异常对象是只读的,有时您的代码无法处理异常,但是它可以通过创建一个新的异常并将最初抛出的异常包装在其中来添加更多信息。这使得您可以添加信息,但不需要逐字段复制原始异常中的每一条信息(如果您不知道将引发的异常的类型,这甚至可能是不可能的)

这里是我的一个项目中的一个稍微修改过的片段,它使用了一些处理异常的所有东西

    private void SomeFunction(string username, string password)
    {
        try
        {
            try
            {
                _someObject.DoSpecialPrivilegedFunction(username, password);
            }
            catch (UnauthorizedAccessException ex)
            {
                throw new UserUnauthorizedException(username, "DoSpecialPrivilegedFunction", ex);
            }
            catch (IOException ex)
            {
                throw new UserModuleActionException("A network IO error happend.", username, "DoSpecialPrivilegedFunction", ex);
            }

            //Other modules

        }
        catch (Exception ex)
        {
            //If it is one of our custom expections, just re-throw the exception.
            if (ex is UserActionException)
                throw;
            else
                throw new UserActionException("A unknown error due to a user action happened.", username, ex);
        }
    }

//elsewhere
[Serializable]
public class UserUnauthorizedException : UserModuleActionException
{
    private const string DefaultMessage = "The user attempted to use a non authorized module";

    public UserUnauthorizedException()
        : base(DefaultMessage)
    {
    }

    public UserUnauthorizedException(string message) 
        : base(message)
    {
    }

    public UserUnauthorizedException(string message, Exception innerException) 
        : base(message, innerException)
    {
    }

    public UserUnauthorizedException(string username, string module, Exception innerException = null) : base(DefaultMessage, username, module, innerException)
    {
    }

    protected UserUnauthorizedException(SerializationInfo info, StreamingContext context) 
        : base(info, context)
    {
    }
}

[Serializable]
public class UserModuleActionException : UserActionException
{
    private readonly string _module;

    public UserModuleActionException()
    {
    }

    public UserModuleActionException(string message) : base(message)
    {
    }

    public UserModuleActionException(string message, Exception innerException) : base(message, innerException)
    {
    }

    public UserModuleActionException(string message, string username, string module, Exception innerException = null)
        : base(message, username, innerException)
    {
        _module = module;
    }

    protected UserModuleActionException(SerializationInfo info, StreamingContext context) 
        : base(info, context)
    {
    }

    public virtual string Module
    {
        get { return _module; }
    }

    public override string Message
    {
        get
        {
            string s = base.Message;
            if (!String.IsNullOrEmpty(_module))
            {
                return s + Environment.NewLine + String.Format("Module: {0}", _module);
            }
            return base.Message;
        }
    }
}

[Serializable]
public class UserActionException : Exception
{
    private readonly string _username;

    public UserActionException()
    {
    }

    public UserActionException(string message)
        : base(message)
    {
    }

    public UserActionException(string message, Exception innerException)
        : base(message, innerException)
    {
    }

    public UserActionException(string message, string username, Exception innerException = null)
        : base(message, innerException)
    {
        _username = username;
    }

    protected UserActionException(SerializationInfo info, StreamingContext context) 
        : base(info, context)
    {
    }

    public override string Message
    {
        get
        {
            string s = base.Message;
            if (!String.IsNullOrEmpty(_username))
            {
                return s + Environment.NewLine + String.Format("Username: {0}", _username);
            }
            return base.Message;
        }
    }

    public virtual string Username
    {
        get { return _username; }
    }
}

您可以看到下面的代码

第一步,我将“abc”解析为整数。它将引发格式化异常

在catch块中,我尝试打开一个文本文件来记录异常消息。但是这个文件不存在。将引发FileNotFoundException

我想知道是什么引发了第二个异常,所以我将第一个异常(或FormatException)添加到第二个异常的构造函数中

现在,第一个异常是第二个异常的InnerException

在catch块中,我可以访问InnerException的属性以了解第一个异常是什么

它有用吗

using System;
using System.IO;
public class Program
{
    public static void Main( )
    {
        try
        {
            try
            {
                var num = int.Parse("abc"); // Throws FormatException               
            }
            catch ( FormatException fe )
            {
                try
                {
                    var openLog = File.Open("DoesNotExist", FileMode.Open);
                }
                catch
                {
                    throw new FileNotFoundException("NestedExceptionMessage: File `DoesNotExist` not found.", fe );
                }                              
            }
        }
        // Consider what exception is thrown: FormatException or FileNotFoundException?
        catch ( FormatException fe)
        {
            // FormatException isn't caught because it's stored "inside" the FileNotFoundException
        }
        catch ( FileNotFoundException fnfe ) 
        {
            string inMes="", outMes;
            if (fnfe.InnerException != null)
                inMes = fnfe.InnerException.Message; // Inner exception (FormatException) message
            outMes = fnfe.Message;
            Console.WriteLine($"Inner Exception:\n\t{inMes}");
            Console.WriteLine($"Outter Exception:\n\t{outMes}");
        }        
    }
}
控制台输出

Inner Exception:
    Input string was not in a correct format.
Outter Exception:
    NestedExceptionMessage: File `DoesNotExist` not found.

Outter异常是指最终抛出的嵌套最深的异常。内部异常指的是最浅(范围内)的异常。

因此您通常只对内部异常感兴趣,但请注意,该异常也可能有其内部异常。您可能需要运行自己的例程来查找最内部的异常。可能需要获取根异常,即可能存在多个内部异常。。。而(ex.InnerException!=null)ex=ex.InnerException;关于这一主题的好教程,来自好的老Kudvenkat,适用于偶然发现这个问题的人:异常中的异常。。我喜欢称之为“开端”。外在的东西怎么可能是深刻的,内在的东西怎么可能是肤浅的?难道不是相反吗?