When is a try catch not a try catch?

Posted by Dearmash on Stack Overflow See other posts from Stack Overflow or by Dearmash
Published on 2010-04-17T23:16:45Z Indexed on 2010/04/17 23:23 UTC
Read the original article Hit count: 217

Filed under:
|

I have a fun issue where during application shutdown, try / catch blocks are being seemingly ignored in the stack.

I don't have a working test project (yet due to deadline, otherwise I'd totally try to repro this), but consider the following code snippet.

public static string RunAndPossiblyThrow(int index, bool doThrow)
{
    try
    {
        return Run(index);
    }
    catch(ApplicationException e)
    {
         if(doThrow)
             throw;
    }
    return "";
}

public static string Run(int index)
{
    if(_store.Contains(index))
        return _store[index];
    throw new ApplicationException("index not found");
}

public static string RunAndIgnoreThrow(int index)
{
    try
    {
        return Run(index);
    }
    catch(ApplicationException e)
    {
    }
    return "";
}

During runtime this pattern works famously. We get legacy support for code that relies on exceptions for program control (bad) and we get to move forward and slowly remove exceptions used for program control.

However, when shutting down our UI, we see an exception thrown from "Run" even though "doThrow" is false for ALL current uses of "RunAndPossiblyThrow". I've even gone so far as to verify this by modifying code to look like "RunAndIgnoreThrow" and I'll still get a crash post UI shutdown.

Mr. Eric Lippert, I read your blog daily, I'd sure love to hear it's some known bug and I'm not going crazy.

EDIT This is multi-threaded, and I've verified all objects are not modified while being accessed

© Stack Overflow or respective owner

Related posts about c#

Related posts about exception