aboutsummaryrefslogtreecommitdiff
path: root/c/err.h
diff options
context:
space:
mode:
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