SOL9 2.0 Class: Exception

 SOL9 C++ Class Library  SOL9 Samples  SOL9 Tutorial  SOL9 FAQ  SOL9 ClassTree 

Source code

/*
 * Exception.h 
 * Copyright (c) 2011 Antillia.com TOSHIYUKI ARAI. ALL RIGHTS RESERVED. 
 */


// SOL++2000

//2009/05/26 Added the following constructor
//    Exception(DWORD error, const char* format,...)

#pragma once

#include <sol\Object.h>

namespace SOL {

class Exception :public Object {

protected:
    char*    message;
    int        errorCode;
    HRESULT hresult;

    static const int MAX_MESSAGE = 1024*2;

public:
    Exception(int error=0)
        :message(NULL),
          errorCode(error),
          hresult((HRESULT)0)
    {
        this ->message = new char[MAX_MESSAGE];
        memset(this->message, 0, MAX_MESSAGE);
    }

public:

    Exception(const char* msg, int error=0)
        :message(NULL),
          errorCode(error),
          hresult((HRESULT)0)
    {
              
        if (msg) {
            size_t slen = strlen(msg) +1;
            this->message = new char[slen];
            strcpy_s(this->message, slen, msg);
        }    
    }

public:

    Exception(const wchar_t* msg, int error=0)
        :message(NULL),
          errorCode(error),
          hresult((HRESULT)0)
    {
              
        if (msg) {

            this->message = toMBString(msg);
        }    
    }

public:
    /**
     *
     */
    Exception(int error, const char* format,...)
        :message(NULL),
        errorCode(error),
        hresult((HRESULT)0)
    {
        this ->message = new char[MAX_MESSAGE];
        memset(this->message, 0, MAX_MESSAGE);

        va_list pos;
        va_start(pos, format);
        vsprintf_s(this->message, MAX_MESSAGE, format, pos);
        va_end(pos);
    }

public:
    /**
     *
     */
    //2009/05/26
    Exception(DWORD error, const char* format,...)
        :message(NULL),
        errorCode(error),
        hresult((HRESULT)0)
    {
        this ->message = new char[MAX_MESSAGE];
        memset(this->message, 0, MAX_MESSAGE);

        va_list pos;
        va_start(pos, format);
        vsprintf_s(this->message, MAX_MESSAGE, format, pos);
        va_end(pos);
    }

public:
    /**
     *
     */
    //2009/10/21
    Exception(int error, const wchar_t* format,...)
        :message(NULL),
        errorCode(error),
        hresult((HRESULT)0)
    {
        wchar_t wmessage[MAX_MESSAGE];
        memset(wmessage, (wchar_t)0, MAX_MESSAGE);

        va_list pos;
        va_start(pos, format);
        _vsnprintf(wmessage, MAX_MESSAGE, format, pos);
        va_end(pos);
        
        this->message = toMBString(wmessage);
    }

public:
    /**
     *
     */
    //2009/10/21
    Exception(DWORD error, const wchar_t* format,...)
        :message(NULL),
        errorCode(error),
        hresult((HRESULT)0)
    {
        wchar_t wmessage[MAX_MESSAGE];
        memset(wmessage, (wchar_t)0, MAX_MESSAGE);

        va_list pos;
        va_start(pos, format);
        _vsnprintf(wmessage, MAX_MESSAGE, format, pos);
        va_end(pos);
        
        this->message = toMBString(wmessage);
    }

public:
    /**
     *
     */
    //2012/01/21
    Exception(HRESULT herror, const wchar_t* format,...)
        :message(NULL),
        errorCode(0),
        hresult(herror)
    {
        wchar_t wmessage[MAX_MESSAGE];
        memset(wmessage, (wchar_t)0, MAX_MESSAGE);

        va_list pos;
        va_start(pos, format);
        _vsnprintf(wmessage, MAX_MESSAGE, format, pos);
        va_end(pos);
        
        this->message = toMBString(wmessage);
    }

private:
    char* toMBString(const wchar_t* wcstring) 
    {

        char* mbstring = NULL;

        if (wcstring == NULL) {
            return mbstring;
        }
        
        int cb = WideCharToMultiByte(CP_ACP, 0, wcstring, -1, NULL, 0, NULL, NULL);
        
        if (cb > 0) {
            mbstring = new char[cb];
            mbstring[0] = Zero;
            WideCharToMultiByte(CP_ACP, 0, wcstring, -1, mbstring, cb, NULL, NULL);
        }
        return mbstring;
    }

public:
    /**
     *
     */
    // 2009/05/15
    Exception(HRESULT hr, const char* format,...)
        :message(NULL),
        errorCode(0),
        hresult(hr)
    {
        this ->message = new char[MAX_MESSAGE];
        memset(this->message, 0, MAX_MESSAGE);

        va_list pos;
        va_start(pos, format);
        vsprintf_s(this->message, MAX_MESSAGE, format, pos);
        va_end(pos);
    }

public:
    ~Exception() { 
        if (message) {
            delete [] message;
            message = NULL;
        }
    }

protected:
    //2008/07/11
    void formatMessage(const char* format, va_list pos)
    {
        vsprintf_s(this->message, MAX_MESSAGE, format, pos);
    }


public:
    const char* getString() {
            return (const char*)message; 
    }

public:
    const char* getErrorMessage() {
            return (const char*)message; 
    }

public:
    int        getErrorCode() { return errorCode; }

public:
    HRESULT        getHRESULT() { return hresult; }

public:
    //2011/01/30 Added virtual
    virtual void    printf() {
        ::printf("Exception{\nMessage:%s\nErrorNo:0x%08X\nHRESULT:0x%08X\n}\n", 
                (const char*)message, 
                errorCode,
                hresult);
    }

public:
    //2011/01/30 Added virtual
    virtual void    dump() {
        ::printf("Exception{\nMessage:%s\nErrorNo:0x%08X\nHRESULT:0x%08X\n}\n", 
                (const char*)message, 
                errorCode,
                hresult);
    }

};

}


Last modified: 1 Feb 2012

Copyright (c) 2009-2012 Antillia.com ALL RIGHTS RESERVED.