How to check in JNI if java class exists? - c++

If im using this:
if(!env->FindClass("com/test/app")){
Log("Not found");
return 0;
}
I got error: no such class

As mentioned in the documentation, FindClass can result in a variety of (Java) exceptions being thrown.
It is an error to make any further JNI calls with a pending Java exception. So you need to, at a minimum, clear the exception using env->ExceptionClear().

Related

c++ try/catch ignorance in iOS

In our app we have a c++ static library and I use Objective-C++ to work with it.
That c++ library utilizes rapidjson to parse XML data:
try {
rapidjson::Document document;
document.Parse(connection.data.description);
connection.openTime = document["openFrom"].GetInt();
connection.closeTime = document["openTo"].GetInt();
return true;
} catch (std::exception e) {
connection.openTime = 0;
connection.closeTime = 0;
return false;
}
The problem is that if document["openFrom"] cannot be converted into Int via GetInt() method, exception is not raised. Instead of that my app crashes with SIGABRT.
Assertion failed: (data_.f.flags & kIntFlag), function GetInt, file /Users/xxx/xxx/xx/ios/../src/rapidjson/document.h, line 1645.
On Android OS, btw, in the same case exception is raised successfully.
What could be the problem? I guess the issue is in Xcode's Swift compiler behavior.
As it clearly stated in the log you provided – it is not a crash, it is only a failed assert which internally calls abort() that results in SIGABRT which stands for 'signal abort'. Asserts are disabled in release mode so it should work fine there. Or you can disable asserts in rapidjson (by defining macro RAPIDJSON_ASSERT).

Retrieving error message from HRESULT in C++ after calling a C# COM object

I have a COM object written in C#. Under some circumstances, the COM object may throw an Exception with a descriptive message if it encounters an error (for example throw new Exception("error message")).
This COM object is called from VB6 code and from C++ code.
From the VB6 code, I can retrieve the error message using Err.Message.
In C++, I get an HRESULT 0x80131500 as specified in the System.Exception documentation.
Once I have this HRESULT in C++, how can I get the error message of the Exception (Exception.Message) like the message returned by Err.Message in VB6?
I searched the web and found a few examples using FormatMessage and _com_error, but none of these return the message I want.
Updated.
Try to QueryInterface the failing object for the ISupportErrorInfo interface, then call the InterfaceSupportsErrorInfo method with the REFIID of the interface throwing the exception. If it returns S_OK, then just calls the GetErrorInfo function to get an IErrorInfo interface. Then use the GetDescription method.

Nodejs C++ event emitter. add-on error. Non-function in MakeCallback. method = emit Abort trap: 6

I'm creating a C++ level event emitter addon for node.js. I'm getting this C++ error when including the add-on in a node.js project.
Non-function in MakeCallback. method = emit Abort trap: 6
I found this Gist with a simplified example of the same behavior:
https://gist.github.com/jedi4ever/4250746
Hoping for some general insight into why this might be caused or what this error means.
If I understand what you're trying to do correctly, you have a couple of problems in your keylogger.js file;
util.inherits(new keylogger, events.EventEmitter);
exports = keylogger;
You're trying to extend an instance, and your export statement is a bit off. This should work better to export keylogger as an instance which the test file seems to expect;
util.inherits(keylogger, events.EventEmitter);
exports.keylogger = new keylogger();

Xerces: How to check the validity of an XML file using ErrorHandler

I am trying to determine if a given XML file is valid (has proper syntax and structure), and I am using Xerces. I have been able to succesfully read proper files but when I give it files with incorrect syntax, no errors are thrown.
I have been fishing around and found out that I might have to use an Error handler and user setErrorHandler to catch the errors instead of the traditional try-throw-catch exception handling.
The problem that I am having though is that I am very confused how to declare the proper handler, set it to my parser and then read the errors if there are any that show up.
Is there any chance somebody could shed some light on my situation?
// #input_parameter from function: const string & xmlConfigArg
xercesc::DOMDocument* doc = NULL;
string xmlConfig(xmlConfigArg);
Handler handler; // I'm not sure what type of handler to use
_parser->setErrorHandler(&handler);
try{
_parser->parse(xmlConfigArg.c_str());
doc = _parser-> getDocument();
}catch(...){
//Nothing is ever caught here
}
You need to derive a class from ErrorHandler (< xercesc/sax/ErrorHandler.hpp >)
then overwrite all the virtual methods there.
After doing so, You can get the error code from the class you created. No exceptions will be thrown in the parsing, so you can wave the try/cache block (or keep it for a different use).

SWIG: Reporting Python exceptions from C++ code

I am using a library, which specifies in its API docs to define a class inherited from some particular class of of the library. The library itself is written in C++ and the bindings to Python is generated using SWIG. The problem is, when I run my Python code, no matter what exception Python throws, I get the error saying "terminate called after throwing an instance of 'Swig::DirectorMethodException'".
I would like to have this exception raised by the Python code to be reported while executing my program. Esp, those cases where I get ZeroDivisionError.
I tried to hack a bit by following the method described in the SWIG documentation at http://www.swig.org/Doc2.0/Python.html#Python_nn36 but with no luck. I still get the same message "terminate called after throwing an instance of 'Swig::DirectorMethodException'" no matter what I put in the module.i file.
Can some one please give me pointers on how to go about with this problem, so that Python exceptions are reported as they are?
Report exception raised by Python in the console of the program.
This is the useful fix from Madhusudan.C.S.
See his comment on ginbot's answer.
I am putting it as an answer so that it becomes more visible.
/* MyInterface.i */
%module(directors="1") MyInterface
%feature("director:except") {
if( $error != NULL ) {
PyObject *ptype, *pvalue, *ptraceback;
PyErr_Fetch( &ptype, &pvalue, &ptraceback );
PyErr_Restore( ptype, pvalue, ptraceback );
PyErr_Print();
Py_Exit(1);
}
}
I don't know how far along you are with your code base, so this may be of little use, but I had better luck with boost::python than SWIG. Then you could do this: boost::python Export Custom Exception