From f1f9743e14e2da5345824380d8eeb1d5b4e7c810 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?John=20Ankarstr=C3=B6m?= Date: Fri, 15 Jul 2022 13:15:13 +0200 Subject: Show error and exit gracefully on uncaught exception. --- c/main.cpp | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) (limited to 'c') diff --git a/c/main.cpp b/c/main.cpp index f0adee9..90f828d 100644 --- a/c/main.cpp +++ b/c/main.cpp @@ -23,6 +23,7 @@ int g_bViewWatched = 1; int g_bThread = 0; int g_iDPI = 96; static int g_cxVScroll; +void OnTerminate(void); static LRESULT CALLBACK CBTProc(int, WPARAM, LPARAM); static LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); static INT_PTR CALLBACK AboutDlgProc(HWND, UINT, WPARAM, LPARAM); @@ -34,6 +35,9 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, { LPTSTR tszErr; + /* Exit gracefully on uncaught exception. */ + std::set_terminate(OnTerminate); + /* Set constant values. */ if (auto upLib = try_make(TEXT("uxtheme.dll"))) if (upLib->GetProcAddress("SetWindowTheme")) { @@ -123,11 +127,30 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PL_halt(0); return 0; -f: MessageBox(NULL, tszErr, TEXT("Error"), MB_ICONERROR); +f: MessageBox(NULL, tszErr, TEXT("Fatal Error"), MB_ICONERROR); PL_halt(1); return 1; } +void OnTerminate() +{ + try { + std::rethrow_exception(std::current_exception()); + } catch (term_t &t) { + char *sz; + TCHAR *tsz; + if (PL_get_chars(t, &sz, CVT_WRITE)) /* TODO: PL_get_wchars */ + tsz = TszFromSz(sz, CP_UTF8); + else + tsz = TEXT("The program was terminated due to an exception."); + MessageBox(NULL, tsz, TEXT("Fatal Error"), MB_ICONERROR); + } catch (...) { + MessageBox(NULL, TEXT("The program was terminated due to an exception."), + TEXT("Fatal Error"), MB_ICONERROR); + } + std::_Exit(1); +} + static LRESULT CALLBACK CBTProc(int nCode, WPARAM wParam, LPARAM lParam) { if (nCode != HCBT_CREATEWND) return 0; -- cgit v1.2.3