Hello Dustin,
I tried to reproduce catching an unhandled exception using a sample AddIn
(created using a VS wizard), but it does not seem to work.
When I click on MyAddIn1 from the Tools menu, I see the "Exec" message box,
but then nothing else occurs (no messagebox - nothing).
(I can debug the addin and see the exception being thrown, but after the
throw it is lost in unmanaged code, and I never hit the breakpoint on the
first line of the unhandled exception handler).
I tried throwing an exception from my package and this is not caught either
(it goes to the regular VS global error handler).
Here is my Toolbar command for my addin:
public void Exec(string commandName, vsCommandExecOption executeOption,
ref object varIn, ref object varOut, ref bool handled)
{
handled = false;
if(executeOption == vsCommandExecOption.vsCommandExecOptionDoDefault)
{
if(commandName == "MyAddin1.Connect.MyAddin1")
{
MessageBox.Show("Exec");
throw new InvalidOperationException();
handled = true;
return;
}
}
}
First line of OnConnection():
public void OnConnection(object application, ext_ConnectMode connectMode,
object addInInst, ref Array custom)
{
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
The unhandled exception handler:
void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs
e)
{
if (e.ExceptionObject == null)
return;
if (!e.ExceptionObject.GetType().IsSubclassOf(typeof(Exception)))
return;
if (e.ExceptionObject.GetType() == typeof(System.Threading.ThreadAbortException))
return;
Exception lException = (Exception)e.ExceptionObject;
MessageBox.Show(lException.Message);
}
Dustin Campbell - 26 Oct 2005 21:45 GMT
> I tried to reproduce catching an unhandled exception using a sample
> AddIn (created using a VS wizard), but it does not seem to work.
[quoted text clipped - 7 lines]
> I tried throwing an exception from my package and this is not caught
> either (it goes to the regular VS global error handler).
Correct. If an exception gets handled by Visual Studio it isn't
unhandled anymore. Visual Studio is designed to recover from faults
like these so that it can't be easily crashed by a package or add-in.
Here's what happens, when an exception is thrown from a COM-Callable
Wrapper (CCW) like a managed add-in or package, that exception gets
turned into an appropriate HRESULT by the CLR. In addition, the CCW
supports IErrorInfo so that COM can get the error message of the
exception and any other details. So, if your exception gets back to
Visual Studio, it gets eaten up.
To deal this, we wrap try/catch blocks around major entry points into
our add-in and package. For example, we have try/catch blocks around
the OnConnection and OnDisconnection so that we can display a nice
exception dialog if something goes wrong during start up or shut down.
In addition, every event and sink listened to in our architecture goes
through one central location so this is easy to wrap a try/catch around.
UnhandledException *can* be fired if the exception occurs in pure .NET
code. Exceptions raised from a .NET background thread is a great
example.
So, if Visual Studio calls your method, try/catch blocks are necessary
to display a meaningful dialog. But, UnhandledException can be used to
catch any exceptions that Visual Studio would miss.
I hope that explains the situation!

Signature
Best Regards,
Dustin Campbell
Developer Express, Inc
DarenYong - 27 Oct 2005 00:29 GMT
Ah - thanks for clearing that up!
Best regards,
Daren.