异常处理和断言

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

 

posted on 2019-04-16 09:24  Treelight  阅读(193)  评论(0编辑  收藏  举报