Click here to Skip to main content
15,886,689 members
Articles / Programming Languages / C++

Active Error Codes

Rate me:
Please Sign up or sign in to vote.
4.79/5 (12 votes)
2 May 2009CPOL5 min read 28.3K   101   16  
A mechanism to ensure that error codes are checked and not just ignored
// ErrCodeTest.cpp : Example code for using ActiveErrors.h
// Written by Stuart Dootson (c) 2009
//
// This code is made available under the CodeProject Open License (http://www.codeproject.com/info/cpol10.aspx)

#include <Windows.h>
#include <string>
#include <stdexcept>
#include <iostream>

#if defined(_MSC_VER)
#include <crtdbg.h>
#else
inline void local_assert(bool b, std::string const& s)
{
   if (!b) throw std::runtime_error((s + ": assertion failed ").c_str());
}
#define _ASSERTE(expr)  local_assert((expr), #expr)
#endif

#include "ActiveErrors.h"


bool HRIsGood(HRESULT hr) { return SUCCEEDED(hr); }

typedef ErrCodeWithFn<HRESULT, &HRIsGood> eHRESULT;

eHRESULT Test(bool isGood)
{
   return isGood?S_OK:E_UNEXPECTED;
}

typedef ErrCodeWithGoodValue<int, 0> Err;

Err Test2(bool isGood)
{
   int e = isGood?(Err::value):(Err::value+1);
   return Err(e);
}

int main(int argc, char* argv[])
{
   int testNum = 1;
   try
   {
      eHRESULT ev = Test(false);
      eHRESULT errValue(Test(true));
      if (SUCCEEDED(errValue))
      {
         std::cerr << testNum << " OK" << std::endl;
         _ASSERTE(true);
      }
      else
      {
         std::cerr << testNum << " Bad" << std::endl;
         _ASSERTE(false);
      }
   }
   catch (UncheckedErrorBase&)
   {
      std::cerr << testNum << " OK" << std::endl;
      _ASSERTE(true);
   }
   ++testNum;
   try
   {
      Test(false);
      std::cerr << testNum << " Bad" << std::endl;
      _ASSERTE(false);
   }
   catch (UncheckedErrorBase&)
   {
      std::cerr << testNum << " OK" << std::endl;
      _ASSERTE(true);
   }
   ++testNum;
   try
   {
      if (Test(false).good())
      {
         std::cerr << testNum << " Bad" << std::endl;
         _ASSERTE(false);
      }
      else
      {
         std::cerr << testNum << " OK" << std::endl;
         _ASSERTE(true);
      }
   }
   catch (UncheckedErrorBase&)
   {
      std::cerr << testNum << " Bad" << std::endl;
      _ASSERTE(false);
   }
   ++testNum;
   try
   {
      Test2(true);
      std::cerr << testNum << " OK" << std::endl;
      _ASSERTE(true);
   }
   catch (UncheckedErrorBase&)
   {
      std::cerr << testNum << " Bad" << std::endl;
      _ASSERTE(false);
   }
   ++testNum;
   try
   {
      Test2(false);
      std::cerr << testNum << " Bad" << std::endl;
      _ASSERTE(false);
   }
   catch (UncheckedErrorBase&)
   {
      std::cerr << testNum << " OK" << std::endl;
      _ASSERTE(true);
   }
   ++testNum;
   try
   {
      if (Test2(false).good())
      {
         std::cerr << testNum << " Bad" << std::endl;
         _ASSERTE(false);
      }
      else
      {
         std::cerr << testNum << " OK" << std::endl;
         _ASSERTE(true);
      }
   }
   catch (UncheckedErrorBase&)
   {
      std::cerr << testNum << " Bad" << std::endl;
      _ASSERTE(false);
   }
   ++testNum;
   return 0;
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Technical Lead
United Kingdom United Kingdom
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions