Better to ‘try’ something and catch the exception or test if it’s possible first to avoid an exception?

You should prefer try/except over if/else if that results in speed-ups (for example by preventing extra lookups) cleaner code (fewer lines/easier to read) Often, these go hand-in-hand. speed-ups In the case of trying to find an element in a long list by: try: x = my_list[index] except IndexError: x = ‘NO_ABC’ the try, except is … Read more

Catching exception: divide by zero

You need to check it yourself and throw an exception. Integer divide by zero is not an exception in standard C++. Neither is floating point divide by zero but at least that has specific means for dealing with it. The exceptions listed in the ISO standard are: namespace std { class logic_error; class domain_error; class … Read more