Embedded C ++: use exceptions or not?

I understand that this can be subjective, so ask a specific question, but first: background:

I have always been an embedded software engineer, but usually at level 3 or 2 of the OSI stack. I'm not really a guy. In general, I have always made telecommunication products, usually hand-held / cell phones, which usually means something like an ARM 7 processor.

Now I am in a more general embedded world, in a small launch, where I can go to "not very powerful" processors (there is a subjective bit) - I can not predict which one.

I read a lot about the debate about exception handling in C ++ in embedded systems, and there is no clear answer. There are a few minor worries about portability and a few about runtime, but basically it looks like code size (or am I reading the wrong debate?).

Now I have to decide whether to use or refuse to handle exceptions - for the whole company forever (it is included in some very strong s / w).

It might seem like “how long has it been part of the string”, but someone might answer “if your piece of string is 8051 then no. If OTOH, this is ...”.

Which way am I jumping? Super safe and lose a good feature or exclusive code and possibly run into problems later?

+38
c ++ exception exception-handling embedded
Feb 09 2018-10-09T00
source share
4 answers

In terms of performance, I understand that exceptions actually reduce the size and increase the performance of regular code execution paths, but make exception / error paths more expensive. (often much more expensive).

So if your only concern is performance, I would say don’t worry later. If today the processor can handle this, then tomorrow too.

However. In my opinion, exceptions are one of those functions that require programmers to be smarter than you would expect from programmers. So I say - if you can stay away from exception-based code. Stay away.

Look at Raymond Chen Cleaner, more elegant and difficult to recognize . He says it better than I could.

+19
Feb 09 '10 at 1:57
source share

The biggest problem with exceptions is that they do not have predictable runtimes. Thus, they are not suitable for hard real-time applications (and I believe that most embedded applications do not fall into this category).

The second (possibly) increase in binary size.

I would suggest you read the C ++ Technical Performance Report , which specifically discusses topics that interest you: the use of C ++ in embedded (including hard real-time systems) and how exceptions are usually handled and what kind of overhead they have.

+10
Feb 09 '10 at 10:32
source share

The choice of whether to use exceptions or not should really be whether they fit your program domain well or not.

I have used C ++ exceptions extensively, both in retrofitting with old C code and in some newer code. (TIP. Do not attempt to reinstall the 20-year-old C code written in a low-memory environment with any inconsistent exceptions. This is just a nightmare).

If your problem is that it handles all errors in one place (for example, a TCP / IP server of some type, where each error condition occurs with "break the connection and try again"), then the exceptions are good - you can just throw an exception anywhere and you know where and how it will be handled.

If, on the other hand, your problem is not amenable to central error handling, then exceptions are a BIG pain, because trying to figure out where something (or should) be handled can easily become a Sisyphean task. And it's really hard to see the problem just by looking at the code. Instead, you should look at the call trees for this function and see where the exceptions to this function will end to find out if you have a problem.

+9
Feb 09 '10 at 2:18
source share

I would say use exceptions if the runtime supports them. Exceptions for handling emergency conditions are accurate and may cause small overheads depending on implementation. Some environments do not support them, especially in the embedded world. If you forbid them, be careful to explain why. I once had a guy who, when told to not use exceptions, instead made a division by zero. Not exactly what we had in mind.

+4
Feb 09 '10 at 2:01
source share



All Articles