10

Is there a way to handle the error "WebDev.WebServer.Exe has stopped working" in ASP.NET and keep the page running or even the just the WebServer running? Or is this an impossible task and is essentially like asking how to save someone's life after they've died?

I have the error-causing code inside a try/catch block, but that doesn't make a difference. I've also tried registering a new UnhandledExceptionEventHandler, but that didn't work either. My code is below in case I'm doing something wrong.

Also to be clear, I'm not asking for help on how to prevent the error; I want to know if and when the error happens if there's anything I can do to handle it.

UPDATE 1: TestOcx is a VB6 OCX that passes a reference of a string to a DLL written in Clarion.

UPDATE 2: As per @JDennis's answer, I should clarify that the catch(Exception ex) block is not being entered either. If I removed the call to the OCX from the try\catch block it still won't reach the UnhandledException method. There are essentially two areas that don't ever get executed.

UPDATE 3: From @AndrewLewis, I tried to also add a regular catch block to catch any non-CLS compliant exceptions, and this did not work either. However, I later found that since .NET 2.0 on, all non-CLS exceptions are wrapped inside RuntimeWrappedException so a catch (Exception) will catch non-CLS compliant exceptions too. Check out this other question here for more info.

public bool TestMethod()
{
    AppDomain.CurrentDomain.UnhandledException +=
        new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);

    string input = "test";
    string result = "";
    try
    {
        TestOcx myCom = new TestOcx();
        result = myCom.PassString(ref input); // <== MAJOR ERROR!
        // do stuff with result...
        return true;
    }
    catch (Exception ex)
    {
        log.Add("Exception: " + ex.Message); // THIS NEVER GETS CALLED
        return false;
    }
}

private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
    // THIS NEVER GETS CALLED
    try
    {
        Exception ex = (Exception)e.ExceptionObject;
        log.Add("Exception: " + ex.Message);
    }
    catch (Exception exc)
    {
        log.Add("Fatal Non-UI Error: " + exc.Message);
    }
}
Community
  • 1
  • 1
DanM7
  • 2,203
  • 3
  • 28
  • 46
  • You pulled the plug from your TV, and try to watch a show. What is TestDll doing, where does it come from, what kind of object is it? Does it work fine by itself? – Alexander May 24 '13 at 13:15
  • I just added an update to explain what the object does. Thanks for the comment. – DanM7 May 24 '13 at 14:18
  • Wild guess, but can you add the attribute `[HandleProcessCorruptedStateExceptions]` to your `TestMethod` to see if you can catch the `Exception`? See also [How to handle AccessViolationException](http://stackoverflow.com/a/4759831/1822514) – chue x Aug 21 '14 at 01:30
  • Stupid question time: Have you tried running this in IIS instead of the built in webserver? You may find that the real exception is presented, or even no exception at all. – FlemGrem Aug 21 '14 at 09:23
  • Are you sure // THIS NEVER GETS CALLED does never get called or if it just throws again? Try to just write a string without using the ex. – Pedro.The.Kid Aug 21 '14 at 09:46
  • Awesome comments. I'll try all three and update soon. – DanM7 Aug 21 '14 at 12:41
  • In my test app, I was able to catch a [CSE](http://stackoverflow.com/a/4759831/1822514) with only the attribute I mention above. According to [this answer](http://stackoverflow.com/a/11056153/1822514) you may also need `[SecurityCritical]`. – chue x Aug 21 '14 at 13:25
  • @chuex - I added both `[System.Security.SecurityCritical]` and `[System.Runtime.ExceptionServices.HandleProcessCorruptedStateExceptions]' and I still get the error. @Pedro.The.Kid - that didn't work either. @AaronH - trying now. – DanM7 Aug 21 '14 at 20:24
  • @AaronH - The error basically crashes the app pool (stops it in IIS) and the browser returns a 503 error. It doesn't hit the Global.asax so I don't even get a stack trace. Interesting result, but doesn't get me closer to a solution. Thanks though! – DanM7 Aug 21 '14 at 20:41

2 Answers2

1

You should try catching non-CLS compliant exceptions to make sure nothing is being thrown (keep in mind you don't want to do this in production, always be specific!):

try
{
    TestOcx myCom = new TestOcx();
    result = myCom.PassString(ref input); // <== MAJOR ERROR!
    // do stuff with result...
    return true;
}

catch (Exception ex)
{
    log.Add("Exception: " + ex.Message); // THIS NEVER GETS CALLED
    return false;
}
catch
{
   //do something here
}
Andrew Lewis
  • 5,176
  • 1
  • 26
  • 31
  • That's a great point Andrew. I'll try that tomorrow and let you know how it works out. – DanM7 May 28 '13 at 22:54
  • Unfortunately this did not work. I still get the same error "An unhandled win32 exception occurred in WebDev.WebServer40.EXE". Thanks for the suggestion though. – DanM7 May 29 '13 at 17:48
  • Doing some research, since .NET 2.0 all non-CLS compliant exceptions are wrapped inside `RuntimeWrappedException`, which derives from `Exception`. A `catch (Exception)` will catch both CLS compliant and non-CLS compliant exceptions. – DanM7 May 30 '13 at 14:23
  • 2
    Adding a link to @DanM comment: http://msdn.microsoft.com/en-us/library/ms404228.aspx – Robert Aug 15 '14 at 06:55
0

Your code reads //THIS NEVER GETS CALLED.

If you catch the exception it is no longer un-handled. this is why it doesn't fire an unhandledexception event.

JDennis
  • 672
  • 6
  • 15
  • I see what you are saying @JDennis, so I will clarify in my question too: the catch block is never entered either. It does not reach the call to the log message. – DanM7 May 28 '13 at 18:40
  • try this post: http://stackoverflow.com/questions/12101097/unhandled-exception-not-caught-by-either-global-asax-error-handler-or-custom-iht – JDennis May 29 '13 at 18:25