Exceptions for flow control

I was enjoying lesson 10 when suddenly heard @jeremy explaining how to use Exceptions for flow control, especially for callbacks as a way to stop training/epoch/batch, etc.

Everyone in the software industry knows that this is a terrible idea - https://web.archive.org/web/20140430044213/http://c2.com/cgi-bin/wiki?DontUseExceptionsForFlowControl

How is this anti-pattern can be a good idea for Callbacks!

In Java and C++ maybe, but in python using exceptions as part of program flow is pretty common, eg see nice answer here:

PS your question does come across as a little sharp

1 Like

Indeed. Offensively so.

Iā€™d like for students to come here with an open mind to learn new things, rather than thinking they already know all the answers.

Both Swift and Python have implemented exceptions in a way that explicitly makes them work well for control flow. I tried half a dozen different approaches to control flow in this part of the callbacks notebook and found that exceptions were a clear winner for clarity both at caller and callee.

1 Like

ok fair enough, this is good to know, thanks for the clarification.

i also would prefer that we set af stop on the learner because:

  1. it is a rare event that most callbacks does not use
  2. the runner can decide if i should stop immediately or finish calling the current subscriber of the precise function fx begin_batch

but coming from different backgrounds we have different preferences