diff --git a/Doc/Manual/C.html b/Doc/Manual/C.html index 356380210..03ac9954a 100644 --- a/Doc/Manual/C.html +++ b/Doc/Manual/C.html @@ -683,6 +683,10 @@ void SomeIntTemplateClass_delete(SomeIntTemplateClass * carg1);
+Any call to a C++ function may throw an exception, which cannot be caught by C code. Instead, the special SWIG_CException_get_pending() function must be called to check for this. If it returns a non-null pointer, SWIG_CException_msg_get() can be called to retrieve the error message associated with the exception. Finally, SWIG_CException_reset_pending() must be called to free the exception object and reset the current pending exception. Note that exception handling is much simpler when using C++, rather than C, wrappers, see sections 36.6.2. +
+@@ -702,8 +706,52 @@ Other ones are due to things that could be supported but haven't been implemente
-Note that cxxheader section can be used to output additional -declarations to the C++-only part of the generated header. ++Exception handling in C++ is more natural, as the exceptions are re-thrown when using C++ wrappers and so can be caught, as objects of the special SWIG_CException type, using the usual try/catch statement. The objects of SWIG_CException class have code() and msg() methods, with the latter returning the error message associated with the exception. +
+ ++If necessary, a custom exception type may be used instead of SWIG_CException. To do this, a custom implementation of swig_check() function, called to check for the pending exception and throw the corresponding C++ exception if necessary, must be provided and SWIG_swig_check_DEFINED preprocessor symbol must be defined to prevent the default implementation of this function from being compiled: +
+
+%insert(cxxheader) %{
+#ifndef SWIG_swig_check_DEFINED
+#define SWIG_swig_check_DEFINED 1
+
+#include
+
+class Exception : public std::runtime_error {
+public:
+ explicit Exception(const char* msg) : std::runtime_error{msg} {}
+};
+
+inline void swig_check() {
+ if (auto* swig_ex = SWIG_CException_get_pending()) {
+ Exception const e{SWIG_CException_msg_get(swig_ex)};
+ SWIG_CException_reset_pending();
+ throw e;
+ }
+}
+
+template T swig_check(T x) {
+ swig_check();
+ return x;
+}
+
+#endif // SWIG_swig_check_DEFINED
+%}
+