How can translate C++ to C code with exceptions handling? Unfortunately
Stroustrup translator cfron has only source and not compiled, llvm command
to translate C++ => C not works.
C doesn't have exception handling; it's one of the many features that
C++ added to C. You can use setjmp()/longjmp() to implement a similar
functionality, but doing so is much more complicated than having
exceptions as a built-in feature of the language. That's precisely why
Stroustrup put exceptions in C++.
One of the more important complications has to do with destructors,
another C++ feature that doesn't exist in C. In C++, objects with
automatic storage duration and class type have their destructors
automatically called when their lifetime ends, even if it ends as a
result of an exception being thrown. Furthermore, it is guaranteed that
the destructors will be called in the reverse of the order in which the
constructors were called. When you translate your code to C, you'll have
to move the clean-up code from your C++ destructors into ordinary C
functions, and you'll have to insert explicit calls to them, rather than
relying on them being called implicitly. In particular, you need to make
sure that they get called even when you call longjmp() to emulate C++
exception handling. This is going to require that you keep track, in
some manner, of the list of destructors that need to be called; given
that the corresponding setjmp() will often be in a very different
section of the code, keeping track of which destructors need to be
called is non-trivial.
Redesigning your code so it doesn't rely upon exception handling is
painful, but can result in better C code than trying to translate C++
exception handling directly.
On the other hand: if your entire program never actually handles
exceptions, then you may be able to adequately emulate exceptions simply
by replacing throw() with exit(). That's the case for the C++ code I'm
working on right now.