Stroika Library 3.0d16
 
Loading...
Searching...
No Matches
Throw.h
1/*
2 * Copyright(c) Sophist Solutions, Inc. 1990-2025. All rights reserved
3 */
4#ifndef _Stroia_Foundation_Execution_Throw_h_
5#define _Stroia_Foundation_Execution_Throw_h_ 1
6
7#include "Stroika/Foundation/StroikaPreComp.h"
8
9#include <exception>
10#include <memory>
11#include <stdexcept>
12#include <system_error>
13
14#include "Stroika/Foundation/Common/Common.h"
15#include "Stroika/Foundation/Common/Concepts.h"
17
18/**
19 * \note Code-Status: <a href="Code-Status.md#Beta">Beta</a>
20 *
21 * \note \em Design Note
22 * (essentially) All exceptions thrown by Stroika (except where needed by quirks of underlying library
23 * integrated with) inherit from std::exception, or Stroika::Foundation::Execution::SilentException.
24 *
25 * This means that any code which wishes to report an exception can catch these two types, and use
26 * the 'what()' method to report the text of the exception message.
27 *
28 * Sadly, there is no documentation (I'm aware of) or specification of the character set/code page reported
29 * back by the what () on an exception. It tends to be ascii. Stroika guarantees that all exceptions it throws
30 * will use the current SDK characters (@see SDKString). But - its best to check for inheriting from
31 * Exception<>, since the SDK character set might not allow representing some unicode characters.
32 */
33
35
36 /**
37 * qStroika_Foundation_Execution_Throw_TraceThrowpoint controls whether or not Stroika will DbgTrace () on
38 * (essentially) all exception throws.
39 *
40 * This is nearly always desirable, but in applications that do lots of exceptions (probably not a good idea), this can produce
41 * a lot of trace log noise, and some people object to it.
42 *
43 * Since this only affects calls to DbgTrace () - it only has effect if qStroika_Foundation_Debug_DefaultTracingOn is on.
44 *
45 * \note - to turn this on, you can add the flag
46 * --c-define '\#define qStroika_Foundation_Execution_Throw_TraceThrowpoint 0'
47 * to your configure line
48 */
49#ifndef qStroika_Foundation_Execution_Throw_TraceThrowpoint
50#define qStroika_Foundation_Execution_Throw_TraceThrowpoint qStroika_Foundation_Debug_DefaultTracingOn
51#endif
52
53 /**
54 * qStroika_Foundation_Execution_Throw_TraceThrowpointBacktrace is only meant for debugging. If true, then the
55 * overloads to Throw will also include a call to BackTrace, so its easier to track in TraceLogs where an exception
56 * is thrown from (helpful when you don't have a debugger).
57 *
58 * This has no effect unless qStroika_Foundation_Execution_Throw_TraceThrowpoint is also true.
59 */
60#ifndef qStroika_Foundation_Execution_Throw_TraceThrowpointBacktrace
61#define qStroika_Foundation_Execution_Throw_TraceThrowpointBacktrace qStroika_Foundation_Debug_DefaultTracingOn
62#endif
63
64 /**
65 * \brief identical to builtin C++ 'throw' except that it does helpful, type dependent DbgTrace() messages first
66 *
67 * Utility to call a Trace message (hopefully an appropriate one) for an exception being
68 * thrown... But this function is also specialized to do call D::Throw() for several types -
69 * which CAN translate the kind of exception throw. For example, for Platform:Windows::Exception -
70 * ERROR_OUTOFMEMORY is translated to std::bad_alloc ().
71 *
72 * ONLY the first variation (with no traceMessage) is template specialized. The overloads
73 * which take an extra message are JUST for convenience, and vector through the 1-arg overload -
74 * so as to get is specialization.
75 */
76 template <typename T>
77 [[noreturn]] void Throw (T&& e2Throw);
78 template <typename T>
79 [[noreturn]] void Throw (T&& e2Throw, const char* traceMsg);
80 template <typename T>
81 [[noreturn]] void Throw (T&& e2Throw, const wchar_t* traceMsg);
82
83 /**
84 * Just a regular C++ rethrow, but with a DbgTrace message...
85 */
86 [[noreturn]] void ReThrow ();
87 [[noreturn]] void ReThrow (const exception_ptr& e);
88 [[noreturn]] void ReThrow (const char* traceMsg);
89 [[noreturn]] void ReThrow (const exception_ptr& e, const char* traceMsg);
90 [[noreturn]] void ReThrow (const wchar_t* traceMsg);
91 [[noreturn]] void ReThrow (const exception_ptr& e, const wchar_t* traceMsg);
92
93 /**
94 * If the first argument is null, throw the second argument exception (which defaults to bad_alloc)
95 */
96 template <equality_comparable_with<nullptr_t> T, typename E>
97 void ThrowIfNull (const T& p, const E& e);
98 template <equality_comparable_with<nullptr_t> T>
99 void ThrowIfNull (const T& p);
100 template <Common::Weak_Equality_Comparable_With<nullopt_t> T, typename E>
101 void ThrowIfNull (const T& p, const E& e)
102 requires (not equality_comparable_with<nullptr_t, T>);
103 template <Common::Weak_Equality_Comparable_With<nullopt_t> T>
104 void ThrowIfNull (const T& p)
105 requires (not equality_comparable_with<nullptr_t, T>);
106
107 /**
108 * \def IgnoreExceptionsForCall - ignore all exceptions for the given argument call (evaluate arg)
109 *
110 * @see IgnoreExceptionsExceptThreadAbortForCall
111 */
112#define IgnoreExceptionsForCall(theCode) \
113 try { \
114 theCode; \
115 } \
116 catch (...) { \
117 }
118
119 /**
120 * \def IgnoreExceptionsExceptThreadAbortForCall - ignore all exceptions (except thread abort) for the given argument call (evaluate arg)
121 *
122 * @see IgnoreExceptionsForCall
123 */
124#define IgnoreExceptionsExceptThreadAbortForCall(theCode) \
125 try { \
126 theCode; \
127 } \
128 catch (const Stroika::Foundation::Execution::Thread::AbortException&) { \
129 Execution::ReThrow (); \
130 } \
131 catch (...) { \
132 }
133
134}
135
136/*
137 ********************************************************************************
138 ***************************** Implementation Details ***************************
139 ********************************************************************************
140 */
141#include "Throw.inl"
142
143#endif /*_Stroia_Foundation_Execution_Throw_h_*/
void Throw(T &&e2Throw)
identical to builtin C++ 'throw' except that it does helpful, type dependent DbgTrace() messages firs...
Definition Throw.inl:43
void ThrowIfNull(const Private_::ConstVoidStar &p, const HRESULT &hr)
Template specialization for ThrowIfNull (), for thing being thrown HRESULT - really throw HRESULTErro...