Missed the WAT thread, so here's one for intentionally cursed code

I hate/love this so much.

To the list it goes.

Fantastic thread

To expand on this, another angle is that without else rethrow(), Julia’s bare catch “falls back” to continuing after a if-elseif exception type block. One could argue this is equally valid default behavior, like returning nothing for the “cursed” sqrt_second, but PEP8 argues:

A bare except: clause will catch SystemExit and KeyboardInterrupt exceptions, making it harder to interrupt a program with Control-C, and can disguise other problems. If you want to catch all exceptions that signal program errors, use except Exception: (bare except is equivalent to except BaseException: ).

To explain BaseException, PEP352 introduced another layer of hierarchy to separate some lower-level exceptions from the run-of-the-mill Exception superclass in case the user does want generic handling of the latter; I wouldn’t call except Exception: default behavior because a subset is specified. I don’t know if there’s something similar in Julia, but we wouldn’t need to change our flatter Exception hierarchy with an extra defined supertype because we have Union.

BaseException # Julia’s equivalent is just called Exception
├── BaseExceptionGroup
├── GeneratorExit
├── KeyboardInterrupt
├── SystemExit
└── Exception
… …├── …

I personally agree that Control-C should simply stop the program unless InterruptException is specifically handled, so rethrowing makes more sense as a default behavior for caught exceptions over continuing. Here’s an example where the InterruptException gets caught and risks continuing:

function foo(t=3.0) # t gives time to Control-C
  try
    sleep(t) # Control-C gets caught
    div(1,0)
  catch
    println("catching...")
    sleep(t) # now Control-C stops the program
  end
  "foo end"
end
1 Like