Catching Java exceptions

The following code sample illustrates how to catch a checked exception:

try
{
   mc.myBadMethod(456);
}
catch (A_B_MyException& ex)
{
   ex.printStackTrace();
}

Since the full set of possible unchecked exceptions (subclasses of java.lang.RuntimeException) cannot be known in advance, you can expect to catch an exception type only if the type’s class name was passed to the JNI compiler using the -class option—see Table 13-1.

The following code sample catches checked and unchecked exceptions, errors, and throwables:

try
{
   String s = java_null;
   jint n = s.length();
}
catch (my_special_RuntimeException& ex1)
{
   // Catches exceptions only if jnicc options 
   // included: -class my.special.RuntimeException
}
catch (java_lang_RuntimeException& ex2)
{
   // Catches all unchecked exceptions
}
catch (java_lang_Exception& ex3)
{
   // Catches all checked and unchecked exceptions
}
catch (java_lang_Error& ex4)
{
   // Catches all errors
}
catch (java_lang_Throwable& ex5)
{
   // Catches all throwables
}