exception 질문입니다.
글쓴이: nayana / 작성시간: 월, 2004/08/16 - 8:32오후
#include <iostream> using std::cout; using std::cin; using std::endl; #include <exception> using std::exception; //------------------------------------------------------------ class DivideByZeroException : public exception // - DivideByZeroException객체는 0으로 나누기 예외가 검출될때 //------------------------------------------------------------ { public: DivideByZeroException::DivideByZeroException() :exception( "attempted to divide by zero" ) { } }; //------------------------------------------------------------ double quotient( int numerator, int denominator ) // - 나누기를 실행하고 만약 0으로 나누기 예외가 발생하면 // - DivideByZeroException객체 실행 //------------------------------------------------------------ { if ( denominator == 0 ) { // - 0으로 나누기를 시도하면 DivideByZeroException객체 실행 throw DivideByZeroException(); } // - 나누기 반환 return static_cast< double >( numerator ) / denominator; } int main ( void ) { int number1; int number2; double result; cout << "Enter two integers ( end-of-file to end ): "; while ( cin >> number1 >> number2 ) { try { result = quotient( number1, number2 ); cout << "The quotient is: " << result << endl; } catch ( DivideByZeroException ÷ByZeroException ) // - 예외처리가 0으로 나누기 예외를 처리 { cout << "Exception occured: " << divideByZeroException.what() << endl; } cout << "\nEnter two integers( end-of-file to end): "; } cout << endl; return 0; }
redhat 9.0에서 다음소스를 컴팡리 시켰더니..
다음과 같은 오류가 나옵니다.
fig.cpp: In constructor `DivideByZeroException::DivideByZeroException()': fig.cpp:15: no matching function for call to `std::exception::exception(const char[28])' /usr/include/c++/3.2.2/exception:51: candidates are: std::exception::exception(const std::exception&) /usr/include/c++/3.2.2/exception:53: std::exception::exception()
에러코드를 보니 exception이 없다고 나오는데....
vc++ 실행하니 아무 이상이 없습니다.
리눅스에서...exception 라이브러리(?)가 없는건지요...없다면...만들어서써야하겠지요^^
만들어 써야한다면..적절한 예제 부탁드립니다.
고수님들 답변 부탁드립니다.
Forums:
CD2입니다.
18.6.1 Class exception [lib.exception]
namespace std {
class exception {
public:
exception() throw();
exception(const exception&) throw();
exception& operator=(const exception&) throw();
virtual ~exception() throw();
virtual const char* what() const throw();
};
}
http://www.itga.com.au/~gnb/wp/cd2/index.html
표준 exception ctor에는 c string을 받는 게 없군요.
char* what()을 적당히 override하세요.
___________________________________
Less is More (Robert Browning)
댓글 달기