0

My C# code consumes a COM object. Sometimes that COM object methods will return E_OUTOFMEMORY but it will get translated into System.OutOfMemoryException instead of System.Runtime.InteropServices.COMException with appropriate ErrorCode. This is a problem for me because it complicates my error handling - I'd prefer to have all error indications that arise from COM object methods being thrown as System.Runtime.InteropServices.COMException only.

Is it possible to always have System.Runtime.InteropServices.COMException and not System.OutOfMemoryException when a COM object method returns E_OUTOFMEMORY?

sharptooth
  • 167,383
  • 100
  • 513
  • 979
  • Feature, not a bug, you can't turn that off. OOM means that the *process* ran out of virtual memory, not the COM server. And is just as likely to strike on a managed allocation. Handling OOM is questionable, you can't magically extend the VM size. It should be done very close to the call site so you can recover program state. At which point you *know* that the COM server fell over. – Hans Passant Dec 03 '12 at 13:43
  • 1
    @Hans Passant: Great, except the COM server is outproc and so the invoking process couldn't care less about it's OOM state. – sharptooth Dec 03 '12 at 13:53

1 Answers1

0

It may be a bit of a cop-out, but why not wrap whatever code is throwing the System.OutOfMemoryException in a Try/catch and re-throw a System.Runtime.InteropServices.COMException?

Something along the lines of:

    try
        {
            // COM Code here
        }
        catch (System.OutOfMemoryException ex)
        {
            throw new System.Runtime.InteropServices.COMException("E_OUTOFMEMORY", ex);
        }
T S Taylor
  • 1,501
  • 1
  • 14
  • 16