Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handling STL errors without exceptions

Tags:

c++

exception

stl

I have a project which uses STL a lot. Now I'm working on porting the project to a specific platform which doesn't support exceptions. I can disable exceptions, however I still need to handle STL errors.

Is there any approach to handle STL errors correctly with exceptions disabled? Is there any third party STL implementation which helps with it?

like image 780
Rom098 Avatar asked Oct 06 '10 07:10

Rom098


People also ask

What happens if an exception is not caught in C++?

CPP. 4) If an exception is thrown and not caught anywhere, the program terminates abnormally. For example, in the following program, a char is thrown, but there is no catch block to catch the char.

Why are exceptions not used in C++?

The main reason C++ exceptions are so often forbidden is that it's very hard to write exception safe C++ code. Exception safety is not a term you hear very often, but basically means code that doesn't screw itself up too badly if the stack is unwound.

What happens when a method detects an error and is unable to handle it?

After a method throws an exception, the runtime system leaps into action to try and find someone to handle the exception. The set of possible "someones" to handle the exception is the set of methods in the call stack of the method where the error occurred.

Does Google use C++ exceptions?

We do not use C++ exceptions.


2 Answers

The problem with taking an existing std library coontainer and compiling with exceptions disabled is that the the std container interfaces themselves assume exceptions are enabled. Using exceptions, operator new will throw if it cannot acquire memory, without exceptions, operator new returns a 0 instead, which std containers cannot handle.

One approach is to only use STL algorithms + vector. You can replicate about 95% of what the other containers do using this. The problem is that most STL implementations assume that

v.reserve(v.size()+1);
assert(v.size()+1<=v.capacity());

will never assert (since reserve will throw if there is no memory). To insure this never throws, I have used "fixed capacity" containers, i.e. containers with a capacity fixed at compile time. Basically these are vectors where I pass in a special allocator. Then you can check the max_size() of the container before insertion. Then just avoid using things like at(). For even better predicatbilty, use basic_string instead of vector. This forces you to only store POD types, which never throw when copied or default constructed. Plus memory requirements are easier to compute.

Another approach is to use intrusive containers. These don't throw (outside of misuse of the interface perhaps), since they never acquire memory in the first place.

like image 104
Lance Diduck Avatar answered Sep 28 '22 06:09

Lance Diduck


Possibly an old version of stlport can be configured not to use exceptions. This obviously is non-standard but satisfies your requirement.

like image 37
Peter G. Avatar answered Sep 28 '22 07:09

Peter G.