From 2f7b69d6d4cf18ca9ca04d9a44aaa6871ce51160 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?John=20Ankarstr=C3=B6m?= Date: Wed, 7 Sep 2022 00:40:26 +0200 Subject: Improve error handling. --- c/err.h | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) (limited to 'c/err.h') diff --git a/c/err.h b/c/err.h index d4a9976..4c6e829 100644 --- a/c/err.h +++ b/c/err.h @@ -4,6 +4,8 @@ #include #include +#include "win32.h" + enum ErrType : unsigned char { GENERIC, @@ -22,4 +24,34 @@ struct Err /* Return a wide string describing exception. */ std::wstring What(std::exception_ptr e = std::current_exception()); +/* Exit gracefully on uncaught exception. */ +void OnTerminate(); + +/* Exception-catching version of F that warns on exception. */ +template +auto Except = (decltype(F))[](auto... xs) +{ + try { + return F(xs...); + } catch (...) { + EBMessageBox(What(), L"Error"); + if constexpr (!std::is_same_v) + return decltype(F(xs...)){}; + } +}; + +/* Exception-catching version of F that exits on exception. */ +template +auto Noexcept = (decltype(F))[](auto... xs) +{ + try { + return F(xs...); + } catch (...) { + EBMessageBox(What(), L"Fatal Error", MB_ICONERROR); + exit(1); + if constexpr (!std::is_same_v) + return decltype(F(xs...)){}; + } +}; + #endif -- cgit v1.2.3