Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can a C program handle C++ exceptions?

I am developing C++ component dll that can be used by C or C++ applications. The exposed dll functions are as follows

#include <tchar.h>
#ifdef IMPORT
#define DLL __declspec(dllimport)
#else 
#define DLL __declspec(dllexport)
#endif

extern "C" {

    DLL  bool __cdecl Init();
    DLL  bool __cdecl Foo(const TCHAR*);
    DLL  bool __cdecl Release();

}

the internal implementation of these functions are C++ classes which are not exposed, I assume using this style the dll can be used either in C or C++ apps. The problem is I do not handle any type of c++ exception (i.e. bad_alloc) and I left this stuff to the caller (the higher layer). After much debate with my colleagues that I should catch all the exceptions and return error code or at least false because in case of C application it can not handle C++ exceptions? is that true? and what I should do in general? is there a rule of thumb for handling exeptions if you are developing component that will be used by other system.

like image 247
Ahmed Avatar asked Dec 15 '10 09:12

Ahmed


People also ask

How do you handle exceptions in Objective C?

Exception handling is made available in Objective-C with foundation class NSException. @try − This block tries to execute a set of statements. @catch − This block tries to catch the exception in try block. @finally − This block contains set of statements that always execute.

Can we use try-catch in C language?

Yes, it is limited to one try-catch in the same function.

How do you handle exceptions?

The try-catch is the simplest method of handling exceptions. Put the code you want to run in the try block, and any Java exceptions that the code throws are caught by one or more catch blocks. This method will catch any type of Java exceptions that get thrown. This is the simplest mechanism for handling exceptions.

Does C have try except?

The try-except statement is a Microsoft extension to the C language that enables applications to gain control of a program when events that normally terminate execution occur. Such events are called exceptions, and the mechanism that deals with exceptions is called structured exception handling.


2 Answers

C doesn't have exceptions, therefore in general you should catch all exception and return an error code and/or provide a function that returns the information about the last error.

like image 161
vitaut Avatar answered Oct 22 '22 18:10

vitaut


If this is Windows using MSVC then yes you can catch exceptions in C but you can't catch them that well. C++ exceptions are fed through the OS ABI's Structured Exception Handling mechanism, and Microsoft have a __try, __except, __finally C extension to handle OS structured exceptions. Note that these include access violations, divide-by-zero, etc. that you'd normally want to terminate your program and log a bug report. You can identify C++ exceptions by code 0xE04D5343 (4D 53 43 = "MSC") and throw the rest on.

That all said, you probably don't want to be throwing exceptions across a DLL boundary, and certainly not if you're only exposing a C API.

like image 41
Rup Avatar answered Oct 22 '22 18:10

Rup