C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

throwing C++ exceptions through C and/or FORTRAN

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
apm
Guest





PostPosted: Tue Feb 24, 2004 7:17 pm    Post subject: throwing C++ exceptions through C and/or FORTRAN Reply with quote



I have come across some C++ code that throws exceptions that assumes
they will bubble up through some C and/or FORTRAN to some C++ at the
top. Is this guaranteed by the C++ std to be ok? I realise that others
have done this in the past and pronounced that it worked for them. I
realise that in practise it may work but does the std assure it? Any
ideas? It seems a bit dodgey to me. Personally I would trap exceptions
including using catch(...) to catch /everything/ and make sure that
the C code got informed of errors/status via a return code.

Regards,

Andrew Marlow

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Francis Glassborow
Guest





PostPosted: Wed Feb 25, 2004 2:43 pm    Post subject: Re: throwing C++ exceptions through C and/or FORTRAN Reply with quote



In message <d1a33011.0402240249.2a8b745c (AT) posting (DOT) google.com>, apm
<apm35 (AT) student (DOT) open.ac.uk> writes
Quote:
I have come across some C++ code that throws exceptions that assumes
they will bubble up through some C and/or FORTRAN to some C++ at the
top. Is this guaranteed by the C++ std to be ok? I realise that others
have done this in the past and pronounced that it worked for them. I
realise that in practise it may work but does the std assure it? Any
ideas? It seems a bit dodgey to me. Personally I would trap exceptions
including using catch(...) to catch /everything/ and make sure that
the C code got informed of errors/status via a return code.

The C++ Standard cannot place any requirements on other languages so the
simple answer is that there is no guarantee that it will work. Whether
it does or not very much depends on the way exceptions are being
implemented by a specific C++ implementation.

--
Francis Glassborow ACCU
Author of 'You Can Do It!' see http://www.spellen.org/youcandoit
For project ideas and contributions: http://www.spellen.org/youcandoit/projects


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Pete Becker
Guest





PostPosted: Wed Feb 25, 2004 3:05 pm    Post subject: Re: throwing C++ exceptions through C and/or FORTRAN Reply with quote



apm wrote:
Quote:

I have come across some C++ code that throws exceptions that assumes
they will bubble up through some C and/or FORTRAN to some C++ at the
top. Is this guaranteed by the C++ std to be ok?

The C++ Standard doesn't have anything to say about how C or FORTRAN
code deals with C++ exceptions.

Quote:
I realise that others
have done this in the past and pronounced that it worked for them. I
realise that in practise it may work but does the std assure it? Any
ideas? It seems a bit dodgey to me.

Good instinct. If your C or FORTRAN implementation doesn't document how
it interacts with C++ exceptions then you shouldn't assume that it will
work correctly.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Jack Klein
Guest





PostPosted: Wed Feb 25, 2004 3:11 pm    Post subject: Re: throwing C++ exceptions through C and/or FORTRAN Reply with quote

On 24 Feb 2004 14:17:57 -0500, [email]apm35 (AT) student (DOT) open.ac.uk[/email] (apm) wrote in
comp.lang.c++.moderated:

Quote:
I have come across some C++ code that throws exceptions that assumes
they will bubble up through some C and/or FORTRAN to some C++ at the
top. Is this guaranteed by the C++ std to be ok? I realise that others

No, it is not.

Quote:
have done this in the past and pronounced that it worked for them. I
realise that in practise it may work but does the std assure it? Any
ideas? It seems a bit dodgey to me. Personally I would trap exceptions
including using catch(...) to catch /everything/ and make sure that
the C code got informed of errors/status via a return code.

Regards,

Andrew Marlow

The C++ standard provides linkage specifications primarily for the
purpose of allowing C++ code to be linked with that of compatible C
compilers, while not making any guarantees or requirements that any
give C++ implementation must be able to interoperate with any C
compiler at all.

The mechanism is designed so that individual compilers can provide
their own particular extensions to other languages and linkages, but
there is absolutely no guarantee or requirement that exceptions will
work across language boundaries.

Such guarantees might be provided by the suppliers of the particular
compilers, but most certainly are not given by the C++ standard.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Richard Smith
Guest





PostPosted: Fri Feb 27, 2004 2:20 am    Post subject: Re: throwing C++ exceptions through C and/or FORTRAN Reply with quote

apm wrote:
Quote:
I have come across some C++ code that throws exceptions that assumes
they will bubble up through some C and/or FORTRAN to some C++ at the
top. Is this guaranteed by the C++ std to be ok?

It is not guaranteed by the C++ Standard, and on many implementations
this will not work.

Quote:
Personally I would trap exceptions
including using catch(...) to catch /everything/ and make sure that
the C code got informed of errors/status via a return code.

This is a good strategy. Another possibility is, if you know what
exceptions you are expecting, to catch them explicitly, store them and
rethrow them later. For example, using something like

template <class Exception> class exception_holder_t {
public:
explicit exception_holder_t( Exception const& ex ) : ex(ex) {}
void operator()() const { throw ex; }
private:
Exception ex;
};

static boost::function< void() > active_exception;

template <class Exception>
void hold_exception( Exception const& ex ) {
active_exception = exception_holder_t<Exception>( ex );
}

extern "C" void fn1() {
try {
// C++ code that might throw
} catch ( exception_type_1 const& ex1 ) {
hold_exception( ex1 );
} catch ( exception_type_2 const& ex2 ) {
hold_exception( ex2 );
}
}

void fn2() {
fn1(); // Call C function, perhaps indirectly through
// some C code.
if (active_exception) active_exception();
}

Such an approach can also be useful for moving exceptions between
threads, such as from the producer thread to the consumer thread in
that pattern.

This idea could be greatly simplified if all of your exceptions are in
a hierarchy you control (i.e. not std::exception) as you can then add
a clone() and a rethrow() function to the hierachy to remove the need
to know exactly what types of exception will be produced.

// alternative base class
class exception {
public:
virtual exception* clone() const = 0; // return new type(*this)
virtual void rethrow() const = 0; // throw *this;
virtual char const* what() const throw() = 0; // return "Fish
soup"
virtual ~exception() {}
};

--
Richard Smith

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Tom Tanner
Guest





PostPosted: Fri Feb 27, 2004 2:50 pm    Post subject: Re: throwing C++ exceptions through C and/or FORTRAN Reply with quote

[email]apm35 (AT) student (DOT) open.ac.uk[/email] (apm) wrote in message news:<d1a33011.0402240249.2a8b745c (AT) posting (DOT) google.com>...
Quote:
I have come across some C++ code that throws exceptions that assumes
they will bubble up through some C and/or FORTRAN to some C++ at the
top. Is this guaranteed by the C++ std to be ok? I realise that others
have done this in the past and pronounced that it worked for them. I
realise that in practise it may work but does the std assure it? Any
ideas? It seems a bit dodgey to me. Personally I would trap exceptions
including using catch(...) to catch /everything/ and make sure that
the C code got informed of errors/status via a return code.

I don't think the standard ensures this. It might work for some
compilers. I looked at this once on an embedded system and came to the
conclusion that if you called C++ code (FuncA) from C (FuncB) which
had been called from C++ (FuncC), you needed to ensure that FuncA
caught all exceptions, transmogrified them into something that could
be passed back to the C code in FuncB, which would then return an
error to FuncC which could then turn the error back into an exception.

At which point we decided it would not be a good idea to support
exceptions till we had done more work. Although we did come up with a
pseudo-exception handling method for C using setjmp and longjump which
simplified some code enormously.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Ken Hagan
Guest





PostPosted: Mon Mar 01, 2004 12:10 am    Post subject: Re: throwing C++ exceptions through C and/or FORTRAN Reply with quote

"apm" <apm35 (AT) student (DOT) open.ac.uk> wrote...
Quote:
I have come across some C++ code that throws exceptions that assumes
they will bubble up through some C and/or FORTRAN to some C++ at the
top. Is this guaranteed by the C++ std to be ok?

No, as already noted, but...

It will "work" with Microsoft compilers because they piggy-back C++
exceptions on the operating system's own exception infrastructure.
I've dangled some quotes around "work" because you'd have to write
non-standard C and Fortran to correctly unwind stack-based objects
in those languages as the C++ exception passed through. People do,
though.

That's a little platform-specific, but worth mentioning because many
have criticised MS for this "piggy-backing" but few have ever pointed
out the benefits of doing it this way, despite the not infrequent
requests for a standard C++ ABI. (Obviously for C and Fortran one
might equally substitute "C++ compiled by another vendor".)

(Yes, I'm aware that said operating system also dispatches asynchronous
exceptions through this mechanism, but if one of those is ever thrown
across your code then the whole process is as good as dead anyway.)



[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.