Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ANSI C equivalent of try/catch?

Tags:

I have some C code I'm working with, and I'm finding errors when the code is running but have little info about how to do a proper try/catch (as in C# or C++).

For instance in C++ I'd just do:

try{ //some stuff } catch(...) { //handle error } 

but in ANSI C I'm a bit lost. I tried some online searches but I don't see enough info about how to make it happen / figured I'd ask here in case anyone can point me in the right direction.

Here's the code I'm working with (fairly simple, recursive method) and would like to wrap with try/catch (or equivalent error-handling structure).

However my main question is simply how to do a try / catch in ANSI C...the implementation / example doesn't have to be recursive.

void getInfo( int offset, myfile::MyItem * item ) {     ll::String myOtherInfo = item->getOtherInfo();     if( myOtherInfo.isNull() )         myOtherInfo = "";     ll::String getOne = "";     myfile::Abc * abc = item->getOrig();     if( abc != NULL )     {         getOne = abc->getOne();     }     for( int i = 0 ; i < offset ; i++ )     {              printf("found: %d", i);     }     if( abc != NULL )         abc->release();     int childCount = item->getChildCount();     offset++;     for( int i = 0 ; i < childCount ; i++ )         getInfo( offset, item->getChild(i) );     item->release(); } 
like image 590
aiden Avatar asked Sep 21 '10 16:09

aiden


People also ask

Is there a try and catch in C?

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

Is there exception handling in C?

The C programming language does not support exception handling nor error handling. It is an additional feature offered by C. In spite of the absence of this feature, there are certain ways to implement error handling in C. Generally, in case of an error, most of the functions either return a null value or -1.

What does C use instead of exceptions?

C doesn't have exceptions, it still lets you deal with exceptional cases. You can do the same in C++. You'd deal with errors just like you do in other languages without exceptions: lots of explicit code to check the result of every function call, and long debugging sessions when someone gets it wrong.

What is error handling in C?

Error handling is not supported by C language. There are some other ways by which error handling can be done in C language. The header file “error. h” is used to print the errors using return statement function. It returns -1 or NULL in case of any error and errno variable is set with the error code.


2 Answers

Generally, you don't.

It's possible to use setjmp and longjmp to build something fairly similar to try/catch, although there's no such thing in C as destructors or stack unwinding, so RAII is out of the question. You could even approximate RAII with a so-called "cleanup stack" (see for example Symbian/C++), although it's not a very close approximation, and it's a lot of work.

The usual way to indicate errors or failure in C is to return a value indicating success status. Callers examine the return value and act accordingly. See for example the standard C functions: printf, read, open, for ideas how to specify your functions.

When mixing C and C++ code, you must ensure that a C++ exception never reaches C code. When writing C++ functions that will be called from C, catch everything.

like image 150
Steve Jessop Avatar answered Oct 11 '22 02:10

Steve Jessop


C does not support exception handling.

There is info on one approach to this problem here. This shows the simple setjmp/longjmp approach but also provides a more sophisticated alternative, covered in depth.

like image 35
Steve Townsend Avatar answered Oct 11 '22 02:10

Steve Townsend