Share via


Throw If Fails

I like to use this when writting sample code:

inline void Tif(HRESULT hResult) {

   if FAILED(hResult) {

      throw _com_error(hResult);

   }

}

inline void Tif(bool succeeded) {

   if (!succeeded) {

      DWORD lastError = GetLastError();

      throw win32_exception(lastError);

   }

}

inline void Tif(BOOL succeeded) {

   Tif(succeeded != FALSE); // And not == TRUE, thanks Mike!

}

 

The first exception type is defined in comdef.h and the second in win32_exception.h, in the std namespace.

Comments

  • Anonymous
    November 01, 2006
    Ack! Don't test a BOOL for equality with TRUE, always test != FALSE. With BOOL, any non-zero value counts as "true".