aboutsummaryrefslogtreecommitdiff
path: root/c/err.h
diff options
context:
space:
mode:
authorJohn Ankarström <john@ankarstrom.se>2022-09-07 00:40:26 +0200
committerJohn Ankarström <john@ankarstrom.se>2022-09-07 00:40:26 +0200
commit2f7b69d6d4cf18ca9ca04d9a44aaa6871ce51160 (patch)
treed016a9456621b91ca902f0c1caf48468e899a553 /c/err.h
parent5b1a07607ba593050e37598f731f833b6faabee4 (diff)
downloadEpisodeBrowser-2f7b69d6d4cf18ca9ca04d9a44aaa6871ce51160.tar.gz
Improve error handling.
Diffstat (limited to 'c/err.h')
-rw-r--r--c/err.h32
1 files changed, 32 insertions, 0 deletions
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 <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