AppDomain.CurrentDomain.UnhandledException in Revit Addin

主宰稳场 提交于 2021-01-28 22:02:25

问题


I wanted to use a crash reporter in my own Revit addin but AppDomain.CurrentDomain.UnhandledException is never called. It seems Revit itself manages the unhandled expections and shows its own crash dialog. What should I do to catch all unhandled exceptions in revit addin before Revit cathes them?

I already tried the following lines of code but it does not work: it never enters the handler method:

public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
    {
        AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
    }

    private void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
    {
        throw new NotImplementedException();
    }

Thanks for any help


回答1:


Any particular reason you didn't want to handle this logic in a try-catch block as opposed to subscribing to events?

In this case, by the way, I believe you're subscribing to the application window's event handlers - Revit will never let it get that far up the chain which is why it's not entering the handler method you've created.




回答2:


Are you trying to create a generic crash reporter that catches exceptions unrelated to your add-in? I don't think this is possible with a Revit Add-In. If that is what you are trying to do I would look into parsing the active journal file for specific "error" keywords". This will have a lot more information about the internal failures of Revit.

If you are trying to create a crash reporter for your own add-in then I would just make sure you surround code in your methods called by Revit with a Try/Catch block that logs/reports any exceptions thrown.

[Transaction(TransactionMode.ReadOnly)]
public class MyCommand: IExternalCommand 
{
    public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements) {
    try 
    {
        //do stuff
    } catch (Exception exp) {
        CrashReporter.LogException(exp);
        throw;
    }
}

Then apply the same to any other entry points into your add-in such as IExternalApplication.StartUp(), IExternalApplication.ShutDown() etc..



来源:https://stackoverflow.com/questions/40627262/appdomain-currentdomain-unhandledexception-in-revit-addin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!