Failing to catch exceptions thrown by C++.
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Ksenia_Golod
Posted On:   Tuesday, March 25, 2003 09:10 AM

Hello, I'm using JNI for a simple communication between Java and C++ (there are basically a few methods with bool return value). I have created a shared library and the needed wrappers for every method, and under normal conditions it works great. However, if C++ throws an exception and tries to catch it, this fails and the program aborts. I've done some reading on the web, and someone mentioned that with gcc, shared libraries are unable to catch their own exceptions. Therefore, I've decided to use a static library for the C++ code. But (as far as I've understood) Java can load only a shared library, so my solution was to create proxy for the C++ method calls in a shared library, and then link it to my static library.    More>>

Hello,



I'm using JNI for a simple communication between Java and C++ (there are basically a few methods with bool return value). I have created a shared library and the needed wrappers for every method, and under normal conditions it works great.



However, if C++ throws an exception and tries to catch it, this fails and the program aborts.



I've done some reading on the web, and someone mentioned that with gcc, shared libraries are unable to catch their own exceptions.



Therefore, I've decided to use a static library for the C++ code. But (as far as I've understood) Java can load only a shared library, so my solution was to create proxy for the C++ method calls in a shared library, and then link it to my static library.



But after all this effort, a still see the same behaviour - any exception that is thrown by C++ code is failing to be caught, and the program aborts.



Another thing to try would be to catch the exceptions on the Java side, but unfortunately in my situation I cannot do that.



Can someone please suggest an alternative? Or perhaps I'm missing something?



I will greatly appreciate any feedback!



Thanks a lot,


Ksenia    <<Less

Re: Failing to catch exceptions thrown by C++.

Posted By:   Christopher_Schultz  
Posted On:   Friday, April 4, 2003 12:29 PM

Something must be catching that exception. There must be a defaul exception handler that "wraps" each of your library function calls that is doing this.



I'm pretty sure that libraries don't work well throwing exceptions across the library boundary. You should have all your exported calls catch any exceptions and return them "across the boundary" as standard return values or via some other mechanism.



This is exactly what I've done with my JNI code. In fact, in most cases, I turn around and throw a Java exception back to the VM.



Good luck,

-chris
About | Sitemap | Contact