Catching JNI exception in Objective C
Catching JNI exception in Objective C
- Subject: Catching JNI exception in Objective C
- From: "Ramakrishna Kondapalli" <email@hidden>
- Date: Fri, 28 Nov 2003 18:54:43 +0530
- Thread-topic: Catching JNI exception in Objective C
Hi,
We are building a Cocoa (Objective C) application that interacts with the
server through Java APIs. These APIs are being invoked using JNI.
We are facing problems in handling the exceptions raised by JVM Or Java
method, in Objective C. I am pasting the piece of code below and also
trying to explain the problem.
//Following method is part of the JVM implementation done as part of the
application. This method will be called after each JNI call and will
check for the possible exceptions (either JVM exceptions or Application
specific) thrown during the JNI call.
+ (void) checkForException
{
//Have an dictionary object for sending back the info to exceptions
ATTaskRequest *request = [[ATTaskRequest alloc] init];
//Have a JNI environment pointer
JNIEnv* xenv = [self env];
//Throw exception and have the exception details in jthrowable object
jthrowable xexc = (*xenv)->ExceptionOccurred(xenv);
if ( xexc)
{
//Clear the exception before making subsequent JNI calls
(*xenv)->ExceptionClear(xenv);
//A wrapper object for accessing the java object through Application's
layer (keeping the server interaction isolated from business)
ATExceptionWrapper* testing = [[[ATExceptionWrapper alloc]
initWithJavaObject:xexc] autorelease];
//Send the wrapper object to exception handler
[[request requestDictionary] setObject:testing forKey:@"testKey"];
//Raise exception
NSException *xjvmExc = [[NSException alloc] init];
[xjvmExc initWithName:@"testName" reason:@"Reason" userInfo:request];
[xjvmExc raise];
}
}
The JNI calls are being made from the following file ATAssetWrapper.m
// ATAssetWrapper.m
#import "ATExceptionWrapper.h"
// defined for every wrapper
static jclass m_jclass=nil;
static const char* JAVA_CLASS_NAME="java/lang/Exception";
static const char* SIG_GETMESSAGEID = "()Ljava/lang/String;";
@implementation ATExceptionWrapper
// load the underlying Java-class definition from the JVM
+(void)loadJavaClassDef
{
if ( !m_jclass )
{
m_jclass = [JVM findClass: JAVA_CLASS_NAME];
}
}
// verify that the Java-class definition has been loaded before init is
complete.
- (id)init
{
[ATExceptionWrapper loadJavaClassDef];
return self;
}
// allows wrapper calls to the JVM access the already-loaded Java-class
definition.
- (jclass)javaClass
{
return m_jclass;
}
//////////////////////////////////////////////////////////// /////
//This is the method of this wrapper that will be called by the
business/application layer objects to get exact error details
- (NSString*)getMessage
{
//[self loadJavaClassDef];
NSString* test = [[NSString alloc] init];
NS_DURING
test = [JVM callStringMethod:"toString" withSig:SIG_GETMESSAGEID
onWrapper:self];
L4Debug(([NSString stringWithFormat:@"The error message id is
%@",test]));
NS_HANDLER
L4Debug(([NSString stringWithFormat:@"Got the handler pass as
%@",[localException name]]));
NS_VALUERETURN(nil, NSString*);
NS_ENDHANDLER
return test;
}
@end
In the above method, "callStringMethod" JNI call may also raise an
exception and will in turn call checkForException method from JVM
(implemented).
I need to know how can we propagate the exceptions from one class to
another (at different levels e.g. the exception occured in 10th call and
the exception has to propagate to first call. Assume each call is from a
different .m file). I will also like to know how can we access the object
sent through exception handler through the parent class's exception
handler.
I will also like to know, if we are able to propagate exception from the
first file to it's parent file/function whether the execution will
continue on next line or will it fall into NS_HANDLER of caller. e.g. in
above case, if some function call myFunction from myFile.m results in
JVM/Application exception (checkForException is called), will the control
go to caller's handler????
-------------------------------------------------------
Thanks & Regards,
Ramakrishna,
Artesia CCFM,
Kanbay Software (I) Pvt. Ltd.,
A-1, Technology Park, MIDC,
Talwade, Pune 412 114.
India.
Phone : (91)20 - 760 1000 Ext: 1625
Email : email@hidden
Website : www.kanbay.com
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.