Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Compiler-verified exceptions as part of a function's signature have two (theoretical) advantages: compiler optimizations and compile-time error checking.</p> <p>What is the difference, in terms of the compiler, between a function that throws exception class <code>X</code> and class <code>Y</code>? Ultimately... nothing at all. What kind of optimization could the compiler do with exception class <code>X</code> that it couldn't do with <code>Y</code>? Unless <code>std::exception</code> were special (and <code>X</code> were derived from it, while <code>Y</code> was not), what would it matter to the compiler?</p> <p>Ultimately, the only thing a compiler would care about in terms of optimization is whether a function will throw <em>any</em> exceptions or not. That's why the standards committee for C++11 ditched <code>throw(...)</code> in favor of <code>noexcept</code>, which states that the function will throw nothing.</p> <p>As for compile-time error checking, Java clearly shows how well this works. You're writing a function, <code>foo</code>. Your design has that it throws <code>X</code> and <code>Y</code>. Other pieces of code use <code>foo</code>, and they throw whatever <code>foo</code> throws. But the exception specification doesn't say "whatever <code>foo</code> throws". It must list <code>X</code> and <code>Y</code> specifically.</p> <p>Now you go back and change <code>foo</code> so that it no longer throws <code>X</code>, but now it throws <code>Z</code>. Suddenly, the entire project stops compiling. You must now go to every function that throws whatever <code>foo</code> threw just to change its exception specification to match <code>foo</code>.</p> <p>Eventually, a programmer just throws up their hand and says that it throws any exception. When you abandon a feature like that, it's a de facto admission that the feature is doing more harm than good.</p> <p>It's not that they <em>cannot</em> be useful. It's just that actual use of them shows that they're generally not useful. So there's no point.</p> <p>Plus, remember that C++'s specification states that no specification means that anything will be thrown, not nothing (as in Java). The simplest way to use the language is exactly that way: no checking. So there are going to be plenty of people who don't want the bother of using it.</p> <p>What good is a feature that many don't want to bother with, and even those who do will generally get a lot of grief out of it?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload