如何在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中如何使用函数计算不重复个数?
  • 热门搜索
    房颤怎么治疗 诞怎么读 湖南科技大学怎么样 怎么做系统 苹果4怎么越狱 武林外传五行阵怎么破 号被盗了怎么办 怎么保养卵巢 奥黛丽赫本怎么死的 男人怎么减肥