任何人都可以解释投掷和投掷之间的区别 [英] can any one explain difference between throw and throw ex
问题描述
我无法理解投掷和投掷之间的区别。如果我使用投掷或投掷会发生什么事情?
i was unable to understand difference between throw and throw ex .what happens if i use throw or throw ex???
try
{
}
catch(Exception ex)
{
throw ex;
}
try
{
}
catch(Exception ex)
{
throw;
}
推荐答案
除了解决方案1之外,还有另一个关于何时使用/不使用的讨论throw ex;
此处 [ ^ ] - 它包括对封底内发生的事情的解释
我非常相信经验证据,所以我做了以下几点来证明大多数人的观点...
我创建了一个带有多行文本框和按钮的WinForm,然后添加了以下代码
Further to solution 1 there is another discussion of when to use / not usethrow ex;
here[^] - it includes explanations of what is happening under the covers
I'm a great believer in empirical evidence so I did the following to demonstrate what most people are on about...
I created a WinForm with a multi-line text box and a button then added the following code
private void button1_Click(object sender, EventArgs e)
{
try
{
Level1(); //This is line 17
}
catch (Exception ex)
{
this.textBox1.Text = ex.StackTrace;
}
}
private void Level1()
{
try
{
Level2();
}
catch (Exception ex)
{
throw ex; //This is line 32
}
}
private void Level2()
{
try
{
Level3();
}
catch (Exception ex)
{
throw ex; //This is line 43
}
}
private void Level3()
{
throw new Exception("Test"); //This is line 48
}
当我运行程序并单击按钮时,我在文本框中显示此文本(仅编辑以删除临时项目文件夹的路径)
When I run the program and click the button, I get this text in the textbox (edited only to remove the path to my temporary projects folder)
在WindowsFormsApplication1.Form1.Level1()中的WindowsFormsApplication1 \Form1.cs:第32行
在WindowsFormsApplication1.Form1.button1_Click(Object sender,EventArgs e)在WindowsFormsApplication1 \Form1中.cs:第17行
at WindowsFormsApplication1.Form1.Level1() in WindowsFormsApplication1\Form1.cs:line 32
at WindowsFormsApplication1.Form1.button1_Click(Object sender, EventArgs e) in WindowsFormsApplication1\Form1.cs:line 17
然后我改变了Level1和Level2函数,如下所示(即从throw
I then changed the Level1 and Level2 functions as follows (i.e. remove the "ex" from the throw
private void Level1()
{
try
{
Level2();
}
catch (Exception ex)
{
throw; //This is line 32
}
}
private void Level2()
{
try
{
Level3();
}
catch (Exception ex)
{
throw; //This is line 43
}
}
并重新运行该程序。这次文本框包含
and re-ran the program. This time the text box contained
,位于WindowsFormsApplication1 \Form1中的WindowsFormsApplication1.Form1.Level3()。 cs:第48行
WindowsFormsApplication1.Form1.Level2()在WindowsFormsApplication1 \Form1.cs中的
:第43行
$ b WindowsFormsApplication1.Form1.Form1.Level1()在WindowsFormsApplication1 \Form1.cs中的$ b:第32行
在WindowsFormsApplication1.Form1.button1_Click(Object sender,EventArgs e)中的WindowsFormsApplication1 \Form1.cs:line 17
at WindowsFormsApplication1.Form1.Level3() in WindowsFormsApplication1\Form1.cs:line 48
at WindowsFormsApplication1.Form1.Level2() in WindowsFormsApplication1\Form1.cs:line 43
at WindowsFormsApplication1.Form1.Level1() in WindowsFormsApplication1\Form1.cs:line 32
at WindowsFormsApplication1.Form1.button1_Click(Object sender, EventArgs e) in WindowsFormsApplication1\Form1.cs:line 17
如你所见,后一个版本(throw)给我提供 StackTrace
中的信息一直到原来的异常,而throw ex只给我信息回到最后 throw ex;
。
如果我使用 textBox1.Text = ex.ToString();
,我会有相同的信息差异,但我想绘制注意事实是StackTrace受到了影响。
我希望这有助于为你清理这里表达的意见和提供的各种链接的不同之处解决方案。
As you can see the latter version ("throw") gives me information in StackTrace
all the way down to the original exception whereas "throw ex" only gives me information back to the last throw ex;
.
I would have had the same difference in information if I'd used textBox1.Text = ex.ToString();
but I wanted to draw attention to the fact it is the StackTrace that is affected.
I hope this helps to clear up for you the differences in opinions being expressed here and on the various links provided in the solutions.
请看这里:
http:/ /www.dotnetperls.com/throw [ ^ ]
抛出ex
重置堆栈跟踪。抛出
保留堆栈跟踪。
抛出ex
抛出一个新异常,所以堆栈跟踪更改并重置。
Throw ex
resets the stack trace.Throw
preserves the stack trace.
Throw ex
throws a new exception, so the stack trace changes and is reset.
这篇关于任何人都可以解释投掷和投掷之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!