Exception handling

scapix::link::java library provides fully automatic unified exception handling model: any exception, thrown either in C++ or Java will propagate up the stack, if necessary through multiple C++ and Java call frames, and can be caught at any time in either C++ or Java.

Use C++ exception type scapix::link::java::vm_exception to catch Java exceptions in C++:

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

using namespace scapix::link::java;
using namespace scapix::java_api;

void test()
{
    try
    {
        auto file = java::io::File::new_object(string::new_("//file.txt"));
        auto reader = java::io::FileReader::new_object(file);
    }
    catch (const vm_exception& e)
    {
        if (e == "java.io.FileNotFoundException")
        {
            // Because we caught the exception, JVM exception state was cleared,
            // and we can continue to execute any JNI code normally.
            // Unless we decide to rethrow it:

            throw;

            // In which case normal propagation of exception will resume,
            // first in C++, then in Java (if there is a Java call frame up the stack).
        }
    }
}

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

import com.scapix.NativeException;

class NativeExceptionDemo {

    public static void main(String args[]) {

        try {

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

        } catch (NativeException e) {

           System.out.println("Native Exception");

        }
    }
}