In [ ]:
In [2]:
# basic try/except
def is_int(val):
try:
x = int(val)
return True
except ValueError:
return False
is_int('erik')
Out[2]:
In [3]:
# finally
def is_int(val):
try:
x = int(val)
return True
except ValueError:
return False
finally:
print('done')
is_int('3')
Out[3]:
In [4]:
is_int('erik')
Out[4]:
In [11]:
def is_int(val):
try:
x = int(val)
return True
except ValueError as e: #e is the handled exception
print(e)
return False
is_int('erik')
Out[11]:
In [15]:
# multiple except blocks are tried in order, until a match is found
def is_int(val):
try:
x = int(val)
return True
except TypeError:
print('caught TypeError')
return False
except Exception:
print('caught Exception') # this one is caught
return False
except ValueError as e:
print('caught ValueError')
return False
is_int('e')
Out[15]:
In [14]:
# beause of above, you should order except blocks from most specific to least
def is_int(val):
try:
x = int(val)
return True
except ValueError:
print('caught ValueError')
return False
except Exception as e:
print('caught Exception')
return False
is_int('e')
Out[14]:
In [16]:
# getting code to run if NO exception is raised
def is_int(val):
try:
x = int(val)
except ValueError:
print('caught ValueError')
return False
else:
print('no error')
return True
is_int('10')
Out[16]:
In [17]:
# having two exceptions use the same except block
def is_int(val):
try:
x = int(val)
except (TypeError, ValueError):
print('caught ValueError or TypeError')
return False
else:
print('no error')
return True
is_int('erik')
Out[17]:
In [18]:
# catching all exceptions
def is_int(val):
try:
x = int(val)
return True
except Exception: # use Exception, which handles all exceptions that are not supposed to automatically kill the app
return False
is_int('erik')
Out[18]:
In [43]:
# raising a simple exception
def raise_on_non_string(val):
if not isinstance(val, str):
raise TypeError('Expected a string')
try:
raise_on_non_string(34)
except Exception as e:
print(e)
In [42]:
# catching an exception, wrapping it with your own and re-throwing
def wrap_raise_on_non_string(val):
try:
return raise_on_non_string(val)
except TypeError as e:
raise RuntimeError('Wrapping error....') from e
In [55]:
# catching and rethrowing
def wrap_raise_on_non_string(val):
try:
return raise_on_non_string(val)
except TypeError:
print('TYPE ERROR')
raise
try:
wrap_raise_on_non_string(34)
except Exception as e:
print(e)
In [ ]: