Throwing a Java exception from C

In Java code, the first step in throwing an exception is to create a Java exception object. This is also the first step in C code. To create a Java object, you:
  1. Get a data structure that represents the desired Java class (line 36)
  2. Get an offset from that class that represents the desired constructor (line 37)
  3. Create the object by calling the constructor on the class (line 38)
The class that we want is an "inner class" (line 9), so it is necessary to provide a "fully qualified path name" (line 36).

We have not previously seen the syntax for specifying the signature of a Java method (line 37). A method with the signature

   void doThis( String s, int i, MyPackage.MyClass m )
Would be encoded as
   "doThis", "(Ljava/lang/String;ILMyPackage/MyClass;)V"
The method name (represented as a string literal) is the second argument to the JNI function GetMethodId() (the name for any constructor is always "<init>"). The return type and argument types are all squashed together in a string literal and passed as the third argument. The argument types appear inside of parentheses, and the return type is concatenated at the end. The actual type codes were listed in the table of the previous example.

Note that the semicolon at the end of the 'L' type is the terminator of that parameter type, not a separator between parameter types. This is why there is no semicolon between the int code and the subsequent class code. Also note that in this encoding scheme, you must use '/' instead of '.' to separate the package and class names.

Once you have a Java exception object created, you then use the JNI function Throw() (line 39), and, a return statement (line 40).

[source: Horstmann98, pp609-614]