是得到COMException而不是OutOfMemoryExc

是得到COMException而不是OutOfMemoryExc

本文介绍了如何总是得到COMException而不是OutOfMemoryException?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的C#代码使用COM对象。有时,COM对象方法将返回 E_OUTOFMEMORY ,但它会被转换为 System.OutOfMemoryException 而不是 System.Runtime.InteropServices.COMException 与适当的 ErrorCode 。这对我来说是一个问题,因为它使我的错误处理变得复杂 - 我希望将COM对象方法中引发的所有错误指示都抛出为 System.Runtime.InteropServices.COMException



可以始终有 System.Runtime.InteropServices.COMException 而不是 System.OutOfMemoryException 当COM对象方法返回 E_OUTOFMEMORY

解决方案

它可能是一个cop-out,但为什么不包装任何代码在Try / catch中抛出 System.OutOfMemoryException 重新抛出一个 System.Runtime.InteropServices.COMException



以下行:

  try 
{
// COM代码这里
}
catch(System.OutOfMemoryException ex )
{
throw new System.Runtime.InteropServices.COMException(E_OUTOFMEMORY,前);
}


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?

解决方案

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);
        }

这篇关于如何总是得到COMException而不是OutOfMemoryException?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 08:45