Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

extern "C" and exception handling....

0 views
Skip to first unread message

vlad

unread,
Aug 20, 2002, 9:56:29 AM8/20/02
to
Hi,
cl compiler (VC7) does not correct compile follows:

extern "C"
{
void foo()
{
throw 1;
}
}

int main()
{
try
{
foo();
}
catch( int exc_ )
{
std::cout<<"int exception within code = "<<exc_;
}
catch( ... )
{
std::cout<<"undefined exception";

}

}

when the program is excecuting enexpected() function call is occured
here. It is because cl7 sets nothrow attribute to this function, it is
equivalent to throw() specification. From other hand, g++ correct
compile this code and prints: int exception within code = 1. What
compiler is right?
Thanks,
Vlad.

Ioannis Vranos

unread,
Aug 20, 2002, 10:13:43 AM8/20/02
to
"vlad" <v_la...@yahoo.com> wrote in message
news:d0c2631f.02082...@posting.google.com...


That extern "C" function means that it can be used by C code. C
doesn't have exceptions so it is very bad practice to throw one there.

VC++ 7 assumes this when the switch:

/EHc extern "C" defaults to nothrow


is used. Remove that switch.


--
Ioannis

* Ioannis Vranos
* Programming pages: http://www.noicys.cjb.net
* Alternative URL: http://run.to/noicys

Neil Butterworth

unread,
Aug 20, 2002, 10:31:17 AM8/20/02
to

"vlad" <v_la...@yahoo.com> wrote in message
news:d0c2631f.02082...@posting.google.com...

This is off-topic here, but VC7 turns off exception handling for extern "C"
functions by default. To turn it back on, specify the /EHs compiler flag
rather than /EHsc. Your code then works.

Please ask any further VC7-specific questions in the MS support newsgroups
on the msnews.microsoft.com news server.

NeilB


Alexander Terekhov

unread,
Aug 20, 2002, 1:00:30 PM8/20/02
to

Ioannis Vranos wrote:
[...]

> That extern "C" function means that it can be used by C code. C
> doesn't have exceptions so it is very bad practice to throw one there.

Yeah, obviously [and "to begin with"], you've never heard
of things [they are "off-topic" here in this newsgroup, of
course] along the lines of "#include <pthread_exception.h>".

http://www.testdrive.compaq.com
http://tru64unix.compaq.com/docs/base_doc/DOCUMENTATION/V51A_PDF/ARH9RBTE.PDF

----

spe207.testdrive.compaq.com> what /shlib/libpthread.so|grep DECthreads
DECthreads version V3.18-138 May 12 2002
spe207.testdrive.compaq.com> cc -pthread -o seh seh.c
spe207.testdrive.compaq.com> ./seh

Go...

Hi There! Greetings from the ``yellow zone.''

Exception: Attempted stack overflow was detected (dce / thd)

Indeed. ;-)

spe207.testdrive.compaq.com> cat seh.c

#include <stdio.h>
#include <memory.h>
#include <pthread.h>
#include <pthread_exception.h>

void operation()
{
char buffer[128];
memset( buffer, 0, sizeof( buffer ) );
operation();
}

void* my_thread( void* p )
{
TRY {
printf( "\nGo...\n\n" );
operation();
}
CATCH_ALL {
printf( "Hi There! Greetings from the ``yellow zone.''\n\n" );
pthread_exc_report_np( THIS_CATCH );
TRY {
RERAISE;
}
CATCH( pthread_stackovf_e ) {
printf( "\nIndeed. ;-)\n\n" );
} ENDTRY
}
ENDTRY
return NULL;
}

int main()
{
pthread_t tid;
pthread_create( &tid, NULL, &my_thread, NULL );
pthread_exit( NULL );
}

spe207.testdrive.compaq.com> cc -pthread -c -o seh2c.o seh2c.c
spe207.testdrive.compaq.com> cxx -pthread -o seh2 seh2cpp.cpp seh2c.o
spe207.testdrive.compaq.com> ./seh2

C++ try...

Go...

Hi There! Greetings from the ``yellow zone.''

Exception: Attempted stack overflow was detected (dce / thd)

Indeed. ;-) Ha! >>RERAISE<<

C++ catch(...)

Finished!

spe207.testdrive.compaq.com> cat seh2c.c

#include <stdio.h>
#include <memory.h>
#include <pthread.h>
#include <pthread_exception.h>

void operation()
{
char buffer[128];
memset( buffer, 0, sizeof( buffer ) );
operation();
}

void go()
{
TRY {
printf( "\nGo...\n\n" );
operation();
}
CATCH_ALL {
printf( "Hi There! Greetings from the ``yellow zone.''\n\n" );
pthread_exc_report_np( THIS_CATCH );
TRY {
RERAISE;
}
CATCH( pthread_stackovf_e ) {
printf( "\nIndeed. ;-) Ha! >>RERAISE<<\n\n" );
RERAISE;
} ENDTRY
} ENDTRY
}


spe207.testdrive.compaq.com> cat seh2cpp.cpp

#include <pthread.h>

#include <iostream>
using namespace std;

extern "C" void go();

extern "C" void* my_thread( void* p )
{
cout << "\nC++ try..." << endl;
try {
go();
}
catch(...) {
cout << "C++ catch(...)" << endl;
}
cout << "\nFinished!\n" << endl;
return 0;
}

int main()
{
pthread_t tid;
pthread_create( &tid, 0, &my_thread, 0 );
pthread_exit( 0 );
}

spe207.testdrive.compaq.com>

----

regards,
alexander.

vlad

unread,
Aug 21, 2002, 9:17:52 AM8/21/02
to
> This is off-topic here, but VC7 turns off exception handling for extern "C"
> functions by default. To turn it back on, specify the /EHs compiler flag
> rather than /EHsc. Your code then works.
>
> Please ask any further VC7-specific questions in the MS support newsgroups
> on the msnews.microsoft.com news server.
>
> NeilB

My main question was: what does ISO Standard say concerning this
subject. I looked through the Standard and didn't find any ties
between linkage and exception handling for functions. I now also that
it is a bad practice to throw C++ exceptions from functions specified
with C linkage. But, can I do so or no? The related question is:

extern "C"
{
void foo( int i = 90 ) {}
}

int main()
{
foo();
}

What does a compiler says here? Can I define parameter value by
default for functions that have C linkage?
Thanx,
vlad.

0 new messages