python是否可以忽略该错误并返回到提升行旁边的行并继续

python是否可以忽略该错误并返回到提升行旁边的行并继续,python,Python,就这样 try: 1/0 print "hello world" print "every thing seems fine..." except ZeroDivisionError: print "It is not a critical error, go to next..." SomeWayAllowMeToExeutePrintHelloWorld_TheLineNextToTheRaisedLine() except: pri

就这样

try: 
    1/0
    print "hello world"
    print "every thing seems fine..."

except ZeroDivisionError: 
    print "It is not a critical error, go to next..."
    SomeWayAllowMeToExeutePrintHelloWorld_TheLineNextToTheRaisedLine()

except: 
    print "I have no idea, just stop work..."

在[1/0]引发后,[Exception ZeroDivisionError]捕捉到错误,然后返回到[print hello world]行,然后继续…

您可以在尝试后放置打印行。。。除了语句外,请使用第二次尝试。。。第二个except的except语句


尽管如此,在第二种例外情况下,如果您只想停止程序,则不应捕获异常。

否。当引发异常时,它总是转到捕获它的块。如果要返回导致异常的那一行之后的那一行,则应立即处理异常,然后将该行放在处理异常的代码下面。

您不能这样做,并且没有理由要:

try: 
    1/0
except ZeroDivisionError:
    print "It is not a critical error, go to next..."

print "hello world"
print "every thing seems fine..."
考虑以下代码:

try: 
    important_res = f(1/0)
    send_important_message(important_res)
except ZeroDivisionError: 
    print "It is not a critical error, go to next..."
    SomeWayAllowMeToExeutePrintHelloWorld_TheLineNextToTheRaisedLine()

如果允许继续执行,如何选择要传递给f的值?

如果不明确说明要捕获错误的位置和不需要捕获的内容,就不能这样做

执行上述操作后,可以添加finally:代码块:

try:
    1/0
except ZeroDivisionError:
    print "error"
finally:
    print "hello world"
    print "every thing seems fine..."

不可以。实现您想要的方法是使用两个try语句:

try:
    try: 
        1/0
    except ZeroDivisionError: 
        print "It is not a critical error, go to next..."

    print "hello world"
    print "every thing seems fine..."
except: 
    print "I have no idea, just stop work..."
这里try/except子句的正确用法是使用else或finally或两者,正如其他答案所示

但是,您也可以重构代码,使尝试的每个步骤都是它自己的函数,然后使用循环将它们很好地组合在一起

例如:

steps = [step1, step2, step3] # pointers to function/methods

for step in steps:
    try:
        step()
    except Exception as exc:
        # handle the exception
在您的代码上:

def step1():
    1/0

def step2():
    print "hello world"

def step3():
    print "every thing seems fine..."

steps = [step1, step2, step3]

for step in steps:
    try:
        step()
    except ZeroDivisionError:
        print "It is not a critical error, go to next..."
    except:
        print "I have no idea, just stop work..."
结果是:

>>> 
It is not a critical error, go to next...
hello world
every thing seems fine...

我想做的可能的重复就是破解别人的代码。在try:body中,我将调用其他函数。我不能改变他的功能,它只是提出了一个没有问题的例外,它应该考虑。所以我想要的是至少做一些基本的事情:在错误恢复下一步是不是基本的。你真正的问题是你依赖的功能不符合你的需要。错误恢复下一步类似于重写函数。在这种情况下,要做的事情是重写函数。如果你不能做到这一点,你就没有追索权。最终,OP不想做什么,他/她只想在正常情况下和零分错误情况下打印,而不是其他情况下打印。你不能拆分[1/0]和2个打印行,因为一个函数中可能有。如果你有办法把一个函数分成几行,而这些行仍然可以很好地工作,那么你就会得到一个很好的解决方案。我对gdb不太了解,似乎它可以让代码一步一步地运行。你有什么想法吗?我知道我想要的不是python,但python是动态的,任何事情都应该是可能的。我不确定你在问什么,你给出了一个例子——我只能为你提供的解决方案提供一个解决方案。如果你有一个不同的用例,那么请展示它。在这一点上,最好问一个新问题:发送重要消息重要消息会引发另一个错误,因为未处理,并且不会让下一个恢复,所以事情照常进行。@truease.com:处理是什么意思?它应该抛出什么错误?重要的东西应该有什么价值?嵌套这些东西什么都没有。答案可能就是你们的意思。
>>> 
It is not a critical error, go to next...
hello world
every thing seems fine...