4
class ESClass
{
public:
    void PrintMe() throw();
    void PrintMe(int) throw(int);
};

I want to know whether or not we can define different exception specification for overloaded functions. In other words, can we give different exception specification to different version of the function PrintMe?

Note from VS2010:

warning C4290: C++ exception specification ignored except to indicate a function is not __declspec(nothrow)

8
  • I suggest you read this: gotw.ca/publications/mill22.htm. To cut to the chase, " Moral #1: Never write an exception specification. Moral #2: Except possibly an empty one, but if I were you I’d avoid even that." Commented Aug 11, 2011 at 20:02
  • 1
    The visual studio warning has been there forever. This is not the only situation in which it warns you against complying to the C++ standard. Commented Aug 11, 2011 at 20:02
  • 1
    I think Fred wanted you to read it up to the point where it says "So here’s what seems to be the best advice we as a community have learned as of today: Moral #1: Never write an exception specification. Moral #2: Except possibly an empty one, but if I were you I’d avoid even that." Commented Aug 11, 2011 at 20:07
  • 1
    @q0987 - It isn't about overloaded functions, it is about using exception specifications at all with VC++. The compiler only handles throw() and nothing else. Commented Aug 11, 2011 at 20:07
  • 1
    @Fred: wasn't part of your comment when I posted mine :-) Commented Aug 11, 2011 at 20:20

1 Answer 1

9

Yes: they are different functions, they can have different exception specifications.

If a virtual member function has an exception specification, any override (not overload) must have an exception specification that is at least as strict as the member function being overridden.

Of course, you should "never write an exception specification" except in those few situations where you must.

Visual C++ does not fully support exception specifications, so it allows some code that is not actually valid per the C++ language specification. The warning you mention just means that you are using code that uses a C++ language feature not supported by Visual C++:

A function is declared using exception specification, which Visual C++ accepts but does not implement. Code with exception specifications that are ignored during compilation may need to be recompiled and linked to be reused in future versions supporting exception specifications.

Sign up to request clarification or add additional context in comments.

8 Comments

Although it is legal, it's a bad habit to throw different sets of exceptions from different overloads. It's easy to forget to update the catch clauses when only changing the arguments passed to the function...
What is the meaning of "strictness" in regard to exception specifications?
@pmr: "If a virtual function has an exception-specification, all declarations, including the definition, of any function that overrides that virtual function in any derived class shall only allow exceptions that are allowed by the exception-specification of the base class virtual function (C++03 §15.4/3)." So, an override can have a stricter exception specification, meaning it allows fewer exceptions to be thrown.
@pmr, 'strictness' means that you are ONLY allowed to specify same type or subtype of the original exception specification type.
@AntonK: If you're asking whether we plan to implement full support for dynamic exception specifications, I expect the answer is "probably not." Dynamic exception specifications are deprecated in C++11.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.