C++ exception handling alternatives
#1
Posted 03 April 2006 - 10:43 PM
I'm sick of exceptions. It devours memory, slows down my applications, and terminates them when they could have continued without a problem. Okay that last thing is probably my fault because I should catch and handle each and every one of them, but it's still a pain. So should I just use good old assert? All I'm using exceptions for is to catch (pun unintended) development errors anyway. In release builds they're not helpful at all.
But I'm hesitative and looking for some expert advice. Can anyone honestly tell me they need exceptions badly? Or did anyone get rid of them and gained in performance and stability without regrets? Note that for my applications performance is of very high importance and keeping things running with minor flaws is way better than terminating the application with a 'friendly' notification.
Thanks,
Nick
P.S: Actually I've already made up my mind a while ago but I just want new opinions.
#2
Posted 03 April 2006 - 11:24 PM
-
Currently working on: the 3D engine for Tomb Raider.
#3
Posted 03 April 2006 - 11:29 PM
If you'd really rather have a "high importance" bit of software keep running with 'minor flaws' rather than just have it crash outright with a detailed error report, you scare me and I hope you never write software that my life depends on. The same basic memory corruption problem could be "minor" one run and seriously trash your data on another -- there really isn't any such thing as a "minor flaw" in this area.
I kind of get what you're going for, but if that sort of always-on uptime is your goal you should still let the application crash and just have some background application manager start a new instance when it notices that the previous one crashed.
I think the exception debate had merit back in the days when most compilers did a bad job implementing them, but these days they work perfectly fine in gcc and MSVC++, one or the other of which a huge percentage of the C++ programmers out there will be using.
If you really don't want to use exceptions in C++, you don't have to. Do things the old fashioned C way. Its your choice. But IMO exceptions are extremely useful and I use them a lot.
#4
Posted 03 April 2006 - 11:37 PM
.oisyn said:
#5
Posted 03 April 2006 - 11:54 PM
George McBay said:
We compile all our games with exceptions turned off, this gives us a pretty good overall speed gain (MSVC++ 7.1 that is for all MS platforms, don't know which gcc we're using for the PS2 though). And besides, there is not much reason to throw exceptions in games anyway as you can mostly assume things will work, especially on a console.
(On another note, MSVC++ has the quirck that with exceptions enabled it isn't able to inline functions that return or receive unwindable objects by value, like the __m128 SSE type)
-
Currently working on: the 3D engine for Tomb Raider.
#6
Posted 04 April 2006 - 12:02 AM
George McBay said:
Quote
Quote
Quote
Quote
Quote
Quote
Quote
Thanks!
Nick
#7
Posted 04 April 2006 - 12:08 AM
.oisyn said:
#8
Posted 04 April 2006 - 07:40 AM
An example is the malloc call. I built my own wrapper for it which would throw an exception once an allocation failed. Then, I have a single try-catch-clause around my entire program, and wait for those critical exceptions there, rather than check for (pointer == Null) on each allocation.
However, in the more recent time, I think even that might not be really neccessary, since I could just add a simple message box and an exit statement in the allocator function.
Another argument against exceptions is that there rarely should be an occasion where an error deeply within nested code should be handled somewhere high above. From my point of view, it's simply a better idea to have a normal error return value, and react to it in a way that doesn't make the programm crash, but rather just fail gracefully, and continue as far as possible.
About performance, well I'm not too sure, but I think I had weird problems with VC6 with exceptions turned off, think it was something about some libraries requiring them, but I haven't looked into that much, and just left them enabled. However, if they are used by code outside your control, I guess you will still get a performance penalty.
Just my thoughts,
Cheers,
- Wernaeh
#9
Posted 04 April 2006 - 09:16 AM
There is places where you can hardly avoid exceptions (like guarding your vm that executes scripts). Other than that you can use events or something the like to handle exspected problems (like loosing a connection).
Personally I find it a pain to handle EVERY return value as an exception (like java). I throw only if an illegal error condition has occured or the condition cannot be handled in any way to resume the app (heap corrupt/out of mem/...)
Alex
#10
Posted 04 April 2006 - 10:01 AM
For games and stuff, I agree a small glitch is better than a crashing game. As mentioned before, use assert for most logic errors (errors caused by wrong use of a function), and something else for most runtime errors (errors caused by bad input or other external causes).
Myself, I use exceptions for runtime errors, because (a) about all code I write is used as a module for a scripting language. So, when an error happens, I mostly let it bubble up to the script (what would happen anyway if everything was written in that scripting language). And (b) more importantly, in my case correct results are more important than not crashing ... what makes it a completely different story.
So as always, the correct answer depends on the situation ;)
Bramz' warehouse | LiAR isn't a raytracer
#11
Posted 04 April 2006 - 12:18 PM
bramz said:
In general it's a bit more complicated than that. Suppose you're writing a library that will be supplied as a .lib/.dll to your client. Then any fault trapping that dissolved (ie assert) might actually cause a crash in release build - you just don't know what sort of input a library will have. So in a lib I tend to use exceptions rather than asserts. OTOH if you are supplying libs as source you can probably get away with asserts.
In an application you know exactly what's going into a function so assert works better. In fact you can do this in a library to within implementation-specific methods (ie ones with no public access).
Internal state checks are also good candidates for assertion. Often I implement a brute-force version of an algorithm first and then later switch in a better one. Keep the brute-force one in debug builds, apply both algorithms and assert the results are the same.
For console game development, exceptions are often passe. Memory is at a premium, and the performance hit is a big factor too. So in a single console or multi-platform development system, exceptions tend to get squeezed out in favour of assertions. If this is the case, then functions that can return an error should, and if it does then you are duty-bound to test that value. Game code that runs on a console with no hard-drive needs to be bullet-proof, so some compromise between efficiency and robustness needs to be made.
#12
Posted 04 April 2006 - 12:29 PM
bramz said:
Well, I guess it's also a case of throwing an exception and not correctly handling it, or using exceptions where other approaches would have worked better. Anyway, I'm now convinced that for my applications exceptions are evil and should be avoided when possible.
The only place where I still use exceptions is at public interfaces that return a success/failure code.
#13
Posted 04 April 2006 - 01:17 PM
Quote
-
Currently working on: the 3D engine for Tomb Raider.
#14
Posted 04 April 2006 - 03:16 PM
.oisyn said:
Anyway, that's obviously not an argument against exceptions. But imagine that I didn't have that one particular file that triggered the exception, but some 'end users' do get wrongly processed data... So exceptions should definitely be avoided in many situations.
One small extra question: Is there any C++ style assert? I have the impression that assert is considered C style and many programmers avoid it because they think exceptions can be used as superiour C++ style asserts. Are there any error handling constructs I might not know about yet?
#15
Posted 04 April 2006 - 03:34 PM
About throwing exceptions, I use a macro that adds __FILE__ and __LINE__ to the exception message, so that I at least know where the exception comes from. Then, even with a malconfigured debugger, I at least know where to look ... Of course, this also means to you have to wrap your main() in one big try/catch block to print the actual message.
You mentioned one anecdote where ingoring a user input error would not have harmed, while throwing an exception costed you a few hours of debugging. But there are an equal amount of anecdotes that will tell just the opposite.
So, as with everything: it's not because you have a hammer that everything is a nail, but not everything is a screw either.
Bramz' warehouse | LiAR isn't a raytracer
#16
Posted 04 April 2006 - 03:38 PM
-
Currently working on: the 3D engine for Tomb Raider.
#17
Posted 04 April 2006 - 03:46 PM
Bramz' warehouse | LiAR isn't a raytracer
#18
Posted 04 April 2006 - 03:47 PM
Nick said:
Interesting logic. Even you say it's not an argument against exceptions, you are using it as a reasoning for not using them. This your problem has nothing to do with the exceptions.
1. You should know what your compile settings are
2. you should handle the user input error somehow, was it exceptions or any other method it doesn't matter.
3. You should test your program always with malicious data/user input to see if your error handling is actually working.
You don't blame your car either if you fill the tank with baby oil..or do you?
#19
Posted 04 April 2006 - 04:13 PM
"juhnu" said:
I've found asserts much easier on the fingers for handling programming errors that shouldn't make it into the final product. Is there any drawback to making overzealous use of assertions?
As to exceptions, I would never attempt to add them to code that wasn't built with them in mind from the beginning. If you are careful with them, and only use them in the proper context, then they can be lovely. :)
#20
Posted 04 April 2006 - 04:40 PM
juhnu said:
Quote
Quote
Quote
Quote
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users












