如何捕获在AutoCAD.NET中抛出的未处理异常

4
在我的Autocad.NET应用程序中,我想使用log4net记录所有未处理的异常。AutoCAD本身会显示一个带有详细消息的错误对话框,因此必须有一种方法可以注册到特定事件。
我尝试在应用程序初始化时注册AppDomain.CurrentDomain.UnhandledException事件:
AppDomain.CurrentDomain.UnhandledException += (s, e) =>
{
     System.Exception exception = (System.Exception)e.ExceptionObject;
     log.Error(String.Format("Unhandled Exception: {0}\n{1}", exception.Message, exception.StackTrace));
};

但是该事件从未被触发。


也许这个异常由对话框处理。 - Amiram Korach
1
如果AutoCAD没有崩溃 - 这意味着,异常已经被AutoCAD处理了。 - opewix
有没有一种方法可以在AutoCAD的异常处理之前处理它? - mamuesstack
在另一个AppDomain中运行您的代码。 - Hans Passant
1
@HansPassant,有什么提示可以在AutoCAD扩展应用程序中完成吗? - mamuesstack
1个回答

4

在ObjectARX中,有一个名为acedDisableDefaultARXExceptionHandler的函数。您可以尝试使用P/Invoke来调用它。

    // EntryPoint may vary across autocad versions
    [DllImport("acad.exe", EntryPoint = "?acedDisableDefaultARXExceptionHandler@@YAXH@Z")]
    public static extern void acedDisableDefaultARXExceptionHandler(int value);

您也可以尝试使用 System.Windows.Forms.Application.ThreadException: http://through-the-interface.typepad.com/through_the_interface/2008/08/catching-except.html 最简单的方法是将您的所有代码都包装在 try/catch 块中。在 AutoCAD 中,有两种执行代码的方式:
使用命令
为避免重复代码,请声明一个如下的接口:
public interface ICommand
{
  void Execute();
}

然后在你的命令中使用它:
public class MyCommand : ICommand
{
  void Execute()
  {
    // Do your stuff here
  }
}

在定义命令的类中,使用此通用方法执行:

void ExecuteCommand<T>() where T : ICommand
{
  try
  {
    var cmd = Activator.CreateInstance<T>();
    cmd.Execute();
  }
  catch (Exception ex)
  {
    Log(ex);
  }
}

现在你的命令看起来是这样的:
[CommandMethod("MYCMD", CommandFlags.Modal)]
public void MyCommand()
{
  ExecuteCommand<MyCommand>();
}

在事件处理程序中

在这种情况下,由于需要事件参数,因此可以直接在try/catch中包装代码。


您的链接描述了我目前发现的最佳方法。谢谢。 - mamuesstack
调用acedDisableDefaultARXExceptionHandler来禁用也没对我产生任何改变。 - diimdeep

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接