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
  1. nested exceptions = except (eror1, error2...)
  2. exception clause always exits at the last except
  3. finally clause always go through all finally till the first

    Exceptions are not only for error

    try/finally can be used for workflow that needs ‘clean up’

  4. I/O file.close()
  5. Server curser commit or roll back
  6. 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())

Use sys.exc_info() to show. specific error. It returns a tuple contains (name, value, traceback object)