Search code examples
c#.netexception.net-2.0unhandled

.Net Equivalent of set_unexpected()


Is there a .net equivalent to the C++ unexpected()/set_unexpected() functionality?


Edit: Sorry--I omitted some details previously:

Language: C# 2.0

I have some legacy apps that seem to be throwing some unhandled exception somewhere. I just want to put something in place to stop the customer's pain until I can trace the actual source of the problem. In C++, the function pointed at by set_unexpected() gets called, as far as I know, when an otherwise unhandled exception bubbles to the main routine. Hence my question about a .net equivalent functionality.


Solution

  • There 3 possible scenarios for handling unhandled exceptions, based on the type of the application:

    1. For windows forms application, hook an event handler to Application.ThreadException
    2. For commandline applications, hook an event handler to AppDomain.UnhandledException
    3. For ASP.NET applications, in Global.asax, create:

      protected void Application_Error(Object sender, EventArgs e)

    DISCLAIMER: I'm not c++ developer, but from what I read, this should answer your question.