Python忽略异常并返回到我所在的地方 [英] Python Ignore Exception and Go Back to Where I Was

查看:206
本文介绍了Python忽略异常并返回到我所在的地方的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道使用下面的代码来忽略某个异常,但是如何让代码返回到异常处理并继续执行?说如果异常'异常'在do_something1中引发,那么如何使代码忽略它并继续完成do_something1并处理do_something2?我的代码只是到最后一个进程阻塞后进程传递除了块。请指教,谢谢。

I know using below code to ignore a certain exception, but how to let the code go back to where it got exception and keep executing? Say if the exception 'Exception' raises in do_something1, how to make the code ignore it and keep finishing do_something1 and process do_something2? My code just go to finally block after process pass in except block. Please advise, thanks.

try:
    do_something1
    do_something2
    do_something3
    do_something4
except Exception:
    pass
finally:
    clean_up

编辑:
感谢您的回复。现在我知道做什么是正确的方法。但这是另一个问题,我可以忽略一个特定的异常(说如果我知道错误号)。是否可以使用代码?

Thanks for the reply. Now I know what's the correct way to do it. But here's another question, can I just ignore a specific exception (say if I know the error number). Is below code possible?

try:
    do_something1
except Exception.strerror == 10001:
    pass

try:
    do_something2
except Exception.strerror == 10002:
    pass
finally:
    clean_up

do_something3
do_something4


推荐答案

更新。忽略特定异常的方法是捕获所需的异常类型,测试它以查看是否要忽略它,如果没有,则重新提出。

update. The way to ignore specific exceptions is to catch the type of exception that you want, test it to see if you want to ignore it and re-raise it if you dont.

try:
    do_something1
except TheExceptionTypeThatICanHandleError, e:
    if e.strerror != 10001:
        raise
finally:
     clean_up

如果你想要的话,每个 try 语句需要自己的 finally 子句。它不会将自己附加到以前的 try 语句。一个提出语句没有别的是重新提高最后一个异常的正确方法。不要让别人告诉你。

Note also, that each try statement needs its own finally clause if you want it to have one. It wont 'attach itself' to the previous try statement. A raise statement with nothing else is the correct way to re-raise the last exception. Don't let anybody tell you otherwise.

你想要的是python不是本地提供的延续。除此之外,你的问题的答案取决于你想做什么。如果您希望 do_something1 继续执行,无论例外如何,那么将不得不捕获异常并忽略它们。

What you want are continuations which python doesn't natively provide. Beyond that, the answer to your question depends on exactly what you want to do. If you want do_something1 to continue regardless of exceptions, then it would have to catch the exceptions and ignore them itself.

如果您只想要 do_something2 发生,无论 do_something1 是否完成,您需要

if you just want do_something2 to happen regardless of if do_something1 completes, you need a separate try statement for each one.

try:
   do_something1()
except:
   pass

try:
   do_something2()
except:
   pass

等。如果你能提供一个更详细的例子,说明你想做什么,那么有一个很好的机会,我自己或者比我更聪明的人可以帮助你,或者(更有可能)和你说话,建议一个更合理的

etc. If you can provide a more detailed example of what it is that you want to do, then there is a good chance that myself or someone smarter than myself can either help you or (more likely) talk you out of it and suggest a more reasonable alternative.

这篇关于Python忽略异常并返回到我所在的地方的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆