CWinException

Win32++

CWinException Class

Description

A class which handles exceptions.  Exceptions should only be used to handle errors which might arise from exceptional circumstances.  In Win32++, exceptions are typically used to handle the unexpected failure of Windows API functions, like, for example, CreateWindowEx.

It is not normal for a program to routinely throw exceptions.  A thrown exception often indicates a programming error, but they could also be thrown when a system is so starved of resources that the operating system cannot successfully allocate the resources needed to complete the task.

CWinException Members

CWinException
CWinException (LPCTSTR msg);
Constructor for CWinException
GetError
DWORD GetError() const throw ();
Retrieves the last error code value. GetErrorString
LPCTSTR GetErrorString() const;
Retrieves the error string from GetLastError. what
const char * what () const throw ();
Sends the exception message and error string to the debug window.

Remarks

Exceptions should be caught by constant reference to avoid the creation of a temporary copy of the CWinException object.  Since an exception could be thrown when the operating system is already starved of resources, consuming additional resources to create this copy to catch the exception would by very undesirable.

void SomeFunction()
{
  try
  {
    if(IsValid())
    {
      // Do the normal stuff
      .
      .
      .
    }
    else
      throw CWinException(_T("Not Valid"));

  }
  
  catch (const CWinException &e)
  {
    // Send the exception information to the debug window
    e.what();
  }
  
}

The what function reports the text message associated with the exception and sends this information to the IDE's debug window. It also reports the text associated with the GetLastError function.

Summary Information

Header file wincore.h
Win32/64 support Yes
WinCE support Yes