From 01124be71c818a6ac39f55d1a0eed6235864855f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?John=20Ankarstr=C3=B6m?= Date: Tue, 23 Aug 2022 01:41:58 +0200 Subject: Make exceptions within WndProc non-fatal. --- c/main.cpp | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) (limited to 'c/main.cpp') diff --git a/c/main.cpp b/c/main.cpp index e3e2f82..8dc5eab 100644 --- a/c/main.cpp +++ b/c/main.cpp @@ -22,14 +22,17 @@ #define YMAIN CW_USEDEFAULT #endif -/* Exit gracefully on uncaught exception. */ -static void OnTerminate() noexcept; -static auto UNUSED = std::set_terminate(OnTerminate); - /* main.cpp defines all global (non-template) variables used in the * program. `extern' is used to access them from other files, when * need be. */ +/* Exit gracefully on uncaught exception. */ +static auto _ = std::set_terminate([]() noexcept +{ + ShowException(L"Episode Browser was terminated due to an error: %s", L"Fatal Error", MB_ICONERROR); + _Exit(1); +}); + /* Looked-up constants. */ int g_dpi = 96; @@ -70,6 +73,7 @@ BOOL (*SetWindowTheme)(HWND, const wchar_t*, const wchar_t*); static void InitializeMainWindow(HWND); /* Process parent window commands. */ static LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); +static LRESULT CALLBACK HandleMsg(HWND, UINT, WPARAM, LPARAM); /* Process main menu commands. */ static void HandleMainMenu(HWND, WORD); /* Wait for thread. */ @@ -79,12 +83,6 @@ static INT_PTR CALLBACK AboutDlgProc(HWND, UINT, WPARAM, LPARAM); /* Try to style application according to current Windows theme. */ static void UpdateTheme(); -void OnTerminate() noexcept -{ - ShowException(L"Episode Browser was terminated due to an error: %s", L"Fatal Error", MB_ICONERROR); - _Exit(1); -} - int WINAPI WinMain(const HINSTANCE hInstance, const HINSTANCE, char* const, const int nCmdShow) { setbuf(stdout, nullptr); @@ -204,6 +202,16 @@ void InitializeMainWindow(const HWND hWnd) } LRESULT CALLBACK WndProc(const HWND hWnd, const UINT uMsg, const WPARAM wParam, const LPARAM lParam) +{ + try { + return HandleMsg(hWnd, uMsg, wParam, lParam); + } catch (...) { + ShowException(L"The action was cancelled due to an error: %s"); + } + return DefWindowProc(hWnd, uMsg, wParam, lParam); +} + +LRESULT CALLBACK HandleMsg(const HWND hWnd, const UINT uMsg, const WPARAM wParam, const LPARAM lParam) { switch (uMsg) { case WM_CREATE: -- cgit v1.2.3