override 하는경우의 throw (error handling)

manim의 이미지

class B {
public:
virtual void Saturday() throw(Exam);
};
class D : public B {
public:
virtual void Saturday() throw();
};

이 경우의 class D에서의 throw()는 가능하다고 알고 있습니다.

class B {
public:
virtual void Saturday() throw(Exam);
};
class D : public B {
public:
virtual void Saturday() throw(Exam, Party);
};

그런데 이 경우에서는 D에서 throw가 문제가 되는데요
왜, subclass에서의 throw는 argument를 base class보다 같거나 적게 가져야 하는건가요?

ssehoony의 이미지

C++ 문법상 throw 를 명시할 수 있지만 실제 구현된 컴파일러가 없는 것으로 알고 있었는데...
현재 구현된 컴파일러가 있나요?

그리고 저렇게 명시하는 문법의 장점이 무엇인가요?

에구.. 답이 아니라 질문이라 죄송합니다.

manim의 이미지

standard exception에 대한 설명에 이런게 나오네요..

- Each class has a what() method that gives a human-readable description of the error.
- All constructors(except exception()) take the human-readable string as an argument.
- Hierarchy allows polymorphic catching of exceptions.
- You can subclass the built-in exceptions to provide more specific exception types.

현재 구현된 컴파일러는.. 잘 모르겠네요 ^^:;

thyoo의 이미지

Compile Error가 나는 게 맞습니다.

> 왜, subclass에서의 throw는 argument를 base class보다 같거나 적게 가져야 하는건가요?

추상화겠지요.
Base Class만 알고 있는 녀석들이 있을 텐데,
Derived Class에서 다른 걸 던지면 위험하지요.

예외를 못 잡으면 프로세스가 죽을 테고
만약 우주선이라든가 열차라든가 사람 목숨이 걸린 시스템에서
못 잡은 예외는 끔찍한 사고로도 이어집니다.

실제로 유럽 아리안 몇혼가가 익셉션 때문에 공중폭발했읍니다.

GCC/VC 둘 다 지원 안합니다.

[C++98 p296]

15.4 Exception specifications

3 항
If a virtual function has an exception-specification, all declarations, including the definition, of any function that overrides that virtual function in an derived class shall only allow exceptions that are allowed by the exception-specification of the base class virtual function.

struct B {
    virtual void f() throw (int, double);
    virtual void g();
};
 
struct D: B {
    void f();                // ill-formed
    void g() throw (int);    // OK
};

The declaration of D::f ill-formed because it allows all exception, whereas B::f allows only int and double.

Similarly, any function or pointer to function assigned to, or initializing, a pointer to function shall only allow exceptions that are allowed by the pointer or function being assigned to or initialized.

class A { /* ... */ };
void (*pf1) ();            // no exception specification
void (*pf2) () throw(A);
 
void f()
{
    pf1 = pf2;             // OK: pf1 is less restrictive
    pf2 = pf1;             // error: pf2 is more restrictive
}

___________________________________
Less is More (Robert Browning)

___________________________________
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
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.