From 25f261a4d7f89e780aa9558c6454a58022d965db Mon Sep 17 00:00:00 2001 From: Howard Kapustein Date: Wed, 3 Mar 2021 10:33:33 -0800 Subject: [PATCH 1/4] Add note about catching exceptions in C++ --- docs/Coding-Guidelines.md | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/docs/Coding-Guidelines.md b/docs/Coding-Guidelines.md index ec35227409..b208ed6358 100644 --- a/docs/Coding-Guidelines.md +++ b/docs/Coding-Guidelines.md @@ -4,17 +4,31 @@ Project Reunion prefers using industry-standard coding styles, guidelines, and p #### C++ -Project Reunion implementations prefer C++ and C++/WinRT implementations. New code must adhere to the **cppcoreguidelines** at +Project Reunion implementations prefer C++ and C++/WinRT implementations. New code must adhere to the **cppcoreguidelines** at https://github.com/isocpp/CppCoreGuidelines and be /W4 clean (for Visual C++.) -Prefer existing std:: and gsl:: types, but use **wil** (https://github.com/Microsoft/wil) types for Windows-specific helpers and lifecycle management +Prefer existing std:: and gsl:: types, but use **wil** (https://github.com/Microsoft/wil) types for Windows-specific helpers and lifecycle management rather than creating your own. +##### Catching Exceptions and HRESULT + +Don't just catch winrt::hresult_error or other variations as that doesn't catch std::bad_alloc. + +Use the following snippet to catch exceptions and retrieve their HRESULT: + +``` +catch (...) +{ + auto e{ hresult_error(to_hresult(), take_ownership_from_abi) }; + ...e.code() contains the HRESULT... +} +``` + #### Markdown -**GUIDELINE:** The preferred line length limit is ~100 characters. GitHub formats lines regardless of individual length but GitHub diff is line oriented. +**GUIDELINE:** The preferred line length limit is ~100 characters. GitHub formats lines regardless of individual length but GitHub diff is line oriented. Keeping lines within the preferred limit makes changes easier to review. #### Other Languages -If new languages become common, we will describe the coding guidelines for such languages here. +If new languages become common, we will describe the coding guidelines for such languages here. From 25401da1933521b77a4880e70568f9b2688f04e1 Mon Sep 17 00:00:00 2001 From: Howard Kapustein Date: Wed, 3 Mar 2021 10:36:43 -0800 Subject: [PATCH 2/4] Minor formatting improvements --- docs/Coding-Guidelines.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/Coding-Guidelines.md b/docs/Coding-Guidelines.md index b208ed6358..bb14229d12 100644 --- a/docs/Coding-Guidelines.md +++ b/docs/Coding-Guidelines.md @@ -12,7 +12,7 @@ rather than creating your own. ##### Catching Exceptions and HRESULT -Don't just catch winrt::hresult_error or other variations as that doesn't catch std::bad_alloc. +Don't just catch `winrt::hresult_error` or other variations as that doesn't catch `std::bad_alloc`. Use the following snippet to catch exceptions and retrieve their HRESULT: From b5005e68dacea2984787a0a9ef945590084b58f4 Mon Sep 17 00:00:00 2001 From: Howard Kapustein Date: Thu, 4 Mar 2021 10:40:51 -0800 Subject: [PATCH 3/4] Added C++ tagging to a code snippet --- docs/Coding-Guidelines.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/Coding-Guidelines.md b/docs/Coding-Guidelines.md index bb14229d12..8ce03bc23a 100644 --- a/docs/Coding-Guidelines.md +++ b/docs/Coding-Guidelines.md @@ -16,7 +16,7 @@ Don't just catch `winrt::hresult_error` or other variations as that doesn't catc Use the following snippet to catch exceptions and retrieve their HRESULT: -``` +```c++ catch (...) { auto e{ hresult_error(to_hresult(), take_ownership_from_abi) }; From 26ff5febe7b2d62e93786751b1d5acdbc187e3a8 Mon Sep 17 00:00:00 2001 From: Howard Kapustein Date: Fri, 5 Mar 2021 18:07:55 -0800 Subject: [PATCH 4/4] Incorporated feedback --- docs/Coding-Guidelines.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/Coding-Guidelines.md b/docs/Coding-Guidelines.md index 8ce03bc23a..ab3fc4aa18 100644 --- a/docs/Coding-Guidelines.md +++ b/docs/Coding-Guidelines.md @@ -19,8 +19,8 @@ Use the following snippet to catch exceptions and retrieve their HRESULT: ```c++ catch (...) { - auto e{ hresult_error(to_hresult(), take_ownership_from_abi) }; - ...e.code() contains the HRESULT... + auto e{ winrt::hresult_error(winrt::to_hresult(), winrt::take_ownership_from_abi) }; + ...e.code() contains the WinRT exception or best-guess conversion from a C++ exception... } ```