Skip to content

Instantly share code, notes, and snippets.

@robinhouston
Created February 16, 2012 23:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robinhouston/1848636 to your computer and use it in GitHub Desktop.
Save robinhouston/1848636 to your computer and use it in GitHub Desktop.
Test case for throwing an exception in Java and catching it in C++
/* Derived from the example at
* http://gcc.gnu.org/onlinedocs/gcj/Invocation.html
*
* Compile using:
* /usr/local/bin/g++ -c unholy.cc
* /usr/local/bin/gcj -lstdc++ unholy.o -o unholy
*/
#pragma GCC java_exceptions
#include <gcj/cni.h>
#include <java/io/PrintStream.h>
#include <java/lang/Integer.h>
#include <java/lang/String.h>
#include <java/lang/System.h>
#include <java/lang/Throwable.h>
#include <iostream>
using namespace std;
int main(int argc, char** argv)
{
try {
JvCreateJavaVM(NULL);
JvAttachCurrentThread(NULL, NULL);
java::lang::String *n = JvNewStringLatin1("23skidoo");
JvInitClass(&java::lang::Integer::class$);
java::lang::Integer::parseInt(n);
JvDetachCurrentThread();
}
catch (java::lang::Throwable* t_p) {
cerr << "Unhandled Java Exception:" << endl;
t_p->printStackTrace();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment