Handle errors with try-except or if-else? Which is better?

Hello friends, I have already read some information about the above question on the internet, but I would like to know what are your thoughts about this. I read try-except should be only used if the ‘tried’ code is supposed to work correctly most of the time, otherwise if-else should be used by performance reasons.

I also read which python actually recommend the use of try-excepts; the whole idea of not checking for each error, but instead act as the code is right(unless it gets an exception) is new to me. If you want to know, I discovered about that yesterday; after my game code has already hundreds and hundreds of lines which make use of if-else to handle errors(no try or excepts). Any clarifications about this whole subject is welcome.

Personally, I haven’t come across a situation where I needed try except code in my Blender scripts. In my non-Blender Python code, I’ve used try except where I had to but I have a personal tendency towards using decorators to make such things conveniently disappear.

Sounds like the advice you’ve gotten so far has already been good. Best thing I can tell you is to try out those try-except blocks in places you’re already doing error handling and see what happens. However you handle it, remember… Version control is your friend. Cheers and good luck on your game.

Apart from tech details, the only difference is that if you don’t use exceptions you will have to manually provide the appropriate functionality to avoid getting your application behave strange or crash.

The most classic usage of Exceptions is for type safety, to ensure let’s say for example you will do math operations only with numbers or that you won’t do a division with zero.

I have been using PHP for the last 4 years and I have seen the transition towards version 5 and 6. In the old days there were no exceptions (:P), but from version 5 they were supported. The only difference was that you had manually check each variable in specific situations, the better your experience and guessing was the better the application would be. But the real deal is that there is always dangerous code sleeping like a Volcano. At least with exceptions you will be more safe in some way.

Thanks for the answers, cotejrp1 and const; I will see if there are still more people willing to give opinion on this subject, but I will also already try using uh…try at my code at least where it is obvious it would be a good practice.