-2

If there is an OutOfMemoryException in the try block of the code below, is it ever possible for the is checks themselves to throw another OutOfMemoryException? In other words do the is checks allocate memory?

    public void Main()
    {
        try
        {
            Execute();
        }            
        catch (Exception e)
        {
            if (e is OutOfMemoryException || e is ThreadAbortException)
            {
                throw;
            }
            else
            {
                Log(e);
                throw;
            }
        }
    }
Raghu Dodda
  • 1,505
  • 1
  • 21
  • 28
  • Kinda trivial to just try this, isn't it? Use [code like this](http://referencesource.microsoft.com/#mscorlib/system/runtime/versioning/multitargetinghelpers.cs#65). – Hans Passant Jul 19 '14 at 07:50
  • If I try it, it does not throw and exception, but I am not sure if that means that it is equivalent John Saunders solution below. – Raghu Dodda Jul 19 '14 at 17:32
  • I took the code in the question from the usages in the .NET framework such as the one you linked to. It appears to me that another OOM exception might be possible if memory needs to be allocated for `is` but this is used .NET library code, which is presumably robust. I don't know enough about CLR memory allocation, and so I am wondering if this is identical to the code in John Saunders answer. Hence the question. – Raghu Dodda Jul 19 '14 at 17:45
  • So you already convinced yourself it is not a problem by trying it. And you now know that Microsoft doesn't think it is a problem either since they do the exact same thing in the framework source code. Why do you keep insisting that you have a problem and that you have to do something about it? Stop trying to fix a problem you don't have. – Hans Passant Jul 19 '14 at 17:49
  • Because the accepted answer to the question (http://stackoverflow.com/questions/82483/how-to-catch-all-exceptions-crashes-in-a-net-app?lq=1) gives an example of action that when performed in a OOM catch block can throw another exception. I wanted to know if `is` qualifies for that scenario. Just wanted someone more familiar with the CLR to articulate how exactly the `is` check works in terms of memory allocation. The best i could find is some analysis on how much time isinst takes, but no memory usage details - http://msdn.microsoft.com/en-us/library/ms973852.aspx – Raghu Dodda Jul 19 '14 at 18:14
  • Downvoters, please comment. – Raghu Dodda Jul 21 '14 at 04:24

1 Answers1

2

Instead of your code, try the following:

public void Main()
{
    try
    {
        Execute();
    }
    catch (OutOfMemoryException)
    {
        throw;
    }
    catch (ThreadAbortException)
    {
        throw;
    }
    catch (Exception e)
    {
        Log(e);
        throw;
    }
}
John Saunders
  • 160,644
  • 26
  • 247
  • 397
  • Are you saying this would never throw a second OOM exception? The code in the catch block is not doing an `is` check, but CLR must be doing the same check under the covers in your example. Does the CLR do it in a way that is different than using the `is` operator? – Raghu Dodda Jul 19 '14 at 17:49
  • I have no reason to believe that either one does any significant memory allocation, but as has been said: if memory is that tight, then a lot of things are going to fail. .NET is not a real-time system, and you're not going to get any guarantees of which actions will or will not allocate memory. – John Saunders Jul 19 '14 at 17:53