locked
How to make common handler in C# package to prevent VS crash RRS feed

  • Question

  • Hi,

     

    I am developing VS package in C#. I have some WinForms UI (that is shown in VS toll windows) and sometimes I have unhandled C# exceptions (usually from UI event handlers) that caused VS to crash. Is there any way to handle all unhandled exceptions to prevent VS crash? May be I can register some global handler?

    Really it is not good that unhandled managed exception in package is crashing VS. I think that this situations should be handled by managed layer in VS. Of course, I will try to put try/catch to all event handlers, but it will be very usefull for me now to have such global handler. So can anybody help me with this? Or this is impossible?

     

    Thanks,

    Max

    Thursday, December 2, 2010 11:17 AM

Answers

  • VS catching all exceptions is not a good plan.  Should we catch Access violations?  Null refs?  Stack overflows?  In general exceptions should indicate exceptional (unexpected) occurances.  As such it is NOT safe for us (VS) to just blindly catch / ignore all exceptions.  This could lead to a less stable app since we would continue running but the application is in an unknown state, in memory data or data structures could be corrupted, the user could lose data.  At least with a crash we get an immediate notification, hopefully some useful information to debug and we don't put the user at risk of continuing the application in a bad state. 

    Further with the extensibility in VS there isn't a way to 'handle all exceptions at the managed layer' as some callbacks flow directly from the main message loop (native code) directly into third party, managed code.  We can't know when we are dispatching the message if that is going to happen and the only way to guard against it would be to catch every singl exception, which is really just illusory stability.

    In general your package is responsible for its own exceptions.  If you have 'unexpected' exceptions you need to find out where/why they are occuring and deal with them appropriately.  There is no 'global' hook that would only catch say your exceptions.  There are 'global' hooks for unhandled managed exceptions but that is for ALL unhandled managed exceptions in all of VS, not just your package, thus I would strongly recommend against using them.

    Ryan

    • Marked as answer by Victor_Chen Friday, December 10, 2010 9:04 AM
    Thursday, December 2, 2010 5:44 PM

All replies

  • VS catching all exceptions is not a good plan.  Should we catch Access violations?  Null refs?  Stack overflows?  In general exceptions should indicate exceptional (unexpected) occurances.  As such it is NOT safe for us (VS) to just blindly catch / ignore all exceptions.  This could lead to a less stable app since we would continue running but the application is in an unknown state, in memory data or data structures could be corrupted, the user could lose data.  At least with a crash we get an immediate notification, hopefully some useful information to debug and we don't put the user at risk of continuing the application in a bad state. 

    Further with the extensibility in VS there isn't a way to 'handle all exceptions at the managed layer' as some callbacks flow directly from the main message loop (native code) directly into third party, managed code.  We can't know when we are dispatching the message if that is going to happen and the only way to guard against it would be to catch every singl exception, which is really just illusory stability.

    In general your package is responsible for its own exceptions.  If you have 'unexpected' exceptions you need to find out where/why they are occuring and deal with them appropriately.  There is no 'global' hook that would only catch say your exceptions.  There are 'global' hooks for unhandled managed exceptions but that is for ALL unhandled managed exceptions in all of VS, not just your package, thus I would strongly recommend against using them.

    Ryan

    • Marked as answer by Victor_Chen Friday, December 10, 2010 9:04 AM
    Thursday, December 2, 2010 5:44 PM
  • Hi Max,

     

    Is it resolved?

     

    Best Regards,

    Ziwei Chen

     

    MSDN Subscriber Support in Forum
    If you have any feedback on our support, please contact msdnmg @ microsoft.com

     


    Ziwei Chen [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, December 10, 2010 9:04 AM