如何在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中如何使用函数计算不重复个数?
  • 热门搜索
    油面筋怎么做好吃 带鱼怎么处理干净 老年斑是怎么形成的 台账怎么做 bape怎么读 三方协议怎么签 辣条怎么做家庭做法 夯怎么读 入党志愿书怎么写 你怎么舍得让我的爱流向海