异常捕获Try..exception

cava原创notebookpython大约 1 分钟

异常捕获

try........exception

1.一个try一个except ,except中可以捕获多种异常类型,try子句中若遇到执行有错误的语句1号,其余下的语句都将被忽略,捕获1号语句中的异常

2.异常类型可以放在一个元组中

3.非系统型异常可以用Except统一捕获

4.finally中的语句表示,无论如何都是会被执行的

while True:
    try:
        the_num=input("please enter here:")
        num1=int(the_num.split(",")[0].strip())
        num2=int(the_num.split(",")[1].strip())
        the_result=num1+num2
        print(the_result)
        test = {[1, 2]: "haha"}
        break
    #写法1
    # except ValueError as err:
    #     print("Value Err is {}".format(err))
    # except TypeError as err:
    #     print("Type Err is {}".format(err))
    #写法2
    except(ValueError,TypeError) as err:
        print("The Err is {}".format(err))
    # finally:
    #     print("这里是无论是否异常,都将执行的内容")
print("continue")
------------------------------------
#输出
please enter here:1 , 2
3
The Err is unhashable type: 'list'
please enter here:
-------------------------------------
please enter here: 1 , ss
The Err is invalid literal for int() with base 10: 'ss'
please enter here:
#因为TypeError的try子句顺序在ValueError之后,当ValueError执行捕获异常时,TypeError就不会被执行
上次编辑于:
贡献者: augwewe,AndersonHJB
你认为这篇文章怎么样?
  • 0
  • 0
  • 0
  • 0
  • 0
  • 0
  • 0
  • 0
  • 0
评论
  • 按正序
  • 按倒序
  • 按热度
Powered by Waline v2.14.7