I think there is no way to do this in Revit main thread.
But if you will run your addin in separate thread exceptions from that thread will not be handled by Revit, and UnhandledExceptions event will be fired (most likely Revit will crash as well).
If your addin is on wpf you can launch it in separate thread like this, and even handle exceptions from that thread.
namespace ClassLibrary2
{
[Transaction(TransactionMode.Manual)]
public class Startup : IExternalCommand
{
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{
try
{
App.ThisApp.ShowFormSeparateThread();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
return Result.Succeeded;
}
}
}
_
public void ShowFormSeparateThread()
{
if ( !(_uiThread is null) && _uiThread.IsAlive )
return;
_uiThread = new Thread(() =>
{
SynchronizationContext.SetSynchronizationContext(
new DispatcherSynchronizationContext(Dispatcher.CurrentDispatcher));
Dispatcher.CurrentDispatcher.UnhandledException += (sender, args) =>
{
MessageBox.Show("Err" + args.Exception.Message);
args.Handled = true;
};
_mMyForm = new MainWindow();
_mMyForm.DataContext = new MainViewModel();
_mMyForm.Closed += (s, e) => Dispatcher.CurrentDispatcher.InvokeShutdown();
_mMyForm.Show();
Dispatcher.Run();
});
_uiThread.SetApartmentState(ApartmentState.STA);
_uiThread.IsBackground = true;
_uiThread.Start();
}
If you want you can get information about ALL exception in Revit even thrown by someone else addins, just use
AppDomain.CurrentDomain.FirstChanceException += (sender, args) =>
{
//your code here
};