Note that there are some explanatory texts on larger screens.

plurals
  1. POHandling C++ exceptions in Java via SWIG
    primarykey
    data
    text
    <p>I'm attempting to use SWIG to wrap a C++ class into a Java class. This C++ class has a method that throws an exception.</p> <p>I have three goals, none of which are currently happening although I have followed the manual as I understand it:</p> <ul> <li>To get the Java class to declare <code>throws &lt;exceptiontype&gt;</code> on the method that throws in C++</li> <li>To get the SWIG generated Exception class to extend <code>java.lang.Exception</code></li> <li>To override <code>Exception.getMessage()</code> in the generated SWIG class.</li> </ul> <p>It seems that the root of the problem seems that my <code>typemap</code>s are not being applied, as none of the above happens. What have I done wrong?</p> <p>Minimal example is below. The C++ does not have to compile, I'm only interested in the generated Java. The class of the exception is irrelevant, and the code below uses IOException just because the documentation uses it. All code is adapted from examples here:</p> <ul> <li><a href="http://www.swig.org/Doc1.3/Java.html#typemap_attributes">http://www.swig.org/Doc1.3/Java.html#typemap_attributes</a></li> <li><a href="http://www.swig.org/Doc1.3/Java.html#exception_typemap">http://www.swig.org/Doc1.3/Java.html#exception_typemap</a></li> </ul> <p>C++ header file (test.h):</p> <pre><code>#include &lt;string&gt; class CustomException { private: std::string message; public: CustomException(const std::string&amp; message) : message(msg) {} ~CustomException() {} std::string what() { return message; } }; class Test { public: Test() {} ~Test() {} void something() throw(CustomException) {}; }; </code></pre> <p>SWIG .i file:</p> <pre><code>%module TestModule %{ #include "test.h" %} %include "std_string.i" // for std::string typemaps %include "test.h" // Allow C++ exceptions to be handled in Java %typemap(throws, throws="java.io.IOException") CustomException { jclass excep = jenv-&gt;FindClass("java/io/IOException"); if (excep) jenv-&gt;ThrowNew(excep, $1.what()); return $null; } // Force the CustomException Java class to extend java.lang.Exception %typemap(javabase) CustomException "java.lang.Exception"; // Override getMessage() %typemap(javacode) CustomException %{ public String getMessage() { return what(); } %} </code></pre> <p>When running this with <code>swig -c++ -verbose -java test.i</code> with SWIG 2.0.4, the exception class does not extend <code>java.lang.Exception</code> and none of the Java methods have a <code>throws</code> declaration.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload