C ++重载异常的构造函数

时间:2014-05-09 14:38:35

标签: c++ exception-handling

我尝试制作自己的异常,我可以使用一个或两个参数抛出。所以我做的是重载我的异常的构造函数。但是第二个构造函数似乎没有被调用。这是我的代码:

代码列表-1

class myException: public std::exception {
public:
    /*
     * first constructor
     */
    myException(unsigned short ExceptionCode):exception(){
        code = ExceptionCode;
        errorMessage = std::string("");
    }
    /*
     * second constructor
     */
    myException(unsigned short ExceptionCode, std::string errorMessage):exception(){
        std::cout << "debugging message";//present here for debugging purpose only
        code = ExceptionCode;
        this->errorMessage = errorMessage;
    }

    const char * what() const throw ()
    {   
        std::string tmp;
        switch(code){
            case MYERROR_CODE_1:
                tmp = std::string("MYERROR_CODE_1 : ");
                if(errorMessage.empty())
                    tmp += std::string("this is a default error message for code 1");
                tmp += errorMessage;
                break;
            case MYERROR_CODE_2:
                tmp = std::string("MYERROR_CODE_2 : ");
                if(errorMessage.empty())
                    tmp += std::string("this is a default error message for code 2");
                tmp += errorMessage;
        }


      return tmp.c_str();
    }
    ~myException() throw(){}

private:
    std::string errorMessage;
    unsigned short   code;
};

例如当我这样称呼它时:

代码列表-2。

void myFunction(myClass myObject){
    //some code
    if(conditionsMeet)
         throw myException(MYERROR_CODE_2,"this is a more specific custom message for code 2");
    //some other code
}

根本没有出现“调试消息”。当我评论“第一个构造函数”时,我收到错误和很多警告。

错误-列表-1

main.cpp:28:41: error: no matching function for call to 'myException::myException(ExceptionCode)'
     throw myException(MYERROR_CODE_2);
                                         ^
main.cpp:28:41: note: candidates are:
In file included from Node.h:12:0,
                 from main.cpp:10:
myException.h:50:5: note: myException::myException(short unsigned int, std::string)
     myException(unsigned short ExceptionCode, std::string errorMessage):exception(){
     ^
myException.h:50:5: note:   candidate expects 2 arguments, 1 provided
myException.h:44:7: note: myException::myException(const myException&)
 class myException: public std::exception {
       ^
myException.h:44:7: note:   no known conversion for argument 1 from 'ExceptionCode' to 'const myException&'

编辑:错误代码定义如下:

代码列表-2。

enum ExceptionCode{
    MYERROR_CODE_1                 = 1,
    MYERROR_CODE_2                 = 2,
};

任何人都可以向我解释我做错了什么,为什么?提前谢谢。

4 个答案:

答案 0 :(得分:2)

正如Massa所指出的那样,由于您的示例行而未提出编译错误:

throw myException(MYERROR_CODE_2,"this is a more specific custom message for code 2");

而是因为你在代码中的其他地方调用了第一个构造函数,例如:

throw myException(MYERROR_CODE_2);

答案 1 :(得分:1)

正如其他人已经回答的那样,错误是其他地方的错误。我可以在没有你提到的问题的情况下编译和运行你的程序。但是,您的代码中还有其他问题。您正在string方法中创建临时what,并返回该字符串所持有的字符数组。这是未定义的行为,因为从方法返回后,string不存在,并且指针指向无效的内存。

答案 2 :(得分:1)

您有一个枚举类型ExceptionCodeMYERROR_CODE_2的类型为ExceptionCode,而非unsigned short。您有一些名为ExceptionCode的变量。这是一个坏主意。不要那样做。

顺便说一句,我建议你删除错误代码并为每个错误代码派生一个子类,就像从std :: exception派生的几种类型一样。

答案 3 :(得分:1)

举例并提供错误代码的定义,所有编译。

然而,what()函数中存在一个严重的错误,它会导致一天崩溃。

您正在临时调用c_str(),然后将指针返回到已释放的内存。这非常非常糟糕。

您需要将该字符串存储在异常中,并在构造函数中构建它以保证安全。

示例提供:

#include <iostream>
#include <exception>

enum Errors {
    MYERROR_CODE_1,
    MYERROR_CODE_2
};


class myException: public std::exception {
public:
    /*
     * only one constructor necessary
     */
    myException(unsigned short ExceptionCode, std::string errorMessage = std::string())
    : exception()
    , message(buildErrorMessage(ExceptionCode, errorMessage))
    , code(ExceptionCode)
    {
        std::cout << "debugging message\n";//present here for debugging purpose only
    }

    static std::string buildErrorMessage(unsigned short code, const std::string& errorMessage) {
        std::string tmp;
        switch(code){
            case MYERROR_CODE_1:
                tmp = std::string("MYERROR_CODE_1 : ");
                if(errorMessage.empty())
                    tmp += std::string("this is a default error message for code 1");
                tmp += errorMessage;
                break;
            case MYERROR_CODE_2:
                tmp = std::string("MYERROR_CODE_2 : ");
                if(errorMessage.empty())
                    tmp += std::string("this is a default error message for code 2");
                tmp += errorMessage;
        }
        return tmp;
    }

    const char * what() const throw ()
    {   
        return message.c_str();
    }

private:
    std::string message;
    unsigned short   code;
};

using namespace std;

int main()
{
    int err = MYERROR_CODE_1;

    try {
        throw myException(err);
    }
    catch(exception& e) {
        cout << e.what() << endl;
    }

    try 
    {
        throw myException(err, "more info");
    }
    catch(exception& e) {
        cout << e.what() << endl;
    }


}