Skip to content

Commit

Permalink
Update exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
hombit committed Jan 9, 2019
1 parent f4f161b commit b3d0ffa
Showing 1 changed file with 42 additions and 2 deletions.
44 changes: 42 additions & 2 deletions scientific_python/a_intro/x_exceptions.py
Expand Up @@ -14,8 +14,8 @@
# `ZeroDivisionError was caught`

# An exception is represented as a special object of built-in class
# `Exception` (or its inheritor, see below). Such an object can be get using
# `except ... as` syntax:
# `Exception` (or its inheritor, see details below). Such an object can be
# get using `except ... as` syntax:
try:
1 + '0'
except TypeError as e:
Expand Down Expand Up @@ -69,3 +69,43 @@
assert count_type_errors == 1

# ## Exception inheritance hierarchy

# As it was mentioned above all exception classes inherit `BaseException`
# built-in class. The main inheritor of this class is `Exception`, which is
# the base of almost all built-in exceptions and should be used to create
# user-defined exceptions. See the full hierarchy of built-in exceptions on
# https://docs.python.org/3/library/exceptions.html#exception-hierarchy

assert issubclass(Exception, BaseException)
assert issubclass(ValueError, Exception)

# Multiple `except` statements work like `if` with one or more `elif`
# statements: only first matched exception is used, not all of them.
# In the next example `LookupError` and its inheritors `IndexError` and
# `KeyError` will be used.

assert issubclass(IndexError, LookupError)

a = []
try:
x = a[0]
except LookupError as e:
count_lookup_error = 1
except IndexError as e:
assert False # we cannot be here
assert count_lookup_error == 1

try:
x = a[0]
except KeyError as e:
assert False # not a KeyError
except IndexError as e:
count_index_error = 1
except LookupError as e:
assert False
assert count_index_error == 1

# ## Catch them all

# It could be desired to catch all types of exceptions at once. It is
# possible, but be aware to use it all the time.

0 comments on commit b3d0ffa

Please sign in to comment.