Control - C exception in Java
I need to catch that exception but I can't figure out which one it is. The IDE i'm开发者_运维百科 using right now doesn't allow for a program interrupt that way.
I know how to user try/catch, but I don't actually know what I'm trying to catch..
Can anyone help me with this?
I seem to recall that CTRL-C becomes an InterruptedException and this article seems to support that. As suggested in other answers the use of addShutdownHook
is a good way to go, unless you want to prevent the shutdown.
Sounds like you need Runtime.addShutdownHook()
Registers a new virtual-machine shutdown hook.
The Java virtual machine shuts down in response to two kinds of events:
The program exits normally, when the last non-daemon thread exits or when the exit (equivalently, System.exit) method is invoked, or
The virtual machine is terminated in response to a user interrupt, such as typing ^C, or a system-wide event, such as user logoff or system shutdown.
I'm not sure if you can actually catch it as such, you just supply a Thread
to be run when the VM is shutting down. I don't think you can stop it once this kicks off.
addShutdownHook() is what you want.
http://www.esus.com/javaindex/j2se/jdk1.2/javalang/trapctrlc.html
精彩评论