How to run a final function when either program fails or user crtl + c?

Depending on whether you are running Julia interactively, Ctrl-C produces an InterruptException which can be caught like any other. This behaviour can be changed with Base.exit_on_sigint.

Alternatively, you can register a function using atexit.