简体   繁体   English

如何在try except块中检查某种类型的OSError?

[英]How do I check for a certain type of OSError in a try except block?

I have some code that potentially could raise an OSError based on the user's input. 我有一些代码可能会根据用户的输入引发OSError More specifically, it could raise OSError: [WinError123] . 更具体地说,它可能引发OSError: [WinError123] The problem I'm facing is that my try except block checks for OSError which is way too broad of an exception. 我面临的问题是我的尝试除了块检查OSError这是一个太宽泛的异常。

I've looked at this question and this question however, it's unclear to me how errno works. 我看过这个问题和这个问题但是,我不清楚errno是如何工作的。 I've also looked at the errno documentation but it is unclear to me how it relates to the specific errors within OSError . 我也查看了errno 文档,但我不清楚它是如何与OSError的特定错误相关的。

How do I catch a specific OSError namely, WinError 123 ? 如何捕获特定的OSErrorWinError 123

Also if you could explain to me what libraries you utilized / how you did it / the thought process of your solution would be wonderful! 另外,如果您能向我解释一下您使用的库/您的工作方式/解决方案的思维过程将非常棒!

Can you not do something like: 你能不能做这样的事情:

try:
    my_function()
except OSError as e:
    if e.args[0] != 123:
        raise
    print("Deal with the 123 error here")

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM