Python3基础 try-except 几个异常采取同样的处理方法
Python : 3.7.0 OS : Ubuntu 18.04.1 LTS IDE : PyCharm 2018.2.4 Conda : 4.5.11 typesetting : Markdown code """ @Author : 行初心 @Date : 18-9-23 @Blog : www.cnblogs.com/xingchuxin @Gitee : gitee.com/zhichengjiu """ def main(): # 几个异常采取同样的处理方法,即except后面跟随多个异常类型 try: sum1 = 1 + '1' # 数据类型错误 file = open('不存在的文件.txt') file.close() except (OSError, TypeError) as reason: print('错误的原因是:', str(reason)) print() try: # sum1=1+'1' #数据类型错误 file = open('不存在的文件.txt') file.close() except (OSError, TypeError) as reason: print('错误的原因是:', str(reason)) if __name__ == '__main__': main()