[Bug] ensure clause not run when call `exit`early

Well, depends on how you look at it. Apparently ensure isn’t run when you exit. The docs says:

An ensure clause is executed at the end of a begin ... end or begin ... rescue ... end expression regardless of whether an exception was raised or not:

Thing is, the exit means that the end of the begin is never reached as such, as the program is, well, stopped.

It works in Ruby because exit in Ruby raises SystemExit, which means we’re still running, while Crystal exit Terminates execution immediately, returning the given status code to the invoking environment.

One could argue that Rubys usage of SystemExit is a bit odd, as exiting isn’t an error condition but part of normal program flow. But I kinda liked it.

7 Likes