重新抛出异常 [英] Re-Throwing Exceptions

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

问题描述

嘿,

我使用下面的代码:

试试

{

//一些代码有一个例外

}

catch(exception ex)

{

if(ex.innerException!= null) ){throw ex.innerException;}

}


一切正常,但问题是异常的堆栈跟踪是

由于抛出而改变了!


有没有办法在不更改堆栈跟踪的情况下抛出异常?

未来感谢


--sternr

解决方案

sternr写道:

嘿,
我使用以下代码:
尝试
//
//一些代码有异常
}
catch(Exception ex)
{
if(ex.innerException!= null){throw ex.innerException;}
}

一切正常,但问题是异常的堆栈跟踪是因为扔了改变了!

有没有ay抛出异常而不改变它的堆栈跟踪?
谢谢你

--sternr



为什么堆栈跟踪应保持不变重新抛出

同样的例外情况?当它再次被抛出时,堆栈跟踪应该更新。

但是如果你重新抛出外部异常(在你的例子中为ex)而不是内部异常的堆栈,那就是内部异常的堆栈跟踪将保持不变。

rg


只是简单地抛出;将工作。抓住

异常并重新抛出内部异常是相当无意义的IMO?为什么这会有所帮助?


-


________________________

热烈的问候,

Alvin Bruney [MVP ASP.NET]


[无耻作者插件]

专业版VSTO.NET - Wrox / Wiley

OWC使用.NET的黑皮书
www.lulu.com/owc ,亚马逊

博客: http://www.msmvps.com/ blogs / alvin

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


" sternr" <圣**** @ gmail.com>在消息中写道

news:11 ********************** @ j55g2000cwa.googlegr oups.com ...

嘿,
我使用下面的代码:
尝试
//某些代码有异常
}
catch(异常) ex)
{
if(ex.innerException!= null){throw ex.innerException;}
}

一切正常,但问题是异常'的堆栈跟踪由于抛出而改变了!

有没有办法在不改变堆栈跟踪的情况下抛出异常?
非常感谢

--sternr



<" Alvin Bruney" < www.lulu.com/owc>>写道:

只是简单地抛出;将工作。


不,这将重新抛出* outer *例外。

它是一个相当无意义的IMO来捕获
异常并重新抛出内部例外?为什么这会有所帮助?




有些情况下会发生已知的包装级别,并且你想要b / b
摆脱它立即报告真正的异常。对于

实例,如果你使用反射创建一个插件实例,

捕获TargetInvocationException并重新抛出
$ b $可能是有意义的。 b内部异常。任何看过堆栈跟踪的人都会忽略TargetInvocationException

,这只是额外的

杂乱。


-

Jon Skeet - < sk *** @ pobox.com>
http://www.pobox.com/~skeet 博客: http://www.msmvps.com/jon.skeet

如果回复该群组,请不要给我发邮件


Hey,
I use the following code:
try
{
//Some code that has an exception
}
catch (Exception ex)
{
if (ex.innerException!=null) {throw ex.innerException;}
}

All works well, but the problem is that the exception''s stacktrace is
changed because of the throw!

Is there a way to throw an exception WITHOUT changing it''s stacktrace?
Thanks ahead

--sternr

解决方案

sternr wrote:

Hey,
I use the following code:
try
{
//Some code that has an exception
}
catch (Exception ex)
{
if (ex.innerException!=null) {throw ex.innerException;}
}

All works well, but the problem is that the exception''s stacktrace is
changed because of the throw!

Is there a way to throw an exception WITHOUT changing it''s stacktrace?
Thanks ahead

--sternr


Why the stacktrace should be left unchanged in case of re-throwing the
same exception? When its thrown again the stacktrace should be updated.
But if you rethrow the outer exception (ex in your example) instead of
innerException, the inner exceptions''s stack trace will be unchanged.
rg


just simply throw; will work. it''s rather pointless IMO to catch an
exception and rethrow the inner exception? Why would that be helpful?

--

________________________
Warm regards,
Alvin Bruney [MVP ASP.NET]

[Shameless Author plug]
Professional VSTO.NET - Wrox/Wiley
The O.W.C. Black Book with .NET
www.lulu.com/owc, Amazon
Blog: http://www.msmvps.com/blogs/alvin
-------------------------------------------------------

"sternr" <St****@gmail.com> wrote in message
news:11**********************@j55g2000cwa.googlegr oups.com...

Hey,
I use the following code:
try
{
//Some code that has an exception
}
catch (Exception ex)
{
if (ex.innerException!=null) {throw ex.innerException;}
}

All works well, but the problem is that the exception''s stacktrace is
changed because of the throw!

Is there a way to throw an exception WITHOUT changing it''s stacktrace?
Thanks ahead

--sternr



<"Alvin Bruney" <www.lulu.com/owc>> wrote:

just simply throw; will work.
No, that will rethrow the *outer* exception.
it''s rather pointless IMO to catch an
exception and rethrow the inner exception? Why would that be helpful?



There are cases where a known level of wrapping occurs, and you want to
get rid of that so that the real exception is reported immediately. For
instance, if you''re creating an instance of a plugin using reflection,
it might make sense to catch TargetInvocationException and rethrow the
inner exception. The TargetInvocationException should basically be
ignored by anyone looking at a stack trace anyway, and it''s just extra
clutter.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too


这篇关于重新抛出异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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