异常处理和断言

try:

  [body]

except [ErrorType1]

  [handler1]

except [ErrorType2]

  [handler2]

except:

  [handler3]

 

自定义异常:

def main():
    class TreelightException(Exception):
        def __init__(self, msg):
            self.message = msg


    try:
        raise TreelightException('测试自定义异常')
    except TreelightException as e:
        print(e)


if __name__ == '__main__':
    main()
View Code

 

断言:

import importlib
aa = importlib.import_module('lib.aa')


def main():
    assert type(aa.C().name) is int
    print('Pass')

if __name__ == '__main__':
    main()
View Code

 

转载于:https://www.cnblogs.com/Treelight/p/10714902.html

你可能感兴趣的:(异常处理和断言)