简体   繁体   English

try / catch块中未处理的异常

[英]Unhandled exception in try/catch block

This one is really baking my noodle... 这个真的是我的面条烤...

I've got some code. 我有一些代码。 When I run it, Visual Studio complains about an unhandled exception. 当我运行它时,Visual Studio会抱怨未处理的异常。 And, sure enough, there is an exception. 而且,果然, 一个例外。 However, it's blatently inside a try/catch block, so... how is that "unhandled"?? 但是,它在try / catch块内部,所以... 这是如何“未处理”?

I've searched around, and the only relevant hit I can find is related to LINQ. 我一直在搜索,我能找到的唯一相关命中与LINQ有关。 But I'm not using any LINQ, so that can't be it. 但我没有使用任何LINQ,所以不可能。

The relevant code looks something like this: 相关代码看起来像这样:

try
{
  method.Invoke(target, new object[0]);
}
catch (MyException e)
{
  Console.WriteLine(e);
}

The method being hit by Invoke() is different every time. Invoke()命中的方法每次都不同。 Some of these methods really do throw exceptions - which is fine. 其中一些方法确实会抛出异常 - 这很好。 But that's why I put it in a try-block! 但这就是为什么我把它放在试块中! I don't understand why VS is complaining that the exception isn't handled. 我不明白为什么VS抱怨没有处理异常。

(Yes, I have checked that the exception being thrown is a MyException object.) (是的,我已经检查过抛出的异常是MyException对象。)

At this point, my only guess is that it's somehow related to using reflection. 在这一点上,我唯一的猜测是它与使用反射有某种关系。 I really need to fix this though... Any suggestions? 我真的需要解决这个问题......有什么建议吗?

You should read the documentation for MethodInfo.Invoke . 您应该阅读MethodInfo.Invoke的文档。

Exceptions 例外

TargetInvocationException - The invoked method or constructor throws an exception. TargetInvocationException - 调用的方法或构造函数抛出异常。

Remarks 备注

If the invoked method throws an exception, the Exception.GetBaseException method returns the exception. 如果被调用的方法抛出异常,则Exception.GetBaseException方法返回异常。

Your exception is being wrapped in a TargetInvocationException . 您的异常被包装在TargetInvocationException

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM