CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Jul 2005
    Posts
    1,030

    A question regarding exception specification

    If a class inherits from another and overrides a virtual function, then the exception-specification for the overridden function must be at least as restrictive. What does it mean? Here is an example,
    Code:
    class B
    {
    public:
    	virtual void foo() throw (int)
    	{
    	}
    };
    
    class D: public B
    {
    public:
    	virtual void foo() throw (double)
    	{
    	}
    };
    It compiles fine. In the derived class, virtual function foo throws double and in the base class, foo throws int. So double is supposed to be at least as restrictive? Thanks.

  2. #2
    Join Date
    Apr 1999
    Posts
    27,449

    Re: A question regarding exception specification

    Quote Originally Posted by LarryChen View Post
    If a class inherits from another and overrides a virtual function, then the exception-specification
    Not to cut you off, but exception specifications have been deprecated in the new C++ standard.

    Here are two links explaining this:

    http://herbsutter.com/2010/03/13/tri...dards-meeting/
    http://www.gotw.ca/publications/mill22.htm

    So are you going to spend time on a "feature" that hardly anyone used, considered broken, and is now deprecated?

    Regards,

    Paul McKenzie

  3. #3
    Join Date
    Jul 2005
    Posts
    1,030

    Re: A question regarding exception specification

    Quote Originally Posted by Paul McKenzie View Post
    Not to cut you off, but exception specifications have been deprecated in the new C++ standard.

    Here are two links explaining this:

    http://herbsutter.com/2010/03/13/tri...dards-meeting/
    http://www.gotw.ca/publications/mill22.htm

    So are you going to spend time on a "feature" that hardly anyone used, considered broken, and is now deprecated?

    Regards,

    Paul McKenzie
    It is good to know. Thanks.

  4. #4
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: A question regarding exception specification

    not to mention that hardly any compiler even actually implemented it, even if they supported the syntax.

    but what the old rule basically said...
    if your base class implementation throws X and Y then a derived class implementation can be specified to throw
    * nothing
    * only X
    * only Y
    * X and Y
    it can't throw something else than those, because any code working off the base class only knows of a possible X and Y being thrown. So an overloaded implementation can't be allowed to throw something the base class doesn't specify.

    but as stated, this is deprecated, and even in pre C++11 compilers you'd be hard pressed to find a compiler actually fully implementing it.

  5. #5
    Join Date
    Jul 2005
    Posts
    1,030

    Re: A question regarding exception specification

    Quote Originally Posted by OReubens View Post
    not to mention that hardly any compiler even actually implemented it, even if they supported the syntax.

    but what the old rule basically said...
    if your base class implementation throws X and Y then a derived class implementation can be specified to throw
    * nothing
    * only X
    * only Y
    * X and Y
    it can't throw something else than those, because any code working off the base class only knows of a possible X and Y being thrown. So an overloaded implementation can't be allowed to throw something the base class doesn't specify.

    but as stated, this is deprecated, and even in pre C++11 compilers you'd be hard pressed to find a compiler actually fully implementing it.
    Your explanation is very clear. Thank you very much!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured