Ensure we exit with EXIT_FAILURE if the main thread had an uncaught exception.

Change-Id: I511d71d84992b2ec5efc7b2c2a39f7f279df941c
diff --git a/src/oatexec.cc b/src/oatexec.cc
index 0fcbc1e..9632ca2 100644
--- a/src/oatexec.cc
+++ b/src/oatexec.cc
@@ -15,16 +15,14 @@
 
 // Determine whether or not the specified method is public.
 static bool IsMethodPublic(JNIEnv* env, jclass clazz, jmethodID method_id) {
-  ScopedLocalRef<jobject> reflected(env, env->ToReflectedMethod(clazz,
-      method_id, JNI_FALSE));
+  ScopedLocalRef<jobject> reflected(env, env->ToReflectedMethod(clazz, method_id, JNI_FALSE));
   if (reflected.get() == NULL) {
     fprintf(stderr, "Unable to get reflected method\n");
     return false;
   }
   // We now have a Method instance.  We need to call its
   // getModifiers() method.
-  ScopedLocalRef<jclass> method(env,
-      env->FindClass("java/lang/reflect/Method"));
+  ScopedLocalRef<jclass> method(env, env->FindClass("java/lang/reflect/Method"));
   if (method.get() == NULL) {
     fprintf(stderr, "Unable to find class Method\n");
     return false;
@@ -82,7 +80,11 @@
 
   // Invoke main().
   env->CallStaticVoidMethod(klass.get(), method, args.get());
-  return EXIT_SUCCESS;
+
+  // Check whether there was an uncaught exception. We don't log any uncaught exception here;
+  // detaching this thread will do that for us, but it will clear the exception (and invalidate
+  // our JNIEnv), so we need to check here.
+  return env->ExceptionCheck() ? EXIT_FAILURE : EXIT_SUCCESS;
 }
 
 // Parse arguments.  Most of it just gets passed through to the VM.