什么是内在异常

本文关键字:异常 什么 | 更新日期: 2023-09-27 17:56:23

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

如果我错了,请纠正我,

内部异常将与当前异常一起使用。

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

我说的对吗?

什么是内在异常

您可以看到下面的代码。

第一步,我将"abc"解析为int。它会提高FormatException.

在第一个catch块(处理引发的FormatException)中,我尝试打开一个文本文件来记录异常消息。但是此文件不存在,因此引发了第二个异常,这次的类型为 FileNotFoundException

我想存储导致引发第二个异常的原因,所以我将第一个异常(FormatException 类型的fe)添加到第二个异常的 FileNotFoundException(String, Exception) 构造函数中传递的 fe 参数(类型 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 innerMessage = "", outerMesage;
            if (fnfe.InnerException != null)
                innerMessage = fnfe.InnerException.Message; // Inner exception (FormatException) message
            outerMesage = fnfe.Message;
            Console.WriteLine($"Inner Exception:'n't{innerMessage}");
            Console.WriteLine($"Outer Exception:'n't{outerMesage}");
        }        
    }
}

控制台输出

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

内部异常是指最终引发的更深的嵌套异常。外部异常是指最浅(在范围内)的异常。

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

它用于以下情况:您希望显示与代码捕获的异常不同的异常,但又不想丢弃原始上下文。

为了使新异常

具有有关前一个异常的信息,就像您所说的那样,将其作为构造函数参数传递给新异常。

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

异常对象在你到达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; }
    }
}