I’m using c++ and i’m in a really deeply nested set of functions and a special case has occurred and I would like to exit to the top level.
Now I hear c++ exceptions are deprecated now so what should I use instead in this case?
So what i’m really asking is, are setjmp() and longjmp() OK in c++ code?
If possible I would avoid using
setjmp/longjmp, as most C stuff, in C++ code. For what concerns C++ exceptions, as far as I know, they are not deprecated. Indeed, I think it would be very hard to deprecate such a pervasive feature without severely hindering backwards compatibility. It’s possible that you have heard that (some form of) exception specifications,a feature of the recent C++11 standard that was not present in the previous version of the language,has been deprecated during the approval process (see, e.g., this post on Herb Sutter’s blog).This said, exceptions in C++ are not easy to use well. C++ has lots of features, and sometimes they interplay in very complex ways. Some necessary (but not sufficient) advices are:
std::exception;But the best advice is: understand exactly how exceptions work – precanned advices do not work well. At the purpose, you might read this, this, this and this about how to use exceptions and RAII. Especially “More Effective C++” has a whole chapter dedicated to exceptions, exposing some intricate consequences of apparently simple snippets of code. It raises your overall awareness of the language.