{"id":6889,"date":"2014-05-12T10:52:35","date_gmt":"2014-05-12T09:52:35","guid":{"rendered":"https:\/\/blogs.mentor.com\/colinwalls\/?p=6889"},"modified":"2026-03-26T16:43:19","modified_gmt":"2026-03-26T20:43:19","slug":"exception-handling-in-c-developers-are-wary","status":"publish","type":"post","link":"https:\/\/blogs.sw.siemens.com\/embedded-software\/2014\/05\/12\/exception-handling-in-c-developers-are-wary\/","title":{"rendered":"Exception handling in C++ &#8211; developers are wary"},"content":{"rendered":"<p>As I <a href=\"https:\/\/blogs.mentor.com\/colinwalls\/blog\/2014\/05\/06\/dynamic-memory-in-real-time-systems-a-solution\/\" target=\"_blank\" rel=\"noopener noreferrer\">mentioned last week<\/a>, I am very much in &#8220;C++ mode&#8221; just now, mainly because I am preparing for some online classes. As a result of various social media contacts, I am getting some interesting impressions of how C++ is viewed among embedded developers. There is certainly much controversy. Some developers love it, but many are very critical.<\/p>\n<p>Various aspects of the language concern engineers, and a particular one is exception handling &#8230;<!--more--><\/p>\n<p>The Exception Handling System [EHS] in C++ has nothing to be with exceptions in the sense of being hardware interrupts. It is designed to handle exceptional situations that are detected by the software. The intention is to provide a controlled failure mode, where a smooth exit from some deeply nested function calls is required [without resorting to <strong>goto<\/strong>].<\/p>\n<p><a href=\"https:\/\/blogs.sw.siemens.com\/wp-content\/uploads\/sites\/51\/2014\/05\/EHS.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignright wp-image-6891\" src=\"https:\/\/blogs.sw.siemens.com\/wp-content\/uploads\/sites\/51\/2014\/05\/EHS-520x484.png\" alt=\"EHS\" width=\"300\" height=\"279\" \/><\/a>The use of EHS is quite straightforward. Code that needs EHS activated during its execution is enclosed in a <strong>try<\/strong> block. A <strong>throw<\/strong> statement is used to assert an exception, which is identified by means of a class. Processing of the exception is performed by some code in a <strong>catch<\/strong> block. There is normally one catch block for each type of exception that may be asserted.<\/p>\n<p>This seems quite neat and simple and does provide a way to write fairly readable code in which exception handling is needed. However, there are some key points that should make embedded developers wary:<\/p>\n<ul>\n<li>If you are going to use the EHS, the additional code much be incorporated [a compiler option] for the entire application. It is not obvious to the human reader [or the compiler] what functions might be called [indirectly] by the code in a <strong>try<\/strong> block. This additional code adds size and reduces execution performance.<\/li>\n<li>Many compilers default to including EHS code. This means that the unwitting user incorporates the overhead automatically, even if they have no intention of using the EHS. A compiler switch is normally available to activate or deactivate the EHS code generation.<\/li>\n<li>If you application&#8217;s exception handling needs are simple, there are two ways to deploy the EHS which reduce the overheads:<\/li>\n<\/ul>\n<ol>\n<ol>\n<li>Use a generic <strong>catch<\/strong> block [where the type is specified with &#8220;&#8230;&#8221;] instead of one for each type of exception.<\/li>\n<li>Do not include any <strong>catch<\/strong> blocks. This results in the library function <strong>terminate()<\/strong> being called when an exception is thrown. This function can be customized.<\/li>\n<\/ol>\n<\/ol>\n<p>Personally, I would shy away from using EHS, but, used with care, it may be beneficial to many designs. The overhead should be carefully measured.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>As I mentioned last week, I am very much in &#8220;C++ mode&#8221; just now, mainly because I am preparing for&#8230;<\/p>\n","protected":false},"author":71677,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spanish_translation":"","french_translation":"","german_translation":"","italian_translation":"","polish_translation":"","japanese_translation":"","chinese_translation":"","footnotes":""},"categories":[1],"tags":[313,577,300,578],"industry":[],"product":[],"coauthors":[],"class_list":["post-6889","post","type-post","status-publish","format-standard","hentry","category-news","tag-c","tag-ehs","tag-embedded-software","tag-exception-handling"],"_links":{"self":[{"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/posts\/6889","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/users\/71677"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/comments?post=6889"}],"version-history":[{"count":1,"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/posts\/6889\/revisions"}],"predecessor-version":[{"id":10269,"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/posts\/6889\/revisions\/10269"}],"wp:attachment":[{"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/media?parent=6889"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/categories?post=6889"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/tags?post=6889"},{"taxonomy":"industry","embeddable":true,"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/industry?post=6889"},{"taxonomy":"product","embeddable":true,"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/product?post=6889"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/coauthors?post=6889"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}