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

**URL:** <https://discourse.julialang.org/t/how-to-run-a-final-function-when-either-program-fails-or-user-crtl-c/118174>\
**Category:** General Usage\
**Created:** [August 14, 2024, 10:55am UTC](https://discourse.julialang.org/t/how-to-run-a-final-function-when-either-program-fails-or-user-crtl-c/118174 "2024-08-14T10:55:20Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ahmed\_Salih](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ahmed_salih/32/206579_2.png) [@Ahmed\_Salih](https://discourse.julialang.org/u/Ahmed_Salih)\
**Post date:** [August 14, 2024, 10:55am UTC](https://discourse.julialang.org/t/how-to-run-a-final-function-when-either-program-fails-or-user-crtl-c/118174/1 "2024-08-14T10:55:20Z")

</div>

Hello!

I was wondering if there was some way to run a final function when either the program fails due to for example division by zero or when an user chooses to terminate a program?

The first bit, running something when a program fails I believe I could do with a try-catch-finally loop, but what about the crtl + c part?

Kind regards

---

<div class="post-metadata">

**Author:** ![screw\_dog](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/screw_dog/32/48119_2.png) [@screw\_dog](https://discourse.julialang.org/u/screw_dog)\
**Post date:** [August 14, 2024, 11:03am UTC](https://discourse.julialang.org/t/how-to-run-a-final-function-when-either-program-fails-or-user-crtl-c/118174/2 "2024-08-14T11:03:54Z")

</div>

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`.

---

<div class="post-metadata">

**Author:** ![Ahmed\_Salih](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ahmed_salih/32/206579_2.png) [@Ahmed\_Salih](https://discourse.julialang.org/u/Ahmed_Salih)\
**Post date:** [August 14, 2024, 11:10am UTC](https://discourse.julialang.org/t/how-to-run-a-final-function-when-either-program-fails-or-user-crtl-c/118174/3 "2024-08-14T11:10:52Z")

</div>

Could you point me to a small example for `Base.exit_on_sigint`?

I am new to handling crtl + c events and exit in general. `atexit` did not seem to work for me, since it is only triggered when exciting Julia completely, while crtl + c just stops the currently running code.

Kind regards

---

<div class="post-metadata">

**Author:** ![screw\_dog](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/screw_dog/32/48119_2.png) [@screw\_dog](https://discourse.julialang.org/u/screw_dog)\
**Post date:** [August 14, 2024, 11:22am UTC](https://discourse.julialang.org/t/how-to-run-a-final-function-when-either-program-fails-or-user-crtl-c/118174/4 "2024-08-14T11:22:06Z")

</div>

If you are running in a Julia REPL (which I’m guessing you’re doing as ctrl+c isn’t exiting completely) then you don’t need to set `Base.exit_on_sigint`. But you can get help with `?Base.exit_on_sigint` in the REPL.

For example, a simple try/catch works for me like this:

```julia
julia> try
         sleep(5.0)
       catch
         println("Hello")
       end
^CHello

```

If you’re running a script using `julia <filename>` then you’ll need

```julia
Base.exit_on_sigint(false)
try
  sleep(5.0)
catch
  println("Hello")
end

```

which produces

```julia
$ julia script.jl
^CHello

```
