Using the Raw Native Interface Previous
Previous
Introduction
Introduction
Next
Next

Exceptions

To throw Java exceptions from native code you can use the SignalError() API. This creates a Java exception object which will be thrown on return from the native code (the normal execution of the native code is not interrupted):

    if (makeJavaString("Hello", 5) == NULL)
    {
        SignalError(NULL, "java/lang/OutOfMemoryError", "Failed to create string.");
        return;
    }

The first parameter is the environment which should always be NULL, the second is the class name of the Java exception you want to throw (this must be a Java Throwable or a subclass of Throwable) and the third is the "detail string" which can be NULL.

If you call into Java using something like execute_java_dynamic_method it's possible that an uncaught exception was thrown during it's execution. To test for this case you can use the exceptionOccured() API which takes NULL as it's only parameter and returns TRUE if an exception was thrown and FALSE otherwise. You can produce a stack trace of the exception (to the System.err PrintStream) using exceptionDescribe() and you can destroy the exception using exceptionClear().

For example:

    // Make sure there isn't already a pending exception.
    exceptionClear(NULL);

    // Call method.
    execute_java_dynamic_method(...);

    // Check if something bad happened.
    if (exceptionOccurred(NULL))
    {
        // Make sure people are aware of the problem.
        exceptionDescribe(NULL);

        // Stop the exception from being propergated.
        exceptionClear(NULL);
    }


© 1996 Microsoft Corporation. All rights reserved.