使用已处理的异常作为预期触发器?
有时,在我的代码中,我会故意使用抛出的异常作为事件触发器。例如,我将循环直到抛出异常,然后在 catch
子句中进行 break;
。这是不好的做法吗?查询我正在循环的某些属性来预先确定索引(即预先确定何时停止循环)是否会更有效(或更干净)?当然,只有当我确定实际上会抛出异常以避免无限循环时,我才会这样做。谢谢!
Occasionally in my code I will intentionally use a thrown exception as an event trigger. For instance, I will loop UNTIL an exception is thrown and then break;
in the catch
clause. Is this bad practice? Would it be more efficient (or cleaner) to query some attribute of what I am looping to predetermine the indices (i.e. predetermine when to stop looping)? Of course I only do this when I am sure that an exception will in fact be thrown to avoid an infinite loop. Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这实际上并不是一个与语言无关的问题!
某些语言(例如 Python)具有相当轻量级的异常,并且它们使使用异常进行控制流成为一种相当无可争议的方法 - 例如,Python 中的每个
for
语句(除非被提前终止) abreak
) 总是在发生异常时终止(for
中使用的迭代器的StopIteration
异常)。因此,Python 的任何用户都不能反对由异常系统地终止的循环……除非他们从不使用for
循环(不太可能;-)。其他语言将异常视为真正异常的事件,在这些语言中,您不应该将它们用于普通的流程控制任务;显然,当前对您的问题给出的所有答案都以“与语言无关”的方式认为这是理所当然的,忽略了诸如Python之类的语言的存在或实际性质。
我们这些对这两种语言都相当了解的人学会了“随波逐流”:在 Python 中使用异常进行流控制不会有任何问题,但我肯定会避免在 C++ 中这样做!
This isn't really a language-agnostic issue!
Some languages, such as Python, have reasonably light-weight exceptions, and they make the use of exceptions for control flow a pretty unobjectionable approach -- for example, every
for
statement in Python (unless prematurely terminated by abreak
) is always terminated when an exception occurs (aStopIteration
exception from the iterator used in thefor
). Any user of Python cannot therefore object to a loop systematically terminated by exceptions... unless they never, ever usefor
loops (pretty unlikely;-).Other languages consider exceptions to be a really exceptional occurrence, and in those languages you shouldn't use them for ordinary flow-control tasks; apparently all the answers currently given to your question take this for granted in a "language agnostic" way, ignoring the existence or the actual nature or languages such as Python.
Those of us who know both kinds of languages reasonably well learn to "swim with the flow": I'd have no problems using exceptions for flow control in Python, but I'd definitely avoid doing so in C++, for example!
是的,这是不好的做法。异常应该用于特殊的情况 - 例如,在正常操作过程中不应发生的真正错误的情况。
一个重要原因是异常对于 CPU 时间来说是昂贵的。
如果您使用标志或其他信号来终止循环,它会更有效,并且可能更容易调试和验证。
Yes, this is bad practice. Exceptions should be used for things that are exceptional - e.g. conditions that are true errors that should not occur in the normal course of operation.
One important reason is that exceptions are expensive in terms of CPU time.
It will be much more efficent, and likely much easier to debug and verify if you use a flag or other signal to terminate your loop.
将异常作为正常程序控制流的一部分抛出是一种不好的做法。应为异常事件(例如错误处理)保留异常。
不断循环等待某些事件发生也不是很好。查看观察者设计模式,了解正确的编码方式类型的事物。
It's bad practice to throw an exception as part of normal program control flow. Exceptions should be reserved for exceptional events such as error handling.
It's also not so great to loop continually waiting for some event to happen. Check out the Observer design pattern for the right way to code this type of thing.