Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All
Expand Minimize
Important This document may not represent best practices for current development, links to downloads and other resources may no longer be valid. Current recommended version can be found here.


Call this function to signal an error based on a HRESULT status code.

inline void AtlThrow(



Standard HRESULT value.

This function is used by ATL and MFC code in the event of an error condition. It can also be called from your own code. The default implementation of this function depends on the definition of the symbol _ATL_NO_EXCEPTIONS and on the type of project, MFC or ATL.

In all cases, this function traces the HRESULT to the debugger.

If _ATL_NO_EXCEPTIONS is not defined in an MFC project, this function throws a CMemoryException or a COleException based on the value of the HRESULT.

If _ATL_NO_EXCEPTIONS is not defined in an ATL project, the function throws a CAtlException.

If _ATL_NO_EXCEPTIONS is defined, the function causes an assertion failure instead of throwing an exception.

For ATL projects, it is possible to provide your own implementation of this function to be used by ATL in the event of a failure. To do this, define your own function with the same signature as AtlThrow and #define AtlThrow to be the name of your function. This must be done before including atlexcept.h (which means that it must be done prior to including any ATL headers since atlbase.h includes atlexcept.h).

// Example for AtlThrow
// Constructors and operators cannot return error codes, and
// so they are the place where exceptions are generally used.
class CMyClass
   CComPtr<ITheirInterface> m_spTheirInterface;
      HRESULT hr = m_spTheirInterface.CoCreateInstance(__uuidof(CTheirCLSID));
      if (FAILED(hr))
   //   methods ..

Community Additions

© 2015 Microsoft