Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Working with a C++ compiler that doesn't support exceptions?

I'm porting a C++ library to my mbed using the hosted mbed C++ compiler which is basically ARMCC with a configuration that you can't really change. One of the configuration options they decided on (for some unknown reason) is that exceptions are not supported. So a throw and a catch will yield compiler errors.

How exactly can you use the standard C++ library without exceptions? I use some vectors in my library. How do I know if the push_back function actually succeeded? Is there any standard way of knowing if an exception occurred or does it just do an exit(1) or something?

like image 972
Earlz Avatar asked Mar 04 '12 07:03

Earlz


People also ask

Why there is no 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.

Does C language support exception handling?

C does not provide direct support for error handling (also known as exception handling). By convention, the programmer is expected to prevent errors from occurring in the first place, and test return values from functions.

Which is used to handle the exceptions in C?

Exception handling is used to handle the exceptions. We can use try catch block to protect the code. Catch block is used to catch all types of exception. The keyword “catch” is used to catch exceptions.


2 Answers

How exactly can you use the standard C++ library without exceptions? I use some vectors in my library. How do I know if the push_back function actually succeeded? Is there any standard way of knowing if an exception occurred or does it just do an exit(1) or something?

You're venturing into very restrictive territory once you disable exception handling in C++.

Some standard library implementations, like Dinkumware's, allows one to disable exceptions. There it's a matter of defining a macro, _HAS_EXCEPTIONS, as 0. STLPort has a similar convention with _STLP_USE_EXCEPTIONS=0.

However, there is no standard definition of what standard libraries should do when exceptions are disabled. Exception-handling, for the most part, is pretty much ingrained into the C++ language. Even dynamic_cast and operator new/new[] throw by default and those are not library features.

There's also a lack of a clear definition of what should happen even for the standard library implementations that don't throw. If a sequence push_back throws in the process of allocating more memory for that sequence, what should happen? Is the element simply not inserted? The standard interfaces of these sequences certainly tells us nothing about when such an error occurs.

Furthermore, a lot of C++ libraries in general will be using functions that do throw like operator new (and not the nothrow versions). As a result, we do venture into a lot of undefined behavior territory once we disable exceptions.

I once had to work in a company which forbid exception-handling since the senior programmers in charge were premature optimizers who preferred C and thought C++ was awful and inefficient (coincidentally, they wrote some of the most inefficient code on the team with a strong preference for linked lists as a default container which caused profiling hotspots to show up left and right due to the sheer number of tiny nodes being allocated/deallocated for everything, but that's another story).

With embedded systems, the argument against exception handling might be a bit stronger, but it does become difficult to rely on C++ in general without it. I think the best you can do without exception-handling is accept a crippled form of C++ without a lot of the standard library parts that throw unless you want to invest a lot of time trying to find workarounds and tricks specific to your particular standard library vendor which might be more trouble than it's worth.

like image 57
stinky472 Avatar answered Nov 03 '22 19:11

stinky472


They explain why it's not supported here:

The conventional wisdom (and the advice from the armcc compiler guys) is that the overhead of exceptions is pretty high and therefore not appropriate for this sort of domain. For now, we are therefore not supporting exceptions (it is hard to remove support, but easy to add it).

We'll definitely look at really understanding the space and time overheads at some point (mbed isn't really conventional, so perhaps exceptions are perfect!), but for now you'll have to stick to more conventional exception handling approaches.

And here:

We don't support exception handling in the compiler, and it is not planned to add it. But i'd be happy to hear about how you usually use them in your microcontroller applications, or your experiences! But for now, you will have to turn to more standard C solutions.

Based on this I can guess the exceptional situations would end up in std::terminate().

I don't think that not supporting exceptions is a legal option in C++ per the language standard. So, you should either make an experiment to see what happens when new or push_back() fails or ask the people behind the compiler.

like image 29
Alexey Frunze Avatar answered Nov 03 '22 19:11

Alexey Frunze