Title says it all. How can I handle or catch a SIGINT in julia? From the docs I assumed I just wanted to catch InterruptException using a try/catch block like the following
try
while true
println("go go go")
end
catch ex
println("caught something")
if isa(ex, InterruptException)
println("it was an interrupt")
end
end
But I never enter the catch block when I kill the program with ^C.
edit: The code above works as expected from the julia REPL, just not in a script.
I see the same behavior as alto, namely that
SIGINTkills the entire process when my code is run as a script but that it is caught as an error when run in the REPL. My version is quite up to date and looks rather similar to that of tholy:Digging through the source, I found hints that
Julia'sinterrupt behavior is determined by anjl_exit_on_sigintoption, which can be set via accall.jl_exit_on_sigintis0for the REPL, but it looks as ifinit.csets it to1when running aJuliaprogram file from the command line.Adding the appropriate
ccallmakes alto's code works regardless of the calling environment:This does seem to be a bit of a hack. Is there a more elegant way of selecting the interrupt behavior of the environment? The default seems quite sensible, but perhaps there should be a command line option to override it.