Revit 2022
After much effort trying to fix some code I noticed that whenever an unhandled exception is thrown inside an IExternalEventHandler.Execute() callback it gets swallowed by the API and does not break code. All you see is a small notice in the output window. Is this a bug or intentional? I've also noticed that even if a transaction was created and committed successfully, any unhandled exception thrown afterwards effectively cancels that transaction without warning.
class MyEventHandler : IExternalEventHandler
{
public void Execute(UIApplication app)
{
// this will get swallowed??
throw new Exception("catch me");
}
public string GetName() => nameof(MyEventHandler);
}
[Transaction(TransactionMode.Manual)]
public class MyCommand : IExternalCommand
{
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{
var handler = new MyEventHandler();
var externalEvent = ExternalEvent.Create(handler);
externalEvent.Raise();
return Result.Succeeded;
}
}
First, IExternalEventHandler instance is called asynchronously. Exceptions will not work correctly in asynchronous context except for special handling. I believe it is intentional. Internally all the raised IExternalEventHandler instances are queued to be consumed by Revit one by one. Your plugin and other plugins share the same queue. An exception in your handler should not break the following handlers which may be created by other plugins. So Revit isolates every IExternalHandler instance to keep its exceptions within its own context.
Can't find what you're looking for? Ask the community or share your knowledge.