Created
February 26, 2013 01:37
-
-
Save cheekybastard/5035033 to your computer and use it in GitHub Desktop.
python_exception_examples
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Python 2.x has an ambiguous except syntax, Python 3.x is stricter so the | |
# following examples help to identify the right way to handle Py2/3 compatible | |
# exceptions | |
# Background: http://www.python.org/dev/peps/pep-3110/ | |
# Note that 'as' and ',' are both accepted in Python 2.x but only 'as' in Python 3.x: | |
# http://docs.python.org/reference/compound_stmts.html#try | |
# There are longer notes on re-raising, stack traces and tracebacks here: | |
# http://www.doughellmann.com/articles/how-tos/python-exception-handling/index.html | |
# http://blog.ianbicking.org/2007/09/12/re-raising-exceptions/ | |
# and a more basic intro to exceptions here: | |
# http://eli.thegreenplace.net/2008/08/21/robust-exception-handling/ | |
# Works in both Python 2.7 and 3.2 | |
# Considered bad form - all exceptions are silently swallowed | |
# This can be useful in an outer loop if you log the exception | |
try: | |
1/0 | |
print("Will never get here") | |
except: | |
print("Catches all exceptions, this is bad form") | |
# This variant catches just one type of exception, this is | |
# the more useful way to handle exceptions | |
try: | |
1/0 | |
print("Will never get here") | |
except ZeroDivisionError: | |
print("This is a ZeroDivisionError") | |
# Works in both Python 2.7 and 3.2 - note use of "as" | |
# This variant catches a known error (e.g. to handle specific situations) | |
# and it also has a catch-all which could be used for logging | |
try: | |
"a" + 22 | |
print("Will never get here") | |
except ZeroDivisionError: | |
print("This is a ZeroDivisionError") | |
except Exception as err: | |
print("This catches any other type of error: " + str(err)) | |
# Works on Python 2.7, Fails on Python 3.2 | |
# due to syntax changes - avoid using the "," | |
#try: | |
# 1/0 | |
# print("Will never get here") | |
#except ZeroDivisionError, err: # this form fails in Python 3.2 | |
# print("Got divide by zero: " + str(err)) | |
# Works in both Python 2.7 and 3.2 | |
# Here we catch several known exceptions and handle them in the same | |
# block of code | |
try: | |
1/0 | |
print("Will never get here") | |
except (ZeroDivisionError, KeyError) as err: | |
print("Got divide by zero: " + str(err)) | |
try: | |
1/0 | |
print("Will never get here") | |
except ZeroDivisionError: | |
print("This is a ZeroDivisionError") | |
# we can extract the traceback for logging | |
import sys | |
(tb_type, tb_value, tb_traceback) = sys.exc_info() # extract our traceback | |
import traceback | |
tb_as_list = traceback.extract_tb(tb_traceback) | |
print("First bit of our traceback:" + str(tb_as_list)[:30] + "...") | |
# and then re-raise the exception if it needs to go further up the line | |
# by uncommenting the 'raise' statement on the next line - this 'raise' | |
# will preserve the original stack/traceback | |
#raise | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment