如何在PYTHON里捕获和抛出异常

 时间:2024-11-13 01:03:42

1、result = int(input("What is the result when 1 plus 1?"))#这里需要用户输入整数,但是这里却是字符,那么就显示出错。

如何在PYTHON里捕获和抛出异常

3、try: result = int(input("What is the result when 1 plus 1?"))except Exception as result: print("This is unknow error %s. " % result)except: print("The value is wrong!")print("The end!")#这个时候就可以排除所有错误了。

如何在PYTHON里捕获和抛出异常

5、try: result = int(input("What is the result when 1 plus 1?"))except Exception as result: print("This is unknow error %s. " % result)else: print("No error!")finally: print("The end!")#但是如果有异常else不会运行。但是finally是会运行的。

如何在PYTHON里捕获和抛出异常

7、def test1(): return int(input("What is the result when 1 plus 1?"))try: test1()except Exception as result: print("somthing is wrong %s. " % result) #面对这种情况也是可以在最后面加上try和except来捕获异常。

如何在PYTHON里捕获和抛出异常

9、def test(): result = input("What is the result when 1 plus 1?") if len(result) == 1: return result print("there is an error.") error = Exception("输入错误") raise errortry: print(test())except Exception as result: print(result) #设置try和except在下方,然后正常运行一下。

如何在PYTHON里捕获和抛出异常
  • excel如何用rept函数按照写的次数重复显示文本
  • 怎样使用VBA单元格链接?
  • 如何用VBA批量产生新的EXCEL并命名?
  • VBA批量删除含有某些关键词的行
  • wps中如何使用函数计算不重复个数?
  • 热门搜索
    k3次列车怎么买票 泸州旅游 奔驰smart怎么样 怎么配置电脑 股票的特点 手机时间不准怎么办 纸飞机怎么折飞得久 北京工业大学耿丹学院怎么样 孕妇可以吃山药 南京旅游景区