Exception handling

Exception tunneling

Any exception, thrown in C++ or Java, will propagate up the stack, if necessary through multiple C++ and Java call frames.

You can catch and rethrow such exception at any time in either C++ or Java.

Use jni::vm_exception to catch Java exceptions in C++:

#include <scapix/java_api/java/io/File.h>
#include <scapix/java_api/java/io/FileReader.h>
#include <scapix/java_api/java/io/FileNotFoundException.h>

namespace jni = scapix::jni;
using namespace scapix::java_api;

void test()
{
    try
    {
        auto file = jni::new_object<java::io::File>("/file.txt");
        auto reader = jni::new_object<java::io::FileReader>(file);
    }
    catch (const jni::vm_exception& e)
    {
        if (auto file_not_found = e.instance_of<java::io::FileNotFoundException>())
        {
            // handle specifically java::io::FileNotFoundException
        }
        else if (auto io = e.instance_of<java::io::IOException>())
        {
            // handle any java::io::IOException
        }
    }
}

Use com.scapix.NativeException to catch C++ exceptions in Java:

class NativeExceptionDemo {

    public static void main(String args[]) {

        try {

            SomeType a = new SomeType();
            a.someNativeMethod();

        } catch (com.scapix.NativeException e) {

           System.out.println(e);

        }
    }
}