开发者

Error catching in dynamically generated assembly

I'm having trouble with an application I've written, it's basically creating dynamically generated assemblies based on code input by the user.

It compiles and runs fine, but for some reason, sometimes when an Exception occurs in that compiled assembly, it crashes the main program even though everything is thoroughly coated in try/catch blocks.

I add try/catch blocks to surround the user's code in the generated assembly, and also try/catch around the Invocation of the assembly in my app:

StringBui开发者_Python百科lder verificationErrors = new StringBuilder();
argz[0] = "hello!";
argz[1] = verificationErrors;
object loResult = null;
try
{
   loResult = loObject.GetType().InvokeMember("doThis", BindingFlags.InvokeMethod, null, loObject, argz);
}
catch (Exception p)
{
   MessageBox.Show(p.Message);
}

It looks like the error is being thrown outside the scope of my app, so it doesn't know how to catch it or something to that effect... any ideas?


It's possible that the invocation of p.Message is throwing an exception. One would presume that we're using the default Message property, but it could be a custom exception that overrides the Message property.

I would wrap the code in the catch block with a further try...catch, and if it throws an error, just say "An unexpected error occurred", and log whatever you can about it.


If you run the program in debug and instruct Visual Studio to break when an exception is thrown, you should be able to inspect the stack trace and determine which method is throwing the exception.

  1. Go to Debug / Exceptions...
  2. Check the box in front of "Common Language Runtime Exceptions", in column "Thrown"
  3. Click "OK"
  4. Run the program in debug mode
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜