views:

38

answers:

1

I have an Android project that contains a class that uses JNI to pull a value from a C function. The C function was built into a library using NDK. The value returned from the C function is in turn used to initialize a variable inside a class when its first loaded. This works fine. However, I also want it to work when the library is missing by providing a default value. So Im using something like this:

static native String getstring();

static {
        try {
                System.loadLibrary("library");
                NAME = getstring();
        }
        catch (Exception e) {
                NAME = "Default";
        }
}

Despite the catch, Im still getting a UnsatisfiedLinkError when I try to run this code with the library missing. Why am I not catching the exception? What am I doing wrong?

+2  A: 

Hi,

UnsatisfiedLinkError is not a subclass of Exception. The hierarchy of UnsatisfiedLinkError is:

Throwable->Error->UnsatisfiedLinkError

You better catch UnsatisfiedLinkError if you want to handle it.

ognian
Ah, that makes sense. Catching that specific error and setting a default value works perfectly. Thanks!
Eno