1 # -*- coding: utf-8 -*- 2 """ 3 Created on Sat Dec 15 09:45:20 2018 4 5 @author: zhen 6 """ 7 # 测试else 8 try: 9 print("--------------")10 except NameError as e: # 捕获NameError类型异常11 print(e)12 except Exception as e: # 捕获其它异常13 print(e)14 else:15 print("没有异常会接着往下执行。。。")16 finally:17 print("最后一定执行。。。")18 19 # 测试捕获某种或某些种except20 try:21 print(aaa)22 print("--------------") # 出现异常后不会执行下面的程序23 except NameError as e: # 捕获NameError类型异常24 print(e)25 except Exception as e: # 捕获其它异常26 print(e)27 else:28 print("没有异常会接着往下执行。。。")29 finally:30 print("最后一定执行。。。")31 32 # 测试捕获其它异常 33 try:34 open("a.txt")35 print("--------------")36 except NameError as e: # 捕获NameError类型异常37 print(e)38 except Exception as e: # 捕获其它异常39 print(e)40 else:41 print("没有异常会接着往下执行。。。")42 finally:43 print("最后一定执行。。。")44 45 # 测试异常传递46 def err():47 print(aaa)48 49 try:50 print("===============")51 err()52 except NameError as e: # 捕获NameError类型异常53 print(e)54 55 print("use try will make your code beauty")
结果: