python try except 0

python

Solution

From what I understand, This is very useful for debugging purposes (Catching the type of exception)

In your example 0 acts as a placeholder to determine the type of exception.

>>> try:
...   x = 5/1 + 4*a/3
... except 0:
...   print 'error'
... 
Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
NameError: name 'a' is not defined
>>> try:
...   x = 5/0 + 4*a/3
... except 0:
...   print 'error'
... 
Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
ZeroDivisionError: integer division or modulo by zero

In the first case, the exception is `NameError` and `ZeroDivisionError` in the second. the `0` acts as a placeholder for any type of exception being caught.

>>> try:
...   print 'error'
... except:
... 
KeyboardInterrupt
>>> try:
...   x = 5/0 + 4*a/3
... except:
...   print 'error'
... 
error

Problem

I am reading some python code written a while ago, and found this: ``` try: # do some stuff except 0: # exception handling stuff ``` And I'm just not sure what except 0 means? I do have my guesses: Supposed to catch nothing i.e. let the exception propagate or it could be some sort of switch to turn debugging mode on and off by removing the 0 which will then catch everything. Can anyone lend some insight? a google search yielded nothing... Thanks! Some sample code (by request): ``` try: if logErrors: dbStuffer.setStatusToError(prop_id, obj) db.commit() except 0: traceback.print_exc() ```

Original source