This is the mail archive of the gcc-bugs@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]

[Bug c++/24187] New: An exception thrown through a C function is not caught


An exception thrown from a C++ function which was called by an external C
function is not caught.

How to reproduce:
Consider the following files:
--- main.cc ---
#include <string>
#include <iostream>
using namespace std;

class error_class {
  public:
    error_class(const string &msg) : message(msg) {}
    void print() const {cerr << message << endl;}
  private:
    string message;
};

extern "C" void c_func() {
  throw error_class("ERROR!");
}

extern "C" void external_c_func();
void external_cc_func();

int main () {
  try{

//    external_cc_func(); // This works ok
//    c_func();           // This works ok
    external_c_func();    // This fails

    return 0;
  }
  catch (const error_class &err) {
    err.print();
    return 1;
  }
}

--- c_file.c ---
extern void c_func();

void external_c_func() {
  c_func();
}

--- cc_file.cc ---
extern "C" void c_func();

void external_cc_func() {
  c_func();
}

If these are compiled:
g++ -c main.cc -o main.o
g++ -c cc_file.cc -o cc_file.o
gcc -c c_file.c -o c_file.o
g++ cc_file.o c_file.o main.o -o throw_test

Then running 'throw_test' will produce:
terminate called after throwing an instance of 'error_class'
Abort(coredump)

The error that is thrown is not caught.


-- 
           Summary: An exception thrown through a C function is not caught
           Product: gcc
           Version: 4.0.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P2
         Component: c++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: JurgenvonOerthel at hotmail dot com


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=24187


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