简体   繁体   English

如何在try中除了块之外在Python中处理多个相同的错误类型

[英]How can I handle multiple same Error type in Python in try except block

I am trying to handle multiple same error in try except block. 我试图在try除了块之外处理多个相同的错误。 I have 2 function where I run 2nd function in except block if 1st block gives an error. 如果第一个块发出错误,我有2个函数,我在第二个函数中运行除了块。

I tried raising the exception like many post suggested and it didn't help. 我尝试像许多帖子一样提出异常,但没有帮助。 I am writing a simple code which in a way is similar running multiple function. 我正在编写一个简单的代码,在某种程度上类似于运行多个函数。 How can i try 10/0 if it fails try 20/0 in except and if we get error go to last except block? 我怎么能尝试10/0如果它失败尝试20/0 in除了如果我们得到错误去最后除了块?

try:
    d =10/0
except ZeroDivisionError as e :

    d=20/0
except ZeroDivisionError as f:
    print("yes")

Result I am expecting according to my above code is "yes" since I get ZeroDivisionError twice. 结果我期待根据我的上面的代码是“是”因为我得到ZeroDivisionError两次。

You can chain try/except as follows. 您可以按如下方式链接try / except。

try:
    d =10/0
except ZeroDivisionError as e :
    try:
        d=20/0
    except ZeroDivisionError as f:
        print("yes")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python:如何使用相同的 try/except 块简化多个语句 - Python: How to simplify multiple statements with the same try/except block 除了 python 中的 web 错误消息外,我如何处理 Selenium 尝试? - How can I handle Selenium try except web error messages in python? 我应该如何在我的 for 循环中添加 Try & except Block 来处理 - How should I add Try & except Block to handle in my for loop Python尝试除了块不识别错误类型 - Python try except block does not recognize error type 如何在try块中为条件执行相同的代码而不重复except子句中的代码 - How can I execute same code for a condition in try block without repeating code in except clause 如何在具有相同条件的一个 try/except 块中对单个和循环操作进行分组? - How can I group single and loop operations in one try/except block with same condition? 如何在try except块中检查某种类型的OSError? - How do I check for a certain type of OSError in a try except block? 如何使用单个 Try-Except 块解决多个错误消息? - How do I account for multiple error messages with a single Try-Except block? Python:如何在try / except块之外使用错误? - Python : How to use an error out of a try/except block? 我怎样才能让我的for循环从try和except块中停下来的地方继续呢? Python 3 - How can i make my for loop continue from where it left off in the try and except block? Python 3
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM