throwing Exception having multiple arguments in constructor

hi,

I am trying to implement exception handling for a JAVA + C++ module.

In which C++ exception is caught by a Java module.

The c++ code is as follows,

try

{

///////////===========

} catch (Exception1 &e) {

jclass clazz = jenv->FindClass("test/Exception1Java");

if(clazz)

jenv->ThrowNew(clazz,e.getErrMsg().c_str());

return 0;

} catch (Exception2 &e) {

jclass clazz = jenv->FindClass("test/Exception2");

if(clazz)

jenv->ThrowNew(clazz,e.getErrMsg().c_str());

return 0;

}

This works perfectly fine.

But I want to pass one more argument as int while throwing the java exception.

jenv->ThrowNew(clazz,e.getErrId(),e.getErrMsg().c_str());

As the jenv->ThrowNew function only take two arguments I am unable to send the error id( int ) to the message.

Is there any alternate to this function or some other approach to throw the java exceptions.

[1004 byte] By [jsp_proa] at [2007-11-26 22:46:48]
# 1

> hi,

> I am trying to implement exception handling for a

> JAVA + C++ module.

> In which C++ exception is caught by a Java module.

>

> The c++ code is as follows,

<snip>

>

> But I want to pass one more argument as int while

> throwing the java exception.

>

>

> env->ThrowNew(clazz,e.getErrId(),e.getErrMsg().c_str());

>

> As the jenv->ThrowNew function only take two

> arguments I am unable to send the error id( int ) to

> the message.

>

> Is there any alternate to this function or some other

> approach to throw the java exceptions.

Build the exception using the normal FindClass, GetMethodID approach,. and then throw it.

#include "NativeExceptionTest.h"

JNIEXPORT void JNICALL Java_NativeExceptionTest_test

(JNIEnv *env, jobject obj, jint data) {

if (data == 42) {

jclass excClass = env->FindClass("MyException");

if (!excClass) {

return;

}

jmethodID ctorID = env->GetMethodID(excClass, "<init>", "(ILjava/lang/String;)V");

if (!ctorID) {

return;

}

jstring excMessage = env->NewStringUTF("ILLEGAL VALUE");

jobject excObject = env->NewObject(excClass, ctorID,-1,excMessage);

if (!excObject) {

return;

}

env->Throw((jthrowable)excObject);

}

return;

}

============================================

public class NativeExceptionTest {

static {

System.loadLibrary("nativeException");

}

NativeExceptionTest() {

try {

test(42);

} catch (MyException e) {

System.out.println("Exception: " + e.getCode() + "(" + e.getMessage() +")");

}

}

private native void test(int i) throws MyException;

public static void main(String[] args) {

new NativeExceptionTest();

}

}

class MyException extends Exception {

private int code;

MyException(int code, String message) {

super(message);

this.code = code;

}

int getCode() {

return this.code;

}

}

Niceguy1a at 2007-7-10 12:05:18 > top of Java-index,Java HotSpot Virtual Machine,Specifications...