3 ms·
Jesus Christ. Never ever use try catch inside a constructor. It's the fastest way to a headache.
by acron0 13y ago
Jesus Christ. Never ever use try catch inside a constructor. It's the fastest way to a headache.
- seabrookmx 13y agoExceptions are utterly broken in C++. I get flamed a lot for this, but I just avoid them in general in C++. In most languages this isn't the case, but in C++ I find explicit error handling code to be less work to write/maintain, even if it is a bit more verbose.
- Iftheshoefits 13y agoI don't care for them, either, for the same reason: they are less explicit (to me) than direct error handling at the point-of-failure. I've found exceptions are also actually more verbose in many cases, for the same level of granularity in error trapping. My view is that exceptions handled in try-catch blocks are okay when handling "bulk" initialization or other computations where it doesn't matter which particular step failed. In every other case, it's better, in my view, to use explicit code.
- byuu 13y agoI see two major use cases for exceptions in C++. The first, is for errors during recursive functions. Very challenging to escape from 13 levels deep into a recursive descent parser without throw. The second, is for container access-by-reference functions that are called on empty or out-of-bounds elements (eg array(4); array[16] = value;), since otherwise there is no way to return from the function, as you may not know how to construct a new container element (eg no default constructor), nor would you necessarily want to return a dummy object in that case.
- nikbackm 13y agoHow are they more broken compared to e.g. Java and C#? (Assuming you don't have a problem with these.) I would think having automatic cleanup via destructors would make C++ more pleasant to use in this aspect as you don't need to wrap all resource handling code in using and try-finally blocks.
- colin_mccabe 13y agoYou're not alone. The Google coding standard bans exceptions in C++. Firefox, V8, and a lot of other pieces of C++ software are also written without exceptions. It's particularly telling that the guys who wrote the LLVM compiler, who presumably know more about the dark corners of C++ than anyone, chose to ban exceptions in their code.