当前位置:  开发笔记 > 编程语言 > 正文

在ASP.NET C中抛出异常#

如何解决《在ASP.NETC中抛出异常#》经验,为你挑选了2个好方法。

只是说throw;throw ex;假设ex是你正在捕捉的例外之间有区别吗?



1> GEOCHET..:

throw ex;将擦除你的堆栈跟踪.除非你想清除堆栈跟踪,否则不要这样做.只是用throw;



2> Eric Schoono..:

这是一个简单的代码片段,有助于说明差异.不同之处在于throw ex将重置堆栈跟踪,就好像行" throw ex;"是异常的来源一样.

码:

using System;

namespace StackOverflowMess
{
    class Program
    {
        static void TestMethod()
        {
            throw new NotImplementedException();
        }

        static void Main(string[] args)
        {
            try
            {
                //example showing the output of throw ex
                try
                {
                    TestMethod();
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }

            Console.WriteLine();
            Console.WriteLine();

            try
            {
                //example showing the output of throw
                try
                {
                    TestMethod();
                }
                catch (Exception ex)
                {
                    throw;
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }

            Console.ReadLine();
        }
    }
}

输出(注意不同的堆栈跟踪):

System.NotImplementedException: The method or operation is not implemented.
at StackOverflowMess.Program.Main(String[] args) in Program.cs:line 23

System.NotImplementedException: The method or operation is not implemented.
at StackOverflowMess.Program.TestMethod() in Program.cs:line 9
at StackOverflowMess.Program.Main(String[] args) in Program.cs:line 43

推荐阅读
mobiledu2402851323
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有