{"id":102849,"date":"2019-09-11T07:00:00","date_gmt":"2019-09-11T14:00:00","guid":{"rendered":"http:\/\/devblogs.microsoft.com\/oldnewthing\/?p=102849"},"modified":"2019-09-10T20:07:09","modified_gmt":"2019-09-11T03:07:09","slug":"20190911-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20190911-00\/?p=102849","title":{"rendered":"The sad history of the MFC <CODE>TRY<\/CODE>\/<CODE>CATCH<\/CODE> macros"},"content":{"rendered":"<p>Why does MFC <a href=\"https:\/\/docs.microsoft.com\/en-us\/cpp\/mfc\/exceptions-using-mfc-macros-and-cpp-exceptions?view=vs-2019\"> define <code>TRY<\/code>\/<code>CATCH<\/code> macros<\/a>? Why can&#8217;t it use standard C++ exception handling?<\/p>\n<p>This is another case of asking <a href=\"https:\/\/devblogs.microsoft.com\/oldnewthing\/20110119-00\/?p=11723\"> why they didn&#8217;t use the Space Shuttle to rescue Apollo 13<\/a>.<\/p>\n<p>MFC was first released in 1992. At that time, C++ was a cool new programming language that everybody was interested in. Back in the early days, there were no templates, there was no standard library, <a href=\"https:\/\/devblogs.microsoft.com\/oldnewthing\/20180607-00\/?p=98935\"> no placement new<\/a>, no namespaces, and no exceptions.\u00b9<\/p>\n<p>That meant that the MFC team had to invent their own exceptions, which they did with macros like <code>TRY<\/code> and <code>CATCH<\/code>. These macros were deprecated in MFC 3.0 (released in 1994), but for compatibility the macros continue to exist so that pre-MFC-3.0 code remains source-level compatible. The macros now just do standard C++ <code>try<\/code> and <code>catch<\/code> operations, although there&#8217;s a <code>_AFX_<\/code><code>OLD_<\/code><code>EXCEPTIONS<\/code> symbol you can define to make them revert to the old <code>setjmp<\/code>-based fake exception handling, for binary compatibility.<\/p>\n<p>MSDN has a <a href=\"https:\/\/docs.microsoft.com\/en-us\/cpp\/mfc\/exceptions-converting-from-mfc-exception-macros?view=vs-2017\"> guide to converting from MFC exceptions to C++ exceptions<\/a>. So convert your code already. You&#8217;ve had 25 years.<\/p>\n<p>\u00b9 This implies that a failed <code>new<\/code> didn&#8217;t throw a <code>std::bad_alloc<\/code> exception, seeing as neither namespaces nor exceptions existed yet! Instead, a failed <code>new<\/code> simply returned <code>NULL<\/code>.\u00b2<\/p>\n<p>\u00b2 The <code>nullptr<\/code> keyword wouldn&#8217;t exist for nearly 20 years.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Because standard C++ exception handling didn&#8217;t exist yet.<\/p>\n","protected":false},"author":1069,"featured_media":111744,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[2],"class_list":["post-102849","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-history"],"acf":[],"blog_post_summary":"<p>Because standard C++ exception handling didn&#8217;t exist yet.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/102849","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/users\/1069"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/comments?post=102849"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/102849\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media\/111744"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media?parent=102849"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=102849"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=102849"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}