我有下面这段代码,它调用一个MethodInfo:
try
{
registrator.Method.Invoke(instance, parameters);
}
catch{
registrator.FailureType = RegistratorFailureType.ExceptionInRegistrator;
//registrator.Exception = e;
}
注册器只是一个MethodInfo包装器,方法属性是MethodInfo对象本身。参数是和object[],而instance是方法声明类型(使用Activator.Create创建)的正确实例。
这个方法看起来像这样(我是在测试异常捕获):
class Test : Plugin, ITest
{
public void Register(IWindow window)
{
throw new Exception("Hooah");
}
}
问题是:异常永远不会被捕获,Visual Studio未捕获的异常气泡会弹出。
这是在带有.NET 4.0的VS 2010中
发布于 2010-08-30 18:08:41
无论如何,问题不在您的代码中。
在Debug/Exceptions菜单中,删除所有复选框。
应该能行得通。
发布于 2010-08-30 17:07:31
你试过这个吗?
在Program.cs中
Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
以及Run方法上的try/catch:
try
{
Application.Run(new Form1());
}
catch (Exception ex)
{
}
发布于 2010-08-30 17:23:55
问题不在于您所显示的代码。
我试过这个:
void Main()
{
Test instance = new Test();
object[] parameters = new object[] { null };
MethodInfo method = typeof(Test).GetMethod("Register");
try
{
method.Invoke(instance, parameters);
}
catch
{
Console.Out.WriteLine("Exception");
}
}
interface ITest { }
interface IWindow { }
class Plugin { }
class Test : Plugin, ITest
{
public void Register(IWindow window)
{
throw new Exception("Hooah");
}
}
正如预期的那样,它输出了"Exception“。你需要给我们看更多的代码。
如果我像这样修改代码:
catch(Exception ex)
{
Console.Out.WriteLine(ex.GetType().Name + ": " + ex.Message);
}
然后我得到一个TargetInvocationException,其中的InnerException属性是您抛出的异常。
https://stackoverflow.com/questions/3599078
复制相似问题