# How to prevent Ctrl-C from terminating a program during readline()

**URL:** <https://discourse.julialang.org/t/how-to-prevent-ctrl-c-from-terminating-a-program-during-readline/42687>\
**Category:** General Usage\
**Tags:** question\
**Created:** [July 7, 2020, 6:46pm UTC](https://discourse.julialang.org/t/how-to-prevent-ctrl-c-from-terminating-a-program-during-readline/42687 "2020-07-07T18:46:42Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bryan\_So](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bryan_so/32/16228_2.png) [@Bryan\_So](https://discourse.julialang.org/u/Bryan_So)\
**Post date:** [July 7, 2020, 6:46pm UTC](https://discourse.julialang.org/t/how-to-prevent-ctrl-c-from-terminating-a-program-during-readline/42687/1 "2020-07-07T18:46:42Z")

</div>

Hi, the following simple code is my attempt to see if I can trap Ctrl-C during readline()

Say I’m writing a REPL for a simple language interpreter… obviously I don’t want user accidentally pressing Ctrl-C to cause program termination.

```julia
function enter()
    println("Enter a line:")
    try
        s = readline()
        println(s)
    catch ex
        if ex isa InterruptException
            println("Ctrl-C")
        else
            println("Other Exceptions")
        end
    end
end

enter()

```

But I find it is not able to get into the catch section.  
When user presses Ctrl-C Julia exits… this output was from MacOS Catalina. (Julia on Unbuntu also cannot trap Ctrl-C but with a much lengthier stack trace):

```julia
$ julia test_readline.jl

Enter a line:
abcd^C

signal (2): Interrupt: 2
in expression starting at /Users/bryanso/GDrive/fun/julia/test_readline.jl:18
kevent at /usr/lib/system/libsystem_kernel.dylib (unknown line)
unknown function (ip: 0x0)
Allocations: 2508 (Pool: 2497; Big: 11); GC: 0

```

Would you mind giving me a hint on how to trap Ctrl-C so I can recover from it?

Thanks a lot.

---

<div class="post-metadata">

**Author:** ![Bryan\_So](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bryan_so/32/16228_2.png) [@Bryan\_So](https://discourse.julialang.org/u/Bryan_So)\
**Post date:** [July 7, 2020, 6:54pm UTC](https://discourse.julialang.org/t/how-to-prevent-ctrl-c-from-terminating-a-program-during-readline/42687/2 "2020-07-07T18:54:46Z")

</div>

Found this topic in FAQ!

But let me know if there are other ways.

### How do I catch CTRL-C in a script?

_Running a Julia script using “julia file.jl” does not throw InterruptException when you try to terminate it with CTRL-C (SIGINT). To run a certain code before terminating a Julia script, which may or may not be caused by CTRL-C, use atexit. Alternatively, you can use “julia -e ‘include(popfirst!(ARGS))’ file.jl” to execute a script while being able to catch InterruptException in the try block._

---

<div class="post-metadata">

**Author:** ![jmkuhn](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jmkuhn/32/2090_2.png) [@jmkuhn](https://discourse.julialang.org/u/jmkuhn)\
**Post date:** [July 8, 2020, 1:46pm UTC](https://discourse.julialang.org/t/how-to-prevent-ctrl-c-from-terminating-a-program-during-readline/42687/3 "2020-07-08T13:46:47Z")

</div>

Starting with Julia 1.5 you will be able to add the following line to your `enter` function to get the desired behavior

```julia
Base.exit_on_sigint(false)

```

---

<div class="post-metadata">

**Author:** ![Bryan\_So](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bryan_so/32/16228_2.png) [@Bryan\_So](https://discourse.julialang.org/u/Bryan_So)\
**Post date:** [July 9, 2020, 3:13am UTC](https://discourse.julialang.org/t/how-to-prevent-ctrl-c-from-terminating-a-program-during-readline/42687/4 "2020-07-09T03:13:47Z")

</div>

Thanks! Looking forward to using it.
