Basics of C++ Exceptions

F

firehead.satish

Hello All,

I am trying to a mini library for myself to load Java libraries from
JNI & C++.

The problem I am having is that, I have custom java file that I want
to use. Lets say that this custom Java module was removed at runtime.
I then want the compile to display which object had failed.

The source code that I have is so far ok when the custom java byte
files are not found at runtime.
However the source code does not tell which load JNI java class file
had failed.

I want to return a Exception which display the location of the object
instantiation had failed.

Could some help me out on this.

Thanks,


#include <JNI_Object.hxx>
#include <windows>
#include <iostream>

//::MessageBoxEx(0,"Reinvet the Wheel","2",MB_ICONWARNING|
IDOK,MAKELANGID(LANG_ENGLISH,SUBLANG_ENGLISH_AUS));

class Exception{
public:
virtual const char* explain()=0;
};

#ifndef _JAVA_CLASSES_
#define _JAVA_CLASSES_
class Java_Class: public Exception
{
private:
jclass *class_instance;
JNIEnv* pEnv;
bool vmbug;
const char* explain(){return "Class was not found";}

bool state(){ return (vmbug == true?false:true ); }

public:
~Java_Class()
{
delete class_instance;
}

Java_Class(JNIEnv &in_env,const char*
name):pEnv(&in_env),class_instance(0),vmbug(false)
{
jclass cls = in_env.FindClass(name);
if(cls == NULL || cls == 0)vmbug = true;
if(vmbug == true){
throw this;
}
else
class_instance = new jclass(cls);

}


JNIEnv* get_Env(){
return pEnv;
}


jclass &get(){return *class_instance;}
};
#endif

#ifndef _JAVA_OBJECTS_
#define _JAVA_OBJECTS_
class Java_Object: public Exception
{

private:
jmethodID *pmID;
bool bMIDbug;
jmethodID* get_Method(){return pmID;}
jobject *class_object;
JNIEnv* pEnv;
JNIEnv& get_Env(){return *pEnv;}
class Java_Class *pInst;
const char* error;
const char* explain(){return "Object not instantiated";}

void set_Object(Java_Class* inst,const char* name,const char*
signature){
set_Method(name,signature);

if(bMIDbug == false)
if(class_object == 0 ||class_object ==NULL)
class_object = new jobject(pEnv->NewObject(inst-
get(),get_Method_Ref()));
else
*class_object = pEnv->NewObject(inst->get(),get_Method_Ref());


}
bool state(){return (bMIDbug == true?false:true );}
public:
Java_Object(Java_Class* inst):class_object(0),pEnv(inst-
get_Env()),pInst(inst),bMIDbug(false),error("")
{
set_Object(inst,"<init>","()V");
if(state() == false)
{
throw this;
}

}

Java_Object(Java_Class* inst,const char* name,const char*
signature):bMIDbug(false)
{
if(pEnv == NULL )
pEnv = inst->get_Env();

if(pInst==NULL)
pInst = inst;

set_Object(inst,name,signature);
if(state() == false){
throw this;
}
}

~Java_Object(){
if(class_object != 0 ||class_object !=NULL)delete class_object;
}

void set_Method(const char* name,const char* signature)
{
bMIDbug = false;
jmethodID mid=0;
mid = pEnv->GetMethodID(pInst->get(),name,signature);
if(mid == NULL || mid == 0){
bMIDbug = true;
}else{
if(pmID == NULL || pmID == 0){
pmID = new jmethodID(mid);
}
else{
*pmID = mid;
}
}
}

jobject& get(){return *class_object;}

jmethodID& get_Method_Ref(){
return *get_Method();
}

};
#endif

//int WINAPI WinMain(HINSTANCE hInst,HINSTANCE hPrevInst,LPSTR
lpszCmd,int nCmdShow)
int main(int argc,char** argv)
{
class JNI_Object *driver=new JNI_Object;

if(driver->ready() == true){

class Java_Class* instance=0;
class Java_Object *jframe =0;
try
{
instance = new Java_Class(driver->getEnv(),"javax/swing/JFrame");
jframe = new Java_Object(instance);
jfieldID stField = driver->getEnv().GetStaticFieldID(instance-
get(),"DISPOSE_ON_CLOSE","I");

if(stField != NULL && stField != 0) {
class Java_Class* inst_main_window =0;
class Java_Object *main_window = 0;

inst_main_window =new Java_Class(driver->getEnv(),"obj/
Main_Window");
main_window = new Java_Object(inst_main_window);
delete main_window;main_window = 0;
delete inst_main_window;

jframe->set_Method("pack","()V");
driver->getEnv().CallVoidMethod(jframe->get(),jframe-
get_Method_Ref());
jframe->set_Method("setSize","(II)V");
driver->getEnv().CallVoidMethod(jframe->get(),jframe-

jframe->set_Method("setDefaultCloseOperation","(I)V");
(void)driver->getEnv().CallVoidMethod(jframe->get(),jframe-
driver->getEnv().GetStaticIntField(instance->get(),stField));

jframe->set_Method("setVisible","(Z)V");
(void)driver->getEnv().CallVoidMethod(jframe->get(),jframe-
get_Method_Ref(),JNI_TRUE);

}

delete jframe;
delete instance;

if(driver->getEnv().ExceptionOccurred()){
driver->getEnv().ExceptionClear();
driver->getEnv().ExceptionDescribe();
class Java_Class *exception = 0;
exception = new Java_Class(driver->getEnv(),"java/awt/
HeadlessException");
driver->getEnv().ThrowNew(exception->get(), "thrown from C code");
delete exception;
}

}catch(Java_Object &obj){
class Exception *bug = &obj;
std::cout<<bug->explain();
}catch(Java_Class &obj){
class Exception *bug = &obj;
std::cout<<bug->explain();
}catch(...){

std::cout<<"Class not found"<<std::endl;
delete jframe;
delete instance;
delete driver;
return 1;
//delete instance;
}
}//TRUE
delete driver;
return 0;
}
 
F

firehead.satish

Hello All,

I am trying to a mini library for myself to load Java libraries from
JNI & C++.

The problem I am having is that, I have custom java file that I want
to use. Lets say that this custom Java module was removed at runtime.
I then want the compile to display which object had failed.

The source code that I have is so far ok when the custom java byte
files are not found at runtime.
However the source code does not tell which load JNI java class file
had failed.

I want to return a Exception which display the location of the object
instantiation had failed.

Could some help me out on this.

Thanks,

#include <JNI_Object.hxx>
#include <windows>
#include <iostream>

//::MessageBoxEx(0,"Reinvet the Wheel","2",MB_ICONWARNING|
IDOK,MAKELANGID(LANG_ENGLISH,SUBLANG_ENGLISH_AUS));

class Exception{
public:
virtual const char* explain()=0;

};

#ifndef _JAVA_CLASSES_
#define _JAVA_CLASSES_
class Java_Class: public Exception
{
private:
jclass *class_instance;
JNIEnv* pEnv;
bool vmbug;
const char* explain(){return "Class was not found";}

bool state(){ return (vmbug == true?false:true ); }

public:
~Java_Class()
{
delete class_instance;
}

Java_Class(JNIEnv &in_env,const char*
name):pEnv(&in_env),class_instance(0),vmbug(false)
{
jclass cls = in_env.FindClass(name);
if(cls == NULL || cls == 0)vmbug = true;
if(vmbug == true){
throw this;
}
else
class_instance = new jclass(cls);

}

JNIEnv* get_Env(){
return pEnv;
}

jclass &get(){return *class_instance;}};

#endif

#ifndef _JAVA_OBJECTS_
#define _JAVA_OBJECTS_
class Java_Object: public Exception
{

private:
jmethodID *pmID;
bool bMIDbug;
jmethodID* get_Method(){return pmID;}
jobject *class_object;
JNIEnv* pEnv;
JNIEnv& get_Env(){return *pEnv;}
class Java_Class *pInst;
const char* error;
const char* explain(){return "Object not instantiated";}

void set_Object(Java_Class* inst,const char* name,const char*
signature){
set_Method(name,signature);

if(bMIDbug == false)
if(class_object == 0 ||class_object ==NULL)
class_object = new jobject(pEnv->NewObject(inst->get(),get_Method_Ref()));

else
*class_object = pEnv->NewObject(inst->get(),get_Method_Ref());

}
bool state(){return (bMIDbug == true?false:true );}
public:
Java_Object(Java_Class* inst):class_object(0),pEnv(inst->get_Env()),pInst(inst),bMIDbug(false),error("")

{
set_Object(inst,"<init>","()V");
if(state() == false)
{
throw this;
}

}

Java_Object(Java_Class* inst,const char* name,const char*
signature):bMIDbug(false)
{
if(pEnv == NULL )
pEnv = inst->get_Env();

if(pInst==NULL)
pInst = inst;

set_Object(inst,name,signature);
if(state() == false){
throw this;
}
}

~Java_Object(){
if(class_object != 0 ||class_object !=NULL)delete class_object;
}

void set_Method(const char* name,const char* signature)
{
bMIDbug = false;
jmethodID mid=0;
mid = pEnv->GetMethodID(pInst->get(),name,signature);
if(mid == NULL || mid == 0){
bMIDbug = true;
}else{
if(pmID == NULL || pmID == 0){
pmID = new jmethodID(mid);
}
else{
*pmID = mid;
}
}
}

jobject& get(){return *class_object;}

jmethodID& get_Method_Ref(){
return *get_Method();
}

};

#endif

//int WINAPI WinMain(HINSTANCE hInst,HINSTANCE hPrevInst,LPSTR
lpszCmd,int nCmdShow)
int main(int argc,char** argv)
{
class JNI_Object *driver=new JNI_Object;

if(driver->ready() == true){

class Java_Class* instance=0;
class Java_Object *jframe =0;
try
{
instance = new Java_Class(driver->getEnv(),"javax/swing/JFrame");
jframe = new Java_Object(instance);
jfieldID stField = driver->getEnv().GetStaticFieldID(instance-


if(stField != NULL && stField != 0) {
class Java_Class* inst_main_window =0;
class Java_Object *main_window = 0;

inst_main_window =new Java_Class(driver->getEnv(),"obj/
Main_Window");
main_window = new Java_Object(inst_main_window);
delete main_window;main_window = 0;
delete inst_main_window;

jframe->set_Method("pack","()V");
driver->getEnv().CallVoidMethod(jframe->get(),jframe-


}

delete jframe;
delete instance;

if(driver->getEnv().ExceptionOccurred()){
driver->getEnv().ExceptionClear();
driver->getEnv().ExceptionDescribe();
class Java_Class *exception = 0;
exception = new Java_Class(driver->getEnv(),"java/awt/
HeadlessException");
driver->getEnv().ThrowNew(exception->get(), "thrown from C code");
delete exception;
}

}catch(Java_Object &obj){
class Exception *bug = &obj;
std::cout<<bug->explain();
}catch(Java_Class &obj){
class Exception *bug = &obj;
std::cout<<bug->explain();
}catch(...){

std::cout<<"Class not found"<<std::endl;
delete jframe;
delete instance;
delete driver;
return 1;
//delete instance;
}
}//TRUE
delete driver;
return 0;

}

Never mind, I have a found an alternative for the code.

Those who tried attempting to help out.. thanks.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top