I have a function that potentially throws an error, which is why I have code to catch the error. But if for some reason my code in the except statement does not resolve the error, is it good standard to throw an exception on purpose after the code has resolved? Obviously indicating that there is maybe some additional code that needs to be added.
我有一个可能引发错误的函数,这就是为什么我有代码来捕获错误的原因。但是,如果由于某种原因,我在Except语句中的代码没有解决错误,那么在代码解决之后故意抛出异常是一个好的标准吗?显然,这表明可能需要添加一些额外的代码。
Please see below for an example:
请参阅下面的示例:
error_counter = 0
error_limit = 10
completed = False
while error_limit > error_counter and not completed:
try:
# Do something
completed = True
except ValueError as e:
print('Trying again...')
error_counter += 1
# Code to resolve error (wait longer or other etc.)
if error_counter >= error_limit:
raise Exception('error, code needs updating!')
I have updated the original question to be clearer and more correct. Please check edit log if you need to.
我已更新了原来的问题,使其更清晰、更正确。如有需要,请勾选编辑日志。
更多回答
Please do not edit your question in ways that might invalidate existing answers.
请不要以可能使现有答案无效的方式编辑您的问题。
Chris has a good point. The correct<footnote>1</footnote> way to deal with an egregious mistake in your Question is to make it clear to the reader that you corrected the mistake; e.g. don't replace the bad code with better code, but add the better code below, flagged as an UPDATE. Better still, don't make the mistake in the first place! <footnote>1 - correct per the consensus standards of SO ... based on 10+ years of doing it.</footnote>
克里斯说得很有道理。正确的1处理问题中严重错误的方法是让读者清楚地知道您已更正错误;例如,不要将错误代码替换为更好的代码,而是添加下面标记为更新的更好代码。更好的是,不要一开始就犯错误!<脚注>1-按照这样的共识标准纠正…基于十多年的实践经验。脚注>
If I wanted to implement that logic, I would write it like this:
如果我想实现这个逻辑,我会这样写:
error_counter = 0
error_limit = 10
completed = False
while not completed:
try:
# Do something
completed = True
except ValueError as e:
error_counter += 1
if error_counter >= error_limit:
# bail out!
raise Exception('error, code needs updating!')
else:
print('Trying again...')
# Code to resolve error
This version of the code is more DRY. The logic for handling the retrying and ultimate failure handling is in one place. It makes it easier to understand.
这个版本的代码更加枯燥。处理重试和最终失败处理的逻辑在一个地方。这让它更容易理解。
But if for some reason my code in the except statement does not resolve the error, is it good standard to throw an exception on purpose after the code has resolved?
I'm not going to say what is "good standard" (because there is no standard!) or "good practice" (because that is a matter of opinion). However:
我不会说什么是“好标准”(因为没有标准!)或者“良好实践”(因为这是一个见仁见智的问题)。但是:
- Catching an exception and immediately raising a different one is a common practice. Lots of code does this.
- In general, raising an exception to indicate that some part of your code has failed ... is what exceptions are designed for.
In Java ... it is widely accepted that throwing Exception
is a bad idea. But the strongest reason against doing this is to do with checked vs unchecked exceptions. Python doesn't make that distinction.
在爪哇。抛出异常是一个坏主意,这一点被广泛接受。但是,反对这样做的最有力的理由是处理已检查异常与未检查异常。Python没有做出这样的区分。
Your question leans heavily into opinion, but...
你的问题很偏向观点,但是...
Your while loop is only broken and continues onto the following code if error_limit
is less than or equal to error_counter
, or rather if error_counter
is greater than or equal to error_limit
.
只有当ERROR_LIMIT小于或等于ERROR_COUNTER,或者ERROR_COUNTER大于或等于ERROR_LIMIT时,While循环才会中断并继续执行以下代码。
Thus the condition error_counter >= error_limit
is always true. As such, this is not an exceptional circumstance, so an exception seems a little out of place.
因此,条件ERROR_COUNTER>=ERROR_LIMIT始终为真。因此,这并不是例外情况,因此例外似乎有点不合时宜。
Having said that, there is nothing wrong with catching an exception, handling it, then throwing a different exception for your code to handle at another level.
话虽如此,捕获并处理一个异常,然后抛出一个不同的异常让您的代码在另一个级别进行处理并没有什么错。
更多回答
hey, thanks. this is helpful and I am always looking to learn. correct, I just randomly inserted the ValueError
, because I just wanted to illustrate on a high level what I was thinking.... of course this is not the error that I am dealing with.
嘿,谢谢。这很有帮助,我一直在寻找学习的机会。正确,我只是随机插入了ValueError,因为我只是想从高层次上说明我在想什么……当然,这不是我正在处理的错误。
I'm not sure I'd use a loop conditional on a boolean variable like completed
version while True:
and then a break
where appropriate.
我不确定我是否会使用布尔变量的循环条件,比如Complete Version While True:,然后在适当的地方使用Break。
That's a matter of opinion too. Either works, but when you write code you have to make a choice.
这也是一个见仁见智的问题。两者都有效,但当您编写代码时,您必须做出选择。
我是一名优秀的程序员,十分优秀!