From 58d30975f0d46dc7812b7266c3c2c823695503e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?John=20Ankarstr=C3=B6m?= Date: Sun, 31 Jul 2022 00:38:17 +0200 Subject: Simplify exception handling. --- c/main.cpp | 36 +++++++++++++++++------------------- 1 file changed, 17 insertions(+), 19 deletions(-) (limited to 'c/main.cpp') diff --git a/c/main.cpp b/c/main.cpp index 144d151..7af5a0b 100644 --- a/c/main.cpp +++ b/c/main.cpp @@ -44,32 +44,30 @@ static void WaitFor(const char*, const char*); static INT_PTR CALLBACK AboutDlgProc(HWND, UINT, WPARAM, LPARAM); static void UpdateTheme(); -void TerminateMsg(const wchar_t* wsz1, const wchar_t* wsz2) noexcept -{ - wchar_t wsz[256] = {0}; - if (wsz2) - wszf(wsz, L"Episode Browser was terminated due to %s: %s", wsz1, wsz2); - else - wszf(wsz, L"Episode Browser was terminated due to %s.", wsz1); - MessageBox(g_hWnd, wsz, L"Fatal Error", MB_ICONERROR); -} - void OnTerminate() noexcept { + const wchar_t* wszWhat = L"an exception"; + wstring_owner wsoWhy; + try { std::rethrow_exception(std::current_exception()); } catch (const term_t& t) { - if (auto wso = PlString(t)) - TerminateMsg(L"a Prolog exception", wso.p); - else - TerminateMsg(L"a Prolog exception", NULL); + wszWhat = L"a Prolog exception"; + try { wsoWhy = PlString(t); } catch (...) {} } catch (const Win32Error& e) { - TerminateMsg(L"a Windows error", e.WhatW()); + wszWhat = L"a Windows error"; + try { wsoWhy = WsoCopy(e.WhatW()); } catch (...) {} } catch (const std::exception& e) { - TerminateMsg(L"an exception", WsoFromSz(e.what()).p); - } catch (...) { - TerminateMsg(L"an exception", NULL); - } + try { wsoWhy = WsoFromSz(e.what()); } catch (...) {} + } catch (...) {} + + wchar_t wsz[256] = {0}; + if (wsoWhy) + wszf(wsz, L"Episode Browser was terminated due to %s: %s", wszWhat, wsoWhy.p); + else + wszf(wsz, L"Episode Browser was terminated due to %s.", wszWhat); + + MessageBox(g_hWnd, wsz, L"Fatal Error", MB_ICONERROR); _Exit(1); } -- cgit v1.2.3