异常处理
一.异常处理的几种情况
# try ... except
try: print("haha") except NameError as e: print(type(e))
# try ... except ... else
try: name print("haha") except NameError as e: print(type(e)) else: # 遇到错误就不执行后面的,没遇到错误就执行else后面的 print('我去')
# try ... finally
try: print("哈哈哈") finally: # 遇没遇到错误都执行后面的内容 print('我去')
# try ... except ... finally
try: aaa print("哈哈哈") except : print("错误") finally: # 遇没遇到错误都执行 print('我去')
# try ... except ... else ... finally
try: aaa print("哈哈哈") except Exception as e: print("错误") else: print("666") finally: # 遇到错误就不执行后面的,没遇到错误就执行else后面的 print('我去')
二.主动抛出异常
raise
三.断言
assert 1==1# 只能接受一个布尔值 False
print(123456)