Exception.InnerException 属性的使用

例子(部分代码):

复制代码
protected void Button3_Click(object sender, EventArgs e)
{
try
{
Divide(10,0);
}
catch (Exception ex)
{
Label3.Text = "产生的异常信息如下:<br/>";
Label3.Text += ex.Message + "<br/>";
////直接使用InnerException属性可以获取内部异常的信息
Label3.Text += ex.InnerException.Message;
}
}
//div方法将抛出其产生的异常, 并传递内部异常
private double Divide(int x, int y)
{
try
{
return DivideOperation(x, y);
}
catch (Exception ex)
{
//使用Exception重载的构造函数传递内部异常
throw new Exception("来自Divide方法的异常", ex);
}
}
////产生一个被0除的异常
private double DivideOperation(int x, int y)
{
try
{
return x / y;
}
catch (DivideByZeroException ex)
{
throw new DivideByZeroException("产生了一个试图除以零的异常, 来自DivideOperation方法!");
}
}
复制代码

在Divide方法中,调用了DivideOperation方法,首先DivideOperation方法会抛出一个DivideByZero 
-Exception类型的异常,Divide方法将捕获该异常,但是Divide并不是直接吞没了该异常,而是抛出了一个异常,并将由DivideOperation方法产生的异常作为Exception构造函数的一个参数向外传递,这种传递异常的方式称为异常传递。当捕捉这种嵌套类型的异常时,开发人员可以使用Exception类的InnerException属性来获取内部异常的详细信息,这是一个Exception类型的属性

输出结果:

产生的异常信息如下:

来自Divide方法的异常

产生了一个试图除以零的异常, 来自DivideOperation方法!

 

遍历所有Exception.InnerException 属性的示例

复制代码
public static class ExceptionExtensions
    {
        public static Exception GetOriginalException(this Exception ex)
        {
            if (ex.InnerException == null)
            {
                return ex;
            }
            else
            {
                return ex.InnerException.GetOriginalException();
            }
        }

        public static Exception GetAllExceptionInfo(this Exception ex, ref string message)
        {
            message += "|" + ex.Message;
            if (ex.InnerException == null)
            {
                return ex;
            }
            else
            {
                return ex.InnerException.GetAllExceptionInfo(ref message);
            }
        }
    }
复制代码

我的测试代码:

复制代码
private ResultModel MyTest(HttpContext context)
        {
            //int i = 100, j=0;
            //var r = i / j;

            //var result = new ResultModel();
            //result = null;
            //var xxx = result.Data;

            try
            {
                Divide(10, 0);
            }
            catch (Exception ex)
            {
                var error = "产生的异常信息如下:<br/>";
                //error += ex.Message + "<br/>";
                ////直接使用InnerException属性可以获取内部异常的信息
                error += ex.ToString();
            }

            return new ResultModel(1);
        }

        //div方法将抛出其产生的异常, 并传递内部异常
        private double Divide(int x, int y)
        {
            try
            {
                return DivideOperation(x, y);
            }
            catch (Exception ex)
            {
                //使用Exception重载的构造函数传递内部异常
                throw new Exception("来自Divide方法的异常", ex);
                //throw ex;
            }
        }
        ////产生一个被0除的异常
        private double DivideOperation(int x, int y)
        {
            try
            {
                return x / y;
            }
            catch (DivideByZeroException ex)
            {
                //throw new DivideByZeroException("产生了一个试图除以零的异常, 来自DivideOperation方法!");
                throw new Exception("产生了一个试图除以零的异常, 来自DivideOperation方法!", ex);
                //throw ex;
            }
        }
复制代码

 

posted on   itjeff  阅读(167)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· .NET10 - 预览版1新功能体验(一)
历史上的今天:
2015-04-04 SQL字符串转换为数组

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示