하나 또는 두 개의 매개 변수로 throw 할 수있는 자체 예외를 만들려고합니다. 그래서 내가 한 것은 예외의 생성자를 오버로드하는 것입니다. 그러나 두 번째 생성자는 호출되지 않는 것 같습니다. 여기 내 코드 :예외의 C++ 오버로드 생성자
코드 목록 -1.
코드 목록-2 :
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;
};
나는이 방법을 호출 할 때, 예를 들어.
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,
};
누구든지 내가 잘못한 이유를 설명 할 수 있습니까? 미리 감사드립니다. 예를 들어,
throw myException(MYERROR_CODE_2,"this is a more specific custom message for code 2");
을하지만 오히려 당신은 당신의 코드에서 다른 곳에서 최초의 생성자를 호출하기 때문에 : 마사는 지적
는'MYERROR_CODE_2' 매크로입니까? 그 경우에 difinition을 게시 할 수 있습니까? – Rakib
오류가 발생하는'throw' 행 (28)이 코드에서 보여준 것과 다릅니다. 질문을 수정하고 관련 정보를 추가 할 때주의하십시오. – Massa
@Massa Jan Kukacka가 응답 한 것처럼 나쁘다. 내 코드의 잘못된 부분을 수정하고 있었다. –