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)

q0987
  • 34,938
  • 69
  • 242
  • 387
  • I suggest you read this: http://www.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." – Fred Larson Aug 11 '11 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. – André Caron Aug 11 '11 at 20:02
  • @Fred, I didn't find the topic related to define exception specification for a overloaded function in the provided link. – q0987 Aug 11 '11 at 20:04
  • @q0987: I didn't mean to imply it answered your question, I suggested you read it because following its advice would make your question moot. – Fred Larson Aug 11 '11 at 20:05
  • 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." – André Caron Aug 11 '11 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. – Bo Persson Aug 11 '11 at 20:07
  • @André Caron: Exactly, which is why I had added that quote to my comment. 8v) – Fred Larson Aug 11 '11 at 20:17
  • 1
    @Fred: wasn't part of your comment when I posted mine :-) – André Caron Aug 11 '11 at 20:20

1 Answers1

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.

Community
  • 1
  • 1
James McNellis
  • 348,265
  • 75
  • 913
  • 977
  • 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... – André Caron Aug 11 '11 at 20:05
  • What is the meaning of "strictness" in regard to exception specifications? – pmr Aug 11 '11 at 20:07
  • @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. – James McNellis Aug 11 '11 at 20:10
  • @pmr, 'strictness' means that you are ONLY allowed to specify same type or subtype of the original exception specification type. – q0987 Aug 11 '11 at 20:11
  • @James, I would prefer to using "specific" rather than "fewer" in your note. – q0987 Aug 11 '11 at 20:13
  • @JamesMcNellis, the C4290 warning is still being issued even in VC++ 18.00.31101. Are you going to fix in your frontend? – Anton K Jul 08 '15 at 19:05
  • 1
    @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. – James McNellis Jul 08 '15 at 19:50
  • @JamesMcNellis, thank you for clarification. Sorry, I missed this point in standard. – Anton K Jul 08 '15 at 20:44