diff options
author | John Ankarström <john@ankarstrom.se> | 2022-09-07 00:40:26 +0200 |
---|---|---|
committer | John Ankarström <john@ankarstrom.se> | 2022-09-07 00:40:26 +0200 |
commit | 2f7b69d6d4cf18ca9ca04d9a44aaa6871ce51160 (patch) | |
tree | d016a9456621b91ca902f0c1caf48468e899a553 /c/err.h | |
parent | 5b1a07607ba593050e37598f731f833b6faabee4 (diff) | |
download | EpisodeBrowser-2f7b69d6d4cf18ca9ca04d9a44aaa6871ce51160.tar.gz |
Improve error handling.
Diffstat (limited to 'c/err.h')
-rw-r--r-- | c/err.h | 32 |
1 files changed, 32 insertions, 0 deletions
@@ -4,6 +4,8 @@ #include <exception> #include <string> +#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 F> +auto Except = (decltype(F))[](auto... xs) +{ + try { + return F(xs...); + } catch (...) { + EBMessageBox(What(), L"Error"); + if constexpr (!std::is_same_v<decltype(F(xs...)), void>) + return decltype(F(xs...)){}; + } +}; + +/* Exception-catching version of F that exits on exception. */ +template <auto F> +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<decltype(F(xs...)), void>) + return decltype(F(xs...)){}; + } +}; + #endif |