This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

RE: try-catch exception


Hello-gcc,

I have a C++ library linked to C executable. C++ library is unable to
catch the exception in the try/catch block. I've also tried
"-fexceptions"  compiler switch and yet the program core-dumps with
SIGABRT signal. Any reason why exception handler is being invoked ?

"terminate called after throwing an instance of 'int'
Aborted (core dumped)"

I have a sample code snippet below.

$cat TryCatch.h
#ifndef __TRYCATCH_H__
#define __TRYCATCH_H__

#ifdef __cplusplus
extern "C" {
#endif

int main1(void);
#ifdef __cplusplus
}
#endif
#endif // __TRYCATCH_H__


$cat main.c
#include <stdio.h>
#include "TryCatch.h"

int main () {
  main1();
}

$cat TryCatch.cpp
#include <iostream>
#include "TryCatch.h"
using namespace std;

#ifdef __cplusplus
extern "C" {
#endif

int main1 () {
  try
  {
    throw 20;
  }
  catch (const exception &e)
  {
    cout << "An exception occurred. Exception Nr. " << e.what() << endl;
  }
  return 0;
}

#ifdef __cplusplus
}
#endif

Compiler Flags:

$g++ -g -O0 main.c -o main.o -c -fexceptions
$g++ -g -O0 TryCatch.cpp -o TryCatch.o -c -fexceptions
$g++ main.o TryCatch.o -o TryCatch -lstdc++
gcc -v
Using built-in specs.
Target: x86_64-redhat-linux
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man
--infodir=/usr/share/info --enable-shared --enable-threads=posix
--enable-checking=release --with-system-zlib --enable-__cxa_atexit
--disable-libunwind-exceptions --enable-libgcj-multifile
--enable-languages=c,c++,objc,obj-c++,java,fortran,ada
--enable-java-awt=gtk --disable-dssi --disable-plugin
--with-java-home=/usr/lib/jvm/java-1.4.2-gcj-1.4.2.0/jre
--with-cpu=generic --host=x86_64-redhat-linux
Thread model: posix
gcc version 4.1.2 20080704 (Red Hat 4.1.2-54)


Sample run in gdb:

GNU gdb (GDB) Red Hat Enterprise Linux (7.0.1-45.el5)
Copyright (C) 2009 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-redhat-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from TryCatch/TryCatch...done.
(gdb) r
Starting program: /users/vinag/TryCatch/TryCatch
warning: no loadable sections found in added symbol-file
system-supplied DSO at 0x2aaaaaaab000
terminate called after throwing an instance of 'int'

Program received signal SIGABRT, Aborted.
0x0000003e484302c5 in raise () from /lib64/libc.so.6
(gdb) bt
#0  0x0000003e484302c5 in raise () from /lib64/libc.so.6
#1  0x0000003e48431d70 in abort () from /lib64/libc.so.6
#2  0x0000003e51ebed94 in __gnu_cxx::__verbose_terminate_handler() ()
from /usr/lib64/libstdc++.so.6
#3  0x0000003e51ebce46 in ?? () from /usr/lib64/libstdc++.so.6
#4  0x0000003e51ebce73 in std::terminate() () from /usr/lib64/libstdc++.so.6
#5  0x0000003e51ebcf71 in __cxa_throw () from /usr/lib64/libstdc++.so.6
#6  0x0000000000400ad2 in main1 () at TryCatch.cpp:12
#7  0x0000000000400a31 in main () at main.c:5
(gdb)


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]