{"id":40,"date":"2010-04-27T14:57:27","date_gmt":"2010-04-27T14:57:27","guid":{"rendered":"http:\/\/miklcct.no-ip.org\/wordpress\/?p=40"},"modified":"2016-10-31T07:52:27","modified_gmt":"2016-10-31T07:52:27","slug":"dont-use-c-exception-specifications","status":"publish","type":"post","link":"https:\/\/miklcct.com\/wordpress\/2010\/04\/27\/dont-use-c-exception-specifications\/","title":{"rendered":"Don&#8217;t use C++ exception specifications"},"content":{"rendered":"<p>Exception specifications should be very familiar to Java programmers. Here is an example:<\/p>\n<pre>\r\nclass MyException extends Exception {\r\n}\r\n\r\nclass Foo {\r\n    void abc(int x) throws MyException {\r\n        if (x &lt; 0) throw new MyException();\r\n    }\r\n\r\n    void def() throws MyException {\r\n        abc(3);\r\n    }\r\n}\r\n<\/pre>\n<p>In the code above, <code>Foo.abc(int)<\/code> may throw <code>MyException<\/code>, in the Java language, it must be declared to be thrown (except classes inheriting from <code>Error<\/code> or <code>RuntimeException<\/code>). <code>Foo.def()<\/code> must also be declared to throw <code>MyException<\/code> because it calls <code>Foo.abc()<\/code> that may throw <code>MyException<\/code> and does not catch it (although <code>Foo.def()<\/code> actually never throws exception). Any attempt to throw exceptions (except classes inheriting from <code>Error<\/code> or <code>RuntimeException<\/code>) or ignore exceptions thrown from method calls that are not declared in the signature is a compile-time error. This also applies to extending classes or implementing interfaces. An overriding method or implementing method can declare only stricter exception specifications i.e. a subset of the overridden method or implemented method but not any exceptions not found in the overridden method or implemented method. The whole mechanism is called checked exception and is a part of the type system in Java.<\/p>\n<p>Now comes to C++. C++ has also exception specification and can be applied on any function declaration, pointer to function declaration, reference of function declaration and pointer-to-member function declaration but not in a <code>typedef<\/code> declaration. Here is the syntax:<br \/>\n<code><br \/>\nvoid func() throw(int);<br \/>\n<\/code><br \/>\nThe above declaration says that <code>func()<\/code> may throw only objects of <code>int<\/code>. If the specification is omitted, unlike in Java, a function may throw anything. To specify that a function may not throw anything, an empty exception specification is needed. The restriction on deriving classes is the same as in Java. An overriding member function can only throw a subset of the overridden function. Also, there are restrictions on assigning pointers to functions. A pointer to function can be assigned to a pointer object to function only if the specification in source value is more restrictive than the specification in destination object.<\/p>\n<p>However, there is a fatal design flaw in the whole system: exceptions are checked at run-time instead of at compile-time. For example, the following code can be compiled:<\/p>\n<pre>\r\nvoid oops() throw(int) {\r\n    throw 3.0;\r\n}\r\n\r\nint main() {\r\n    try {\r\n        oops();\r\n    } catch (double &amp;e) {\r\n    }\r\n}\r\n<\/pre>\n<p><code>oops()<\/code> is declared to throw objects of <code>int<\/code> only but an object of <code>double<\/code> is thrown. In this case, the <code>double<\/code> object cannot be caught, instead, <code>std::unexpected()<\/code> is called. The default action is to call <code>std::terminate()<\/code> which in term abort the program. Although you can supply a custom version of <code>std::unexpected()<\/code>, it is not allowed to return normally. It can instead throw an allowed exception or terminate the program but the use is not practical because there can be only one handler which is very difficult to handle all unexpected exceptions. Furthermore, the use of exception specifications may create a performance penalty because the compiler must emit code to call <code>std::unexcepted()<\/code> in case of exception. The compiler may simply refuse to inline some trivial functions if an exception specification is used.<\/p>\n<p>C++ exception specifications, unlike Java, is flawed by design. NEVER use it.<\/p>\n<p>P.S. C++ exception specifications have been deprecated in the next edition of standard, C++0x.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Exception specifications should be very familiar to Java programmers. Here is an example: class MyException extends Exception { } class Foo { void abc(int x) throws MyException { if (x &lt; 0) throw new MyException(); } void def() throws MyException { abc(3);&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[21],"class_list":["post-40","post","type-post","status-publish","format-standard","hentry","category-programming","tag-cc"],"_links":{"self":[{"href":"https:\/\/miklcct.com\/wordpress\/wp-json\/wp\/v2\/posts\/40","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/miklcct.com\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/miklcct.com\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/miklcct.com\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/miklcct.com\/wordpress\/wp-json\/wp\/v2\/comments?post=40"}],"version-history":[{"count":7,"href":"https:\/\/miklcct.com\/wordpress\/wp-json\/wp\/v2\/posts\/40\/revisions"}],"predecessor-version":[{"id":165,"href":"https:\/\/miklcct.com\/wordpress\/wp-json\/wp\/v2\/posts\/40\/revisions\/165"}],"wp:attachment":[{"href":"https:\/\/miklcct.com\/wordpress\/wp-json\/wp\/v2\/media?parent=40"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/miklcct.com\/wordpress\/wp-json\/wp\/v2\/categories?post=40"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/miklcct.com\/wordpress\/wp-json\/wp\/v2\/tags?post=40"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}