exception 질문입니다.

nayana의 이미지

#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 &divideByZeroException )
		// - 예외처리가 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 라이브러리(?)가 없는건지요...없다면...만들어서써야하겠지요^^
만들어 써야한다면..적절한 예제 부탁드립니다.
고수님들 답변 부탁드립니다.
thyoo의 이미지

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)

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.