错误?抛出投掷前 [英] Bug?! throw vs. throw ex

查看:72
本文介绍了错误?抛出投掷前的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想重新抛出一个例外,以免丢失原始堆栈

跟踪信息。我明白我可以使用throw,比如:


试试{

DoIt();

}

catch(Exception ex){

DoSomeExceptionStuff(ex);

throw;

}

但是,这显然不会保留堆栈跟踪 - 它会丢失它

就像使用throw ex;的确!我目前正在使用.NET 1.1 SP1。

这是一个已知的错误吗?为什么有这么多人报告它对他们有用?

他们真的试过了吗?


为什么v.1.1 SP1中会出现这样的错误?

有谁能帮忙解释一下?


提前致谢。

I want to "rethrow" an exception so as not to lose the original stack
trace information. I understood that I could use throw, like:

try {
DoIt();
}
catch (Exception ex) {
DoSomeExceptionStuff(ex);
throw;
}
However, this apparently does NOT preserve the stack trace--it loses it
just like using throw ex; does! I am currently using .NET 1.1 SP1. Is
this a known bug? Why do so many people report that it works for them?
Have they actually tried it?

Why would a bug like this exist in v. 1.1 SP1?
Can anyone help shed some light on this?

Thanks in advance.

推荐答案

重新抛出不会保留堆栈。这不是一个bug。你需要保留

堆栈,然后用修改后的堆栈抛出一个新的异常


-

问候,

Alvin Bruney [MVP ASP.NET]


[无耻作者插件]

带.NET的Microsoft Office Web Components黑皮书

现已推出@ http://tinyurl.com/27cok

-------------------------------------------- --------------

" GoogleNewsReaderMan" <螺*********** @ hotmail.com>在消息中写道

新闻:11 ********************* @ c13g2000cwb.googlegro ups.com ...
the rethrow does not preserve the stack. it is not a bug. You need preserve
the stack and then throw a new exception with the amended stack

--
Regards,
Alvin Bruney [MVP ASP.NET]

[Shameless Author plug]
The Microsoft Office Web Components Black Book with .NET
Now Available @ http://tinyurl.com/27cok
----------------------------------------------------------
"GoogleNewsReaderMan" <lo***********@hotmail.com> wrote in message
news:11*********************@c13g2000cwb.googlegro ups.com...
我想重新抛出一个例外,以免丢失原始堆栈跟踪信息。我明白我可以使用throw,比如:

尝试{
DoIt();
}
catch(Exception ex){
DoSomeExceptionStuff(ex );
抛出;
}

然而,这显然不会保留堆栈跟踪 - 它会丢失它就像使用throw ex一样;的确!我目前正在使用.NET 1.1 SP1。这是一个已知的bug吗?为什么有这么多人报告它对他们有用?
他们真的尝试过了吗?

为什么在1.1.1 SP1中会出现这样的错误?
任何人都可以帮忙对此有所了解?

提前致谢。
I want to "rethrow" an exception so as not to lose the original stack
trace information. I understood that I could use throw, like:

try {
DoIt();
}
catch (Exception ex) {
DoSomeExceptionStuff(ex);
throw;
}
However, this apparently does NOT preserve the stack trace--it loses it
just like using throw ex; does! I am currently using .NET 1.1 SP1. Is
this a known bug? Why do so many people report that it works for them?
Have they actually tried it?

Why would a bug like this exist in v. 1.1 SP1?
Can anyone help shed some light on this?

Thanks in advance.



>我想重新抛出一个例外,以免丢失原始堆栈
> I want to "rethrow" an exception so as not to lose the original stack
跟踪信息。




我相信堆栈跟踪是作为throw的一部分填充的,不是在创建

异常对象时。


创建一个新的异常并将旧的异常填入其中作为内部

例外。新的应该有新的堆栈跟踪和旧的堆栈跟踪

原始堆栈跟踪。



I believe the stack trace is populated as part of the throw, not when the
exception object is created.

Create a new exception and stuff the old one inside it as the "inner
exception". The new one should have the new stack trace and the old one the
original stack trace.


不确定它是否有效,但尝试


抛出新的异常(一些消息,前)


" GoogleNewsReaderMan" <螺*********** @ hotmail.com>在消息中写道

新闻:11 ********************* @ c13g2000cwb.googlegro ups.com ...
Not sure if it works, but try

throw new Exception("some message", ex)

"GoogleNewsReaderMan" <lo***********@hotmail.com> wrote in message
news:11*********************@c13g2000cwb.googlegro ups.com...
我想重新抛出一个例外,以免丢失原始堆栈跟踪信息。我明白我可以使用throw,比如:

尝试{
DoIt();
}
catch(Exception ex){
DoSomeExceptionStuff(ex );
抛出;
}

然而,这显然不会保留堆栈跟踪 - 它会丢失它就像使用throw ex一样;的确!我目前正在使用.NET 1.1 SP1。这是一个已知的bug吗?为什么有这么多人报告它对他们有用?
他们真的尝试过了吗?

为什么在1.1.1 SP1中会出现这样的错误?
任何人都可以帮忙对此有所了解?

提前致谢。
I want to "rethrow" an exception so as not to lose the original stack
trace information. I understood that I could use throw, like:

try {
DoIt();
}
catch (Exception ex) {
DoSomeExceptionStuff(ex);
throw;
}
However, this apparently does NOT preserve the stack trace--it loses it
just like using throw ex; does! I am currently using .NET 1.1 SP1. Is
this a known bug? Why do so many people report that it works for them?
Have they actually tried it?

Why would a bug like this exist in v. 1.1 SP1?
Can anyone help shed some light on this?

Thanks in advance.



这篇关于错误?抛出投掷前的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆