try:
[body]
except [ErrorType1]
[handler1]
except [ErrorType2]
[handler2]
except:
[handler3]
自定义异常:
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
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()
断言:
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
import importlib aa = importlib.import_module('lib.aa') def main(): assert type(aa.C().name) is int print('Pass') if __name__ == '__main__': main()