Add raise methods for throwing c++ exceptions in C#, Java, D

The director c++ exceptions are thrown in a helper method instead of in
the director overloaded method. This circumvents compiler warnings about
throwing exceptions when the method has an exception specification or
noexcept. If the exception is thrown, abort will still be called!
In Java, the "director:noexcept" typemap can be used to do something
else. This typemap should be ported to the other languages too.
This commit is contained in:
William S Fulton 2017-05-13 17:01:15 +01:00
commit 07ab80b49e
7 changed files with 29 additions and 5 deletions

View file

@ -3923,12 +3923,24 @@ is used in the feature code. Consider the following, which also happens to be th
if ($error) {
jenv->ExceptionClear();
$directorthrowshandlers
throw Swig::DirectorException(jenv, $error);
Swig::DirectorException::raise(jenv, $error);
}
%}
</pre>
</div>
<p>
where <tt>Swig::DirectorException::raise</tt> is a helper method in the DirectorException class to throw a C++ exception (implemented in director.swg):
</p>
<div class="code">
<pre>
static void raise(JNIEnv *jenv, jthrowable throwable) {
throw DirectorException(jenv, throwable);
}
</pre>
</div>
<p>The code generated using the <code>director:except</code> feature
replaces the <code>$directorthrowshandlers</code> special variable with the code in
the "directorthrows" typemaps, for each and every exception defined for the method.
@ -3963,7 +3975,7 @@ if (swigerror) {
throw std::out_of_range(Swig::JavaExceptionMessage(jenv, swigerror).message());
}
throw Swig::DirectorException(jenv, swigerror);
Swig::DirectorException::raise(jenv, swigerror);
}
</pre>
</div>

View file

@ -41,6 +41,10 @@ namespace Swig {
public:
DirectorPureVirtualException(const char *msg) : DirectorException(std::string("Attempt to invoke pure virtual method ") + msg) {
}
static void raise(const char *msg) {
throw DirectorPureVirtualException(msg);
}
};
}

View file

@ -40,6 +40,10 @@ namespace Swig {
public:
DirectorPureVirtualException(const char *msg) : DirectorException(std::string("Attempted to invoke pure virtual method ") + msg) {
}
static void raise(const char *msg) {
throw DirectorPureVirtualException(msg);
}
};
}

View file

@ -356,6 +356,10 @@ namespace Swig {
}
}
static void raise(JNIEnv *jenv, jthrowable throwable) {
throw DirectorException(jenv, throwable);
}
private:
static char *copypath(const char *srcmsg) {
char *target = copystr(srcmsg);

View file

@ -3935,7 +3935,7 @@ public:
}
Delete(super_call);
} else {
Printf(w->code, " throw Swig::DirectorPureVirtualException(\"%s::%s\");\n", SwigType_namestr(c_classname), SwigType_namestr(name));
Printf(w->code, "Swig::DirectorPureVirtualException::raise(\"%s::%s\");\n", SwigType_namestr(c_classname), SwigType_namestr(name));
}
if (!ignored_method)

View file

@ -2077,7 +2077,7 @@ public:
}
Delete(super_call);
} else {
Printf(w->code, " throw Swig::DirectorPureVirtualException(\"%s::%s\");\n", SwigType_namestr(c_classname), SwigType_namestr(name));
Printf(w->code, "Swig::DirectorPureVirtualException::raise(\"%s::%s\");\n", SwigType_namestr(c_classname), SwigType_namestr(name));
}
if (!ignored_method)

View file

@ -4490,7 +4490,7 @@ public:
Printf(directorexcept, "jthrowable $error = jenv->ExceptionOccurred();\n");
Printf(directorexcept, "if ($error) {\n");
Printf(directorexcept, " jenv->ExceptionClear();$directorthrowshandlers\n");
Printf(directorexcept, " throw Swig::DirectorException(jenv, $error);\n");
Printf(directorexcept, " Swig::DirectorException::raise(jenv, $error);\n");
Printf(directorexcept, "}\n");
} else {
directorexcept = Copy(directorexcept);