异常处理和断言
try:
[body]
except [ErrorType1]
[handler1]
except [ErrorType2]
[handler2]
except:
[handler3]
自定义异常:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.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://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
import importlib aa = importlib.import_module('lib.aa') def main(): assert type(aa.C().name) is int print('Pass') if __name__ == '__main__': main()