aboutsummaryrefslogtreecommitdiff
path: root/c
diff options
context:
space:
mode:
authorJohn Ankarström <john@ankarstrom.se>2022-07-15 13:15:13 +0200
committerJohn Ankarström <john@ankarstrom.se>2022-07-15 13:15:13 +0200
commitf1f9743e14e2da5345824380d8eeb1d5b4e7c810 (patch)
tree7540325185f11190d80e5b4265c7eb3b08669cf3 /c
parent8f1ead168558dac9cf4c10e269f568e15b5f8faf (diff)
downloadEpisodeBrowser-f1f9743e14e2da5345824380d8eeb1d5b4e7c810.tar.gz
Show error and exit gracefully on uncaught exception.
Diffstat (limited to 'c')
-rw-r--r--c/main.cpp25
1 files changed, 24 insertions, 1 deletions
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<Library>(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;