Fix Python – I want to exception handle ‘list index out of range.’

I am using BeautifulSoup and parsing some HTMLs.
I’m getting a certain data from each HTML (using for loop) and adding that data to a certain list.
The problem is, some of the HTMLs have different format (and they don’t have the data that I want in them).
So, I was trying to use exception handling and add value null to the list (I should do this s….

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

Should I test if something is valid or just try to do it and catch the exception?

Is there any solid documentation saying that one way is preferred?
Is one way more pythonic?

For example, should I:
if len(my_list) >= 4:
x = my_list[3]
x = ‘NO_ABC’

x = my_list[3]
except IndexError:
x = ‘NO_ABC’

Some thoughts…

Fix Python – How to get exception message in Python properly

What is the best way to get exceptions’ messages from components of standard library in Python?
I noticed that in some cases you can get it via message field like this:
except Exception as ex:

but in some cases (for example, in case of socket errors) you have to do something like this:
except socket.err….

Fix Python – Does ‘finally’ always execute in Python?

For any possible try-finally block in Python, is it guaranteed that the finally block will always be executed?
For example, let’s say I return while in an except block:
except ZeroDivisionError:
print(“Does this code run?”)

Or maybe I re-raise an Exception:
except ZeroDivisionError:

Fix Python – Re-raise exception with a different type and message, preserving existing information

I’m writing a module and want to have a unified exception hierarchy for the exceptions that it can raise (e.g. inheriting from a FooError abstract class for all the foo module’s specific exceptions). This allows users of the module to catch those particular exceptions and handle them distinctly, if needed. But many of the exceptions raised from th….

Fix Python – Logging uncaught exceptions in Python

How do you cause uncaught exceptions to output via the logging module rather than to stderr?
I realize the best way to do this would be:
raise Exception, ‘Throwing a boring exception’
except Exception, e:

But my situation is such that it would be really nice if logging.exception(…) were invoked automatically wh….