locked
unhandled exception... even with a try catch loop RRS feed

  • Question

  • Hi,

    I have a Windows for application.

    I get a simple null pointer exception on perfoming an action.

    I also have a try-catch running over my Application.Run() function. (kind of like a global try catch)

     

    When I run this in debug mode, it enters the catch loop when it hits the null pointer exception. But this does not happen in runtime. there  I get an unhandled exception.

     

    Why is this happening.

     

    Please help

    • Moved by CoolDadTx Thursday, November 18, 2010 2:48 PM Winforms related (From:Visual C# IDE)
    Thursday, November 18, 2010 12:50 PM

Answers

  • WinForms?  In WinForms the UI thread effectively has a try-catch around it already.  When an unhandled exception occurs the default behavior is to display a generic WinForms error dialog and then terminate the application.  If you want to handle the exception yourself then you need to call Application.SetUnhandledExceptionMode to tell WinForms to let exceptions flow through. 

    Note that this only impacts the UI thread.  Other threads are not impacted.  Also note that any exception thrown in the Load portion of a form is silently ignored.  This is a limitation of the underlying Windows subsystem.

    When running under the debugger the Exception Assistant will kick in to allow you to see the exception rather than sending it through the standard exception dialog.  However if you run your debug build outside the debugger then the standard dialog will kick in just like in release mode.  By using SUEM your code will run in both cases.

    Michael Taylor - 11/18/2010
    http://msmvps.com/blogs/p3net

    • Proposed as answer by Wyck Thursday, November 18, 2010 3:11 PM
    • Marked as answer by krarun Thursday, November 18, 2010 4:03 PM
    Thursday, November 18, 2010 2:47 PM

All replies

  • WinForms?  In WinForms the UI thread effectively has a try-catch around it already.  When an unhandled exception occurs the default behavior is to display a generic WinForms error dialog and then terminate the application.  If you want to handle the exception yourself then you need to call Application.SetUnhandledExceptionMode to tell WinForms to let exceptions flow through. 

    Note that this only impacts the UI thread.  Other threads are not impacted.  Also note that any exception thrown in the Load portion of a form is silently ignored.  This is a limitation of the underlying Windows subsystem.

    When running under the debugger the Exception Assistant will kick in to allow you to see the exception rather than sending it through the standard exception dialog.  However if you run your debug build outside the debugger then the standard dialog will kick in just like in release mode.  By using SUEM your code will run in both cases.

    Michael Taylor - 11/18/2010
    http://msmvps.com/blogs/p3net

    • Proposed as answer by Wyck Thursday, November 18, 2010 3:11 PM
    • Marked as answer by krarun Thursday, November 18, 2010 4:03 PM
    Thursday, November 18, 2010 2:47 PM
  • Thanks dad  :),

     

    placing Application.SetUnhandledExceptionMode(UnhandledExceptionMode.ThrowException); at the start of the application solved it.

    Thursday, November 18, 2010 4:04 PM