Home:ALL Converter>Why exception must be declared?

Why exception must be declared?

Ask Time:2015-04-09T10:27:19         Author:zell

Json Formatter

It is recognized that, in C++ for example, if a method throws an exception, it must be declared.  But the compelling reason why such rule has to be applied seems to be less known for some people (I asked some colleagues, no convincing answers).

Can someone explains to me, maybe from the compiler side, that why we absolutely need to declare exception in methods that throw it? Thanks.

Author:zell,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/29528704/why-exception-must-be-declared
user2030052 :

As the commenters have already stated, you don't need to put exception specifications on functions (via throw(), which was deprecated in C++11). This is a requirement of Java (i.e checked exceptions, as they're called). \n\nHowever, what you can do in C++11 is mark functions to be noexcept, meaning that they won't throw. This gives the compiler some freedom to optimize some things it otherwise won't be able to because it doesn't know whether the function can throw or not. The consequences of using noexcept specifications on functions would be that if they somehow do throw, std::terminate will be called, effectively \"crashing\" your application. Hope this helps.",
2015-04-09T02:41:43
yy