2014-05-09 87 views
0

我嘗試使我自己的異常,我可以拋出一個或兩個參數。所以我做的是重載我的異常的構造函數。然而第二個構造函數似乎沒有被調用。這裏是我的代碼:例外的C++重載構造函數

code-listing-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 
} 

「調試消息」根本沒有出現。當我評論「第一個構造函數」時,我得到一個錯誤和很多警告。

error-listing-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, 
}; 

任何人都可以向我解釋我做錯了什麼,爲什麼?提前致謝。

+0

是'MYERROR_CODE_2'宏?在這種情況下,你可以張貼定義嗎? – Rakib

+1

你得到錯誤(28)的'throw'行與你在代碼中顯示的不一樣。謹慎編輯問題,添加更多相關信息? – Massa

+0

@Massa我的不如Jan Kukacka回覆我正在修改我的代碼的錯誤部分 –

回答

2

正如馬薩指出的那樣,你得到的編譯錯誤是沒有提出,因爲你的榜樣行:

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

反倒是因爲你到別的地方調用第一個構造函數中的代碼,例如:

throw myException(MYERROR_CODE_2); 
1

正如其他人已經回答的那樣,這個錯誤是其他的地方。我可以編譯並運行您的程序,而不會遇到您提到的問題。但是,您的代碼中存在其他問題。您正在創建一個string的方法,並返回由該字符串保存的字符數組。這是未定義的行爲,因爲從方法返回後,string不存在,並且指針指向無效內存。

1

你有一個枚舉類型ExceptionCodeMYERROR_CODE_2類型爲ExceptionCode,而不是unsigned short。你有一個名爲ExceptionCode的變量。這是一個壞主意。不要這樣做。另外,我建議你擺脫錯誤代碼併爲每個錯誤代碼派生一個子類,就像從std :: exception派生出幾種類型一樣。

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; 
    } 


}