我们只是为要抛出的每种异常类型编写实用程序方法。这里有些例子:
jint throwNoClassDefError( JNIEnv *env, char *message ){ jclass exClass; char *className = "java/lang/NoClassDefFoundError"; exClass = (*env)->FindClass( env, className); if (exClass == NULL) { return throwNoClassDefError( env, className ); } return (*env)->ThrowNew( env, exClass, message );}jint throwNoSuchMethodError( JNIEnv *env, char *className, char *methodName, char *signature ){ jclass exClass; char *exClassName = "java/lang/NoSuchMethodError" ; LPTSTR msgBuf; jint retCode; size_t nMallocSize; exClass = (*env)->FindClass( env, exClassName ); if ( exClass == NULL ) { return throwNoClassDefError( env, exClassName ); } nMallocSize = strlen(className) + strlen(methodName) + strlen(signature) + 8; msgBuf = malloc( nMallocSize ); if ( msgBuf == NULL ) { return throwOutOfMemoryError ( env, "throwNoSuchMethodError: allocating msgBuf" ); } memset( msgBuf, 0, nMallocSize ); strcpy( msgBuf, className ); strcat( msgBuf, "." ); strcat( msgBuf, methodName ); strcat( msgBuf, "." ); strcat( msgBuf, signature ); retCode = (*env)->ThrowNew( env, exClass, msgBuf ); free ( msgBuf ); return retCode;}jint throwNoSuchFieldError( JNIEnv *env, char *message ){ jclass exClass; char *className = "java/lang/NoSuchFieldError" ; exClass = (*env)->FindClass( env, className ); if ( exClass == NULL ) { return throwNoClassDefError( env, className ); } return (*env)->ThrowNew( env, exClass, message );}jint throwOutOfMemoryError( JNIEnv *env, char *message ){ jclass exClass; char *className = "java/lang/OutOfMemoryError" ; exClass = (*env)->FindClass( env, className ); if ( exClass == NULL ) { return throwNoClassDefError( env, className ); } return (*env)->ThrowNew( env, exClass, message );}
这样,很容易找到它们,您的代码完成编辑器将帮助您键入它们,并且您可以传递简单的参数。
我确信您可以扩展它来处理链式异常或其他更复杂的方法。这足以满足我们的需求。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)