Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I throw an exception in C?

I typed this into Google, but I only found how-tos in C++.

How can I do it in C?

like image 756
httpinterpret Avatar asked May 23 '10 12:05

httpinterpret


People also ask

How do you throw an exception?

Throwing an exception is as simple as using the "throw" statement. You then specify the Exception object you wish to throw. Every Exception includes a message which is a human-readable error description. It can often be related to problems with user input, server, backend, etc.

How do we do exception handling in C?

As such, C programming does not provide direct support for error handling but being a system programming language, it provides you access at lower level in the form of return values. Most of the C or even Unix function calls return -1 or NULL in case of any error and set an error code errno.

Is there exceptions in C?

Although C does not provide direct support to error handling (or exception handling), there are ways through which error handling can be done in C. A programmer has to prevent errors at the first place and test return values from the functions.

Can we throw exception class?

You can't throw an exception in class definition. You should throw your exception in method definition or use try-catch block. Show activity on this post. throwing a specific type of Exceptions will reduce bugs because the base class Exception can handle all the types of exceptions.


2 Answers

There are no exceptions in C. In C the errors are notified by the returned value of the function, the exit value of the process, signals to the process (Program Error Signals (GNU libc)) or the CPU hardware interruption (or other notification error form the CPU if there is)(How processor handles the case of division by zero).

Exceptions are defined in C++ and other languages though. Exception handling in C++ is specified in the C++ standard "S.15 Exception handling", there is no equivalent section in the C standard.

like image 55
Brian R. Bondy Avatar answered Oct 07 '22 07:10

Brian R. Bondy


In C you could use the combination of the setjmp() and longjmp() functions, defined in setjmp.h. Example from Wikipedia

#include <stdio.h> #include <setjmp.h>  static jmp_buf buf;  void second(void) {     printf("second\n");         // prints     longjmp(buf,1);             // jumps back to where setjmp                                  //   was called - making setjmp now return 1 }  void first(void) {     second();     printf("first\n");          // does not print }  int main() {        if ( ! setjmp(buf) ) {         first();                // when executed, setjmp returns 0     } else {                    // when longjmp jumps back, setjmp returns 1         printf("main");         // prints     }      return 0; } 

Note: I would actually advise you not to use them as they work awful with C++ (destructors of local objects wouldn't get called) and it is really hard to understand what is going on. Return some kind of error instead.

like image 35
vava Avatar answered Oct 07 '22 08:10

vava