Revit API Forum
Welcome to Autodesk’s Revit API Forums. Share your knowledge, ask questions, and explore popular Revit API topics.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

IExternalEventHandler swallows unhandled Exceptions

1 REPLY 1
Reply
Message 1 of 2
JOfford_13
355 Views, 1 Reply

IExternalEventHandler swallows unhandled Exceptions

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;
        }

    }

 

Labels (2)
1 REPLY 1
Message 2 of 2
Kennan.Chen
in reply to: JOfford_13

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.

Post to forums  

Rail Community


Autodesk Design & Make Report