Click here to Skip to main content
Licence CPOL
First Posted 25 Dec 2001
Views 60,266
Bookmarked 21 times

Handling Windows API Errors with CWinException

By | 4 Jan 2002 | Article
Class for handling Windows API Errors

Introduction

When using the Windows API I think that it is better to get a well-formatted error description rather than its unmeaningful code. I also thought it may be useful that when you have some class based mostly on Win32 API functions to have it throw exceptions that can be handled in the caller block if needed. CWinException class solves those problems - it is derived from MFC's CException class and can be thrown by any API function that sets the 'last error' if it fails. Also if a function does not set the last error then you can set the last error to an appropriate value from the source block and catch the exception in the caller block.

Description

The CWinException class makes it possible to get the last error, set the value of the last error, or clear it (set it to ERROR_SUCCESS). It can also  get a formatted error description and report it.

Usage

void CService::Start_1(void) throw()
{
    if (!::StartService(m_schService,    // handle to service
            NULL,        // number of arguments 
            NULL))        // no arguments
    {
        throw new CWinException();
    }
}

// Or:

BOOL CService::Start_2(void)
{
    return ::StartService(m_schService, NULL, NULL);
}

// Eception handling

// Header
include "WinException.h"
include "Service.h"
//...
public:
    CService m_cService;
//...

// cpp file
//...

void CMainFrame::OnStartService_1(void)
{
    try
    {
        m_cService.Start_1();
    }
    catch(CWinException* e)
    {
        e->ReportError();
		e->Delete();
    }
}

// Or:

void CMainFrame::OnStartService_2(void)
{
    if(m_cService.Start_1() == FALSE)
    {
        CWinException e;
        e.ReportError();
    }
}

Base Class

CExcepton

Class Members

CWinException() - default constructor, gets last error on initialize.
CWinException(DWORD dwErrCode) - sets last error code to dwErrCode on initialize.

operator LPCTSTR() const - gives direct access to error description stored in CString type private variable.
operator DWORD() const - gives direct access to error code stored in DWORD type private variable.

BOOL SetLastError(DWORD dwErrorCode) - sets last error to dwErrorCode, returns TRUE if successful.
DWORD GetLastError(void) - regets last error code and returns it.

BOOL ToString(DWORD dwErrorCode, CString& szError) - writes dwErrCode error description to szError, returns TRUE if successful.
BOOL GetLastErrorString(CString& szLastError) - writes last error description to szError, returns TRUE if successful.
void ClearLastError(void) - sets last error to 0 (ERROR_SUCCESS), clears it.

virtual BOOL GetErrorMessage( LPTSTR lpszError, UINT nMaxError, PUINT pnHelpContext = NULL ) - provides text about an error that has occurred. Returns true if successful.

lpszError - a pointer to a buffer that will receive an error message.
nMaxError - the maximum number of characters the buffer can hold, including the NULL terminator. If NULL all characters will be written.
pnHelpContext - always NULL.

virtual int ReportError( UINT nType = MB_OK, UINT nMessageID = 0) - reports error text in a message box to the user. Returns an AfxMessageBox value.

nType - any combination of the message-box styles.
nMessageID - Specifies the resource ID (string table entry) of a message to add to the standard error message string (Run-time error : %d) if not NULL.

License

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

About the Author

Armen Hakobyan

Software Developer (Senior)
HyLink
Armenia Armenia

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 1 PinmemberArmen Hakobyan2:23 26 Dec '08  
Generalcall to unexpected() PinmemberAlexei Zakharov21:17 15 Mar '03  
GeneralMemory Leak ! PinmemberBernhard4:42 2 Jan '02  
GeneralRe: Memory Leak ! PinmemberBernhard4:55 2 Jan '02  
GeneralRe: Memory Leak ! Pinmember_6:16 6 Jan '02  
GeneralRe: Memory Leak ! PinmemberAlvaro Mendez11:31 7 Jan '02  
GeneralRe: Memory Leak ! PinmemberTim Smith12:08 7 Jan '02  
GeneralUh, I dont think OnStartService_2 would work... PinmemberPeter Yee6:49 31 Dec '01  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120517.1 | Last Updated 5 Jan 2002
Article Copyright 2001 by Armen Hakobyan
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid