2

We are in our 4 and final C++ class, I have been looking at Herb Sutter's post on exception specification and a few posts on stackoverflow indicating that exception specifications will be deprecated in the next release of C++.

I can find examples of how to implement exception specifications, but I am having some issues in understanding just want exception specifications are and how they differ from exception handing.

Any assistance in providing some insight will be most helpful.

10
  • 1
    They were deprecated in the last release of C++ (nearly two years ago). Commented Jul 16, 2013 at 17:08
  • There are two kinds of exception specification, dynamic exception specifications and noexcept specifications, only dynamic exceptions specifications are deprecated Commented Jul 16, 2013 at 17:15
  • One of the reasons it was deprecated, because people could not get it right, causing more harm than good. Commented Jul 16, 2013 at 17:19
  • @JonathanWakely "dynamic exception specifications" means old style exception specifications; there is nothing especially "dynamic" about them. Commented Nov 10, 2019 at 5:39
  • @SChepurin Nobody ever demonstrated any harm; except the inherent "harm" of having almost useless features that still need to be implemented. Commented Nov 10, 2019 at 5:40

3 Answers 3

4

Exception specifications ask the compiler to handle all unlisted exception types by calling std::unexpected() or the substitute set with std::set_unexpected().

They are universally considered a bad thing.

Common misconceptions:

  • "The compiler will check whether you do anything that could throw other exceptions." False. C++ exceptions are not checked at compile-time. The compiler does not check whether you throw other types, and it does not check whether you forget to handle unlisted exception types.
  • "You can't throw exceptions of other types." False, within the function any exception can be thrown. At runtime, if the function would terminate abnormally via any unlisted exception type, the unexpected handler will be called, which has the opportunity to replace the exception with a listed type.

See also std::bad_exception.

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

7 Comments

Ben, what does this really mean and why is @Crazy Eddie's post incorrect?
@Joe: Because it doesn't actually prevent you from throwing exceptions of other types... it just completely changes the handling.
Never said you couldn't throw exceptions of other types.
""You can't throw exceptions of other types." False," True. If "you" means the function, and not its subparts.
@curiousguy: The function can still throw an exception of any type it pleases. It just won't escape.
|
4

Exception specifications specify what exceptions a function may throw.

3 Comments

No, not really. Although I understand why you'd think that.
More precisely: An exception specification tells you what types of exceptions can escape from a function.
It's still a correct (and very short) answer, though. Yes, you can violate this "contract", but that doesn't really matter. You can break other contracts (constness or protection) too. This one only adds an explicit runtime handler for that, too ... which is not usually the case when you do something other than you've promised.
0

Exceptions specifications, as the name imply, are about specifying which exceptions a function may throw; in general. How the may is enforced however depends on the language.

  • in Java the compiler statically enforces this and will reject the program if ever your function may throw something else (unless the something else derives from a specific base exception class)
  • in C++, the compiler allows the program, however it inserts a runtime check and if ever anything is thrown that was not specified it calls std::unexpected

In general, exceptions specifications are pretty much universally decried because:

  • they do not compose well. If you call two functions throwing each a set X and Y of exceptions, then the resulting function should at least declare the union of X and Y. Since it soon becomes unmanageable you then need to "translate" the exceptions into some common type, wrapping over the original to keep the context. It is not unusual to end up with a 3 or 5 deep exceptions chain.
  • some basic operations may throw. In C++, memory allocation may throw std::bad_alloc; pretty much every single STL collection is thus susceptible for example.

If possible, forget you ever learned about exception specifications.

3 Comments

"How the may is enforced" In Java, an empty throw spec guarantees nothing useful to the caller. In C++ it does; that's enforcement.
@curiousguy: I don't understand your comment. I specifically described how enforcement worked in both languages, and your comment seems to agree with my description for both languages. What's the problem then?
In Java a lot of exceptions are exempted, so many that an empty throw spec is practically meaningless; you write "some basic operations may throw" but in Java all of these are unchecked exception. Not mentioning it makes the comparison unfair. Also a lot of basic building blocks in C++ are guaranteed to be non throwing, making the comparison as described even less fair for C++.

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.