MCPcopy Create free account
hub / github.com/android/ndk-samples / GetExceptionSummary

Function GetExceptionSummary

exceptions/app/src/main/cpp/exception_helper.cpp:56–102  ·  view source on GitHub ↗

* Returns a human-readable summary of an exception object. The buffer will * be populated with the "binary" class name and, if present, the * exception message. */

Source from the content-addressed store, hash-verified

54 * exception message.
55 */
56static bool GetExceptionSummary(JNIEnv* env, jthrowable thrown,
57 std::string* dst) {
58 // Summary is <exception_class_name> ": " <exception_message>
59 jclass exceptionClass = env->GetObjectClass(thrown); // Always succeeds
60 jmethodID getName =
61 FindMethod(env, "java/lang/Class", "getName", "()Ljava/lang/String;");
62 jstring className = (jstring)env->CallObjectMethod(exceptionClass, getName);
63 if (className == NULL) {
64 *dst = "<error getting class name>";
65 env->ExceptionClear();
66 env->DeleteLocalRef(exceptionClass);
67 return false;
68 }
69 env->DeleteLocalRef(exceptionClass);
70 exceptionClass = NULL;
71 if (!AppendJString(env, className, dst)) {
72 *dst = "<error getting class name UTF-8>";
73 env->ExceptionClear();
74 env->DeleteLocalRef(className);
75 return false;
76 }
77 env->DeleteLocalRef(className);
78 className = NULL;
79 jmethodID getMessage = FindMethod(env, "java/lang/Throwable", "getMessage",
80 "()Ljava/lang/String;");
81 jstring message = (jstring)env->CallObjectMethod(thrown, getMessage);
82 if (message == NULL) {
83 return true;
84 }
85 dst->append(": ");
86 bool success = AppendJString(env, message, dst);
87 if (!success) {
88 // Two potential reasons for reaching here:
89 //
90 // 1. managed heap allocation failure (OOME).
91 // 2. native heap allocation failure for the storage in |dst|.
92 //
93 // Attempt to append failure notification, okay to fail, |dst| contains the
94 // class name of |thrown|.
95 dst->append("<error getting message>");
96 // Clear OOME if present.
97 env->ExceptionClear();
98 }
99 env->DeleteLocalRef(message);
100 message = NULL;
101 return success;
102}
103
104static jstring CreateExceptionMsg(JNIEnv* env, const char* msg) {
105 jstring detailMessage = env->NewStringUTF(msg);

Callers 1

DiscardPendingExceptionFunction · 0.85

Calls 3

FindMethodFunction · 0.85
AppendJStringFunction · 0.85
CallObjectMethodMethod · 0.80

Tested by

no test coverage detected