Exceptions
Exceptions are objects
Exception
is the super class for user-defined exceptions. It’s meant for non-system-exit errors. BaseException
is just a frame for exceptions. We shouldn’t directly use it.
workflows for try/exception/else/finally
try:
func() #This function might raise error
except MyError1:
... #Error1 is catched here
except MyError2 as error2:
... #for error2
except (MyError3, MyError4) as error3_4:
... # catch multiple errors
else:
... # when no errors are raised in try block
finally:
... # Always execute this 'clean up' process before else or except clause
- nested exceptions =
except (eror1, error2...)
- exception clause always exits at the last
except
- finally clause always go through all
finally
till the firstExceptions are not only for error
try/finally can be used for workflow that needs ‘clean up’
- I/O
file.close()
- Server curser
commit
orroll back
- Multi-threads
threading.Lock()
try/else clauses for workflows that return only meaningful results
def search():
if succeed:
return item
else:
raise MyError()
try:
item = search()
except MyError():
do_something_else()
else:
process(item)
general try/except for programs meant to run forever, like server.
Inherit same type of exception. So same parent exception can intercept them all.
Intercept all w/o changing code
class Error1(ValueError): pass
class Error2(ValueError): pass
try:
func() # raise Error1 or Error2
Except ValueError:
print(sys.exc_info())