Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What happens if exception gets thrown "through" c code? [duplicate]

Tags:

c++

c

exception

Possible Duplicate:
Will C++ exceptions safely propagate through C code?

If you have c code, for example the png lib, with your own io handlers which are written in c++ and an exception gets thrown due to some io error. is it ok to let it go through the c code and catch it outside of the c code? i know that care has to be taken with memory leaks but typically all structures get pre-allocated.

like image 243
Jochen_0x90h Avatar asked Jun 21 '11 14:06

Jochen_0x90h


People also ask

What happens if an exception is thrown but not caught?

What happens if an exception is not caught? If an exception is not caught (with a catch block), the runtime system will abort the program (i.e. crash) and an exception message will print to the console. The message typically includes: name of exception type.

Can you throw exceptions in C?

C doesn't support exceptions. You can try compiling your C code as C++ with Visual Studio or G++ and see if it'll compile as-is. Most C applications will compile as C++ without major changes, and you can then use the try... catch syntax.

What will happen if exception is thrown?

When an exception is thrown using the throw keyword, the flow of execution of the program is stopped and the control is transferred to the nearest enclosing try-catch block that matches the type of exception thrown. If no such match is found, the default exception handler terminates the program.

What will happen if throw exception is not handled in C Plus Plus?

Explanation: As the func() is throwing a const char* string but we the catch block is not catching any const char* exception i.e. exception thrown is not handled therefore the program results into Aborted(core dumped).


2 Answers

It is entirely up to the compiler if this will work or not. None of the language standards can obviously say anything about what the other language should do.

In the best case, the exception will pass the C code and get back to the next C++ level while possibly leaking any dynamically allocated C structures. In the less good case, it will crash and burn!

One possibilty is of course to compile the C code as C++, while also checking that it is exception neutral.

like image 66
Bo Persson Avatar answered Oct 05 '22 17:10

Bo Persson


The answer to your question is implementation-defined.

For GCC, as @Kerrek says, compiling the C code with -fexceptions will ensure the run-time remains consistent when an exception is thrown through C code. (Note that modern exception mechanisms are not just implemented via setjmp/longjmp; they actually unwind the stack frame by frame in order to handle destructors and try/catch blocks properly.)

However, the C code itself may not be expecting to have exceptions thrown through it. Lots of C code is written like this:

acquire a resource do some stuff release resource 

Here "acquire a resource" could mean malloc, or pthread_mutex_lock, or fopen. If your C++ code is part of the "do some stuff", and it throws an exception, well... Whoops.

Resource leaks are not the only problem; so is correctness is general. Imagine:

subtract $100 from savings account add $100 to checking account 

Now imagine an exception gets thrown after the first step finishes but before the second one does.

In short, although your implementation may provide a way to let you throw exceptions through C code, it is a bad idea unless you also wrote that C code knowing exactly where an exception might be thrown.

like image 21
Nemo Avatar answered Oct 05 '22 17:10

Nemo