简体   繁体   English

我应该何时在Python中使用Try-Except语句?

[英]When Should I Use a Try-Except statement in Python?

Eg If I am trying to open a file, can I not simply check if os.path.exists(myfile) instead of using try/except . 例如,如果我试图打开一个文件,我不能简单地检查是否os.path.exists(myfile)而不是使用try/except I think the answer to why I should not rely on os.path.exists(myfile) is that there may be a number of other reasons why the file may not open. 我认为为什么我不应该依赖os.path.exists(myfile)的答案是,可能还有许多其他原因导致文件无法打开。

Is that the logic behind why error handling using try/except should be used? 这是使用try/except进行错误处理的原因吗?

Is there a general guideline on when to use Exceptions in Python. 是否有关于何时在Python中使用异常的一般准则。

Race conditions. 比赛条件。

In the time between checking whether a file exists and doing an operation that file might have been deleted, edited, renamed, etc... 在检查文件是否存在与执行操作之间的时间内,该文件可能已被删除,编辑,重命名等...

On top of that, an exception will give you an OS error code that allows you to get more relevant reason why an operation has failed. 最重要的是,异常将为您提供操作系统错误代码,使您可以获得操作失败的更多相关原因。

Finally, it's considered Pythonic to ask for forgiveness, rather than ask for permission. 最后,它被认为是Pythonic要求宽恕,而不是要求许可。

Generally you use try/except when you handle things that are outside of the parameters that you can influence. 通常,当您处理可以影响的参数之外的事物时,请使用try / except。

Within your script you can check variables for type, lists for length, etc. and you can be sure that the result will be sufficient since you are the only one handling these objects. 在您的脚本中,您可以检查类型的变量,长度列表等,并且您可以确保结果足够,因为您是唯一处理这些对象的人。 As soon however as you handle files in the file system or you connect to remote hosts etc. you can neither influence or check all parameters anymore nor can you be sure that the result of the check stays valid. 但是,当您处理文件系统中的文件或连接到远程主机等时,您既不能再影响或检查所有参数,也不能确保检查结果保持有效。

As you said, 如你所说,

  • the file might be existent but you don't have access rights 该文件可能存在但您没有访问权限
  • you might be able to ping a host address but a connection is declined 您可以ping一个主机地址,但连接被拒绝

There are too many factors that could go wrong to check them all seperately plus, if you do, they might still change until you actually perform your command. 有太多因素可能会出错,无法单独检查它们,如果这样做,它们可能仍会改变,直到您实际执行命令为止。

With the try/error you can generally catch every exception and handle the most important errors individually. 使用try / error,您通常可以捕获每个异常并单独处理最重要的错误。 You make sure that the error is handled even if the test succeeds at first but fails after you start running your commands. 即使测试最初成功但在开始运行命令后失败,也要确保处理错误。

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

相关问题 Python:try-except,在应有的情况下不调用try - Python: try-except, doesn't call try when it should 如何在 try-except 语句中使用 MergeError? - How to use MergeError in try-except statement? 使用带有try-except块的python“with”语句 - Using python “with” statement with try-except block 我应该在try-except块中添加else语句来处理return语句吗? - Should I add an else statement to try-except block to handle the return statement? 简洁地替换Python中的Try-Except语句的长列表? - Concise replacement for long list of Try-Except statement in Python? 我应该在定义函数的模块中定义try-except,还是在Python中调用它的模块中定义try-except? - Should I define try-except in module where function is defined or in module where it is called in Python? Python:当我尝试将它应用于 http 请求时,try-except 表达式总是获得默认值 - Python: try-except Expression always get the default value when I try to apply it on http request 为什么在调用完全执行此操作的函数时必须重做 try-except 语句? - Why do I have to redo a try-except statement when calling a function that does this exactly? Python性能:尝试 - 不在或不在? - Python performance: Try-except or not in? Python 中的 Try-except 或异常 - Try-except or Exceptions in Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM