aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn Ankarström <john@ankarstrom.se>2022-08-23 01:41:58 +0200
committerJohn Ankarström <john@ankarstrom.se>2022-08-23 01:41:58 +0200
commit01124be71c818a6ac39f55d1a0eed6235864855f (patch)
tree59f03afa8cbe8115e6925d8f2e2505504b4e50be
parent5d0979480ff50390b3883795ec2093e0d3d6193b (diff)
downloadEpisodeBrowser-01124be71c818a6ac39f55d1a0eed6235864855f.tar.gz
Make exceptions within WndProc non-fatal.
-rw-r--r--c/main.cpp28
1 files changed, 18 insertions, 10 deletions
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);
@@ -205,6 +203,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:
UpdateTheme();