5

Possible Duplicate:
Best way to throw exceptions in JNI code?

I see that the System#arraycopy could throw ArrayStoreException or IndexOutOfBoundsException etc.
But I also saw that System#arraycopy is a using a native method, which means C code, right?
So how is it possible that C code could throw any of these java exceptions?

Community
  • 1
  • 1
Jim
  • 18,826
  • 34
  • 135
  • 254

1 Answers1

3

There are no problems in throwing java exception from native code. You can do this easily with code like this one:

jclass cls = env->FindClass("java/lang/ArrayStoreException");
env->ThrowNew(cls, message);
amukhachov
  • 5,822
  • 1
  • 41
  • 60
  • Don't forget to clean up and return afterwards... It is a common newbe mistake to assume that an ThrowNew() will immediately return, just like a throw would normally do. – Fredrik Oct 02 '12 at 08:26
  • The `` interface also provides a `jint (*Throw)(JNIEnv*, jthrowable);` alternative API that is useful when you need to create your own/specific throwable object via `NewObject`. – deltheil Oct 02 '12 at 09:11