logoalt Hacker News

dotancohentoday at 12:21 PM2 repliesview on HN

  > using exceptions for control flow is deprecated?
Exceptions are for the exceptional cases - the ones that mean normal operations are being suspended and error messages are being generated. Don't use them for control flow.

Replies

mort96today at 12:35 PM

In Python, an iterator raises a StopIteration exception to indicate to the for loop that the iterator is done iterating.

In Python, the VM raises a KeyboardInterrupt exception when the user hits ctrl+c in order to unwind the stack, run cleanup code and eventually exit the program.

Python is a quite heavy user of exceptions for control flow.

Hendriktotoday at 12:32 PM

Typically yes, but not in Python. In Python it is quite common and accepted, and sometimes even recommended as Pythonic to use exceptions for control flow. See iterators, for example.

I really dislike this too, but that’s how it is.