473,386 Members | 1,654 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Catching exceptions across shared object boundaries

This is driving me nuts. I can't catch an exception thrown out of a
..so library. It passes up and terminates the program instead. Here is
the code for the .so and the executabe, the compiler commands, and the
output. Please help, I ahve spent hours and hours on this!

====FILE libprint.c ==============================

#include <stdio.h>
#include <stdexcept>
using namespace std;

int hello() throw (runtime_error)
{
printf("hello world!\n");
throw runtime_error("kjh");
}

===FILE main.c ==================

#include <stdio.h>
#include <string>
#include <stdexcept>
using namespace std;

extern int hello() throw (runtime_error);

int main() {
try {
hello();
}
catch (...) {
printf("caught!\n");
}
}

===== COMMAND LINE ======

pinch.203$ g++ -fPIC -fexceptions -c libprint.c
pinch.204$ ld -G libprint.o -fexceptions -o libprint.so
pinch.205$ g++ main.c -fexceptions -lprint
pinch.206$ ./a.out
hello world!
terminate called after throwing an instance of 'std::runtime_error'
what(): kjh
Abort
(gcc version is 3.4.3)

Aug 29 '06 #1
4 3210
foxx wrote:
This is driving me nuts. I can't catch an exception thrown out of a
.so library. [..]
Just to let you know, .so and catching exceptions across them are not
defined by C++, so you might consider asking in the newsgroup for your
OS as well; folks there might be more accustomed to those things, and
have more experience (per newsreader capita) with shared objects.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Aug 29 '06 #2
foxx wrote:
This is driving me nuts. I can't catch an exception thrown out of a
.so library. It passes up and terminates the program instead. Here is
the code for the .so and the executabe, the compiler commands, and the
output. Please help, I ahve spent hours and hours on this!
The following does work:

g++ -fPIC -shared libprint.cc -o libprint.so

I'm not an expert, but I guess you're simply not creating a shared
library (whatever that means technically). -shared is also understood by ld.

Jens
Aug 29 '06 #3

foxx wrote:
This is driving me nuts. I can't catch an exception thrown out of a
.so library. It passes up and terminates the program instead. Here is
the code for the .so and the executabe, the compiler commands, and the
output. Please help, I ahve spent hours and hours on this!
etc:

1. I'd use a makefile and build the library with -shared
2. Get rid of throw specifications.

Aug 30 '06 #4
Thanks guys -- for for reference, I got it working with syntax like
this:
(I'm using cygwin on windows, but compiling without cygwin
dependencies)

---------------Makefile:

libException.dll: Exception.cc
g++ -mno-cygwin -fPIC -shared -o libException.dll Exception.cc

libtest1.dll: test1.cc libException.dll
g++ -mno-cygwin -fPIC -shared -o libException.dll test1.cc

libmymain.dll: mymain.cc libtest1.dll libException.dll
g++ -mno-cygwin -fPIC -shared -o libmymain.dll mymain.cc libtest1.dll

main.exe: libmymain.dll
g++ -mno-cygwin -o main.exe main.cc -L. -ltest1 -lmymain

Aug 31 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Kenny Cutter | last post by:
Hi group, I am quite new to exceptions in .NET and I am a bit confused of how to use the inner exceptions. Could anyone explain? Let's say I have a function that takes a double (X) that is not...
8
by: Adam H. Peterson | last post by:
Hello, I sometimes find myself writing code something like this: try { Derived &d=dynamic_cast<Derived&>(b); d.do_something_complicated(); // etc.... } catch (std::bad_cast) { throw...
12
by: Vasco Lohrenscheit | last post by:
Hi, I have a Problem with unmanaged exception. In the debug build it works fine to catch unmanaged c++ exceptions from other dlls with //managed code: try { //the form loads unmanaged dlls...
0
by: Colmeister | last post by:
I recently read Jason Clark's excellent article on Unhandled Exceptions (http://msdn.microsoft.com/msdnmag/issues/04/06/NET/default.aspx) and have attempted to incorporate the features he talks...
5
by: Simon Tamman | last post by:
I have an object named DisasterRecovery. The Ctor of this object is this: private DisasterRecovery() { Application.ThreadException+= new...
4
by: rwf_20 | last post by:
I'm seeing incorrect (I think) behavior in the event of an exception thrown in a native C++ library. Basically, the stack appears to unwind correctly if the caller is native, but incorrectly if...
22
by: ben mitch | last post by:
Hi I hope you'll see this cross-post (c/c++) as appropriate. I also admit immediately that the question *may* turn out to be compiler-/os-specific, in which case I apologize. But I wonder if...
5
by: Tom P. | last post by:
I have a situation that requires me to catch two different types of exceptions but do the same processing. Is there a way to do this? In VB you can stack the Catch statements but what do I do in...
45
by: =?Utf-8?B?QmV0aA==?= | last post by:
Hello. I'm trying to find another way to share an instance of an object with other classes. I started by passing the instance to the other class's constructor, like this: Friend Class...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.