[Cython] [cython-users] C++: how to handle failures of 'new'?

Sturla Molden sturla at molden.no
Wed Jul 4 02:58:14 CEST 2012


Den 03.07.2012 20:43, skrev Dag Sverre Seljebotn:
>
> Except for the fact that any code touching "new" could be raising 
> exceptions? That propagates.
>
> There is a lot of C++ code out there using exceptions. I'd guess that 
> both mathematical code and Google-written code is unlike most C++ code 
> out there :-) Many C++ programmers go on and on about RAII and 
> auto_ptrs and so on, and that doesn't have much point unless you throw 
> an exception now and then (OK, there's the occasional return statement 
> where it matters well).
>

Usually there is just one C++ exception to care about: std::bad_alloc. 
It is important to know that it can be raised almost anywhere in C++ 
code that are using the STL.

The majority of C++ programs never attempt to catch std::bad_alloc. 
Instead a program will set a global handler routine using 
std::set_new_handler. Usually a callback for memory failure will just 
display an error message and call std::exit with status 
std::EXIT_FAILURE. ||

When interfacing from Python I think a std::bad_alloc should be 
translated to a MemoryError exception if possible.  Though others might 
argue that one should never try to recover from a memory failure. 
Arguably, recovering std::bad_alloc might not be possible if the heap is 
exhausted.

Sturla





-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/cython-devel/attachments/20120704/e840679b/attachment.html>


More information about the cython-devel mailing list