控制 - Java 中的 C 异常

发布于 2024-08-28 02:12:08 字数 111 浏览 6 评论 0原文

我需要捕获该异常,但我不知道它是哪一个。我现在使用的 IDE 不允许这样的程序中断。

我知道如何使用 try/catch,但我实际上不知道我要捕获什么。

任何人都可以帮助我吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

宁愿没拥抱 2024-09-04 02:12:08

我似乎记得 CTRL-C 变成了 InterruptedException 并且这篇 文章似乎支持这一点。正如其他答案中所建议的,使用 addShutdownHook 是一个好方法,除非您想阻止关闭。

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.

浅沫记忆 2024-09-04 02:12:08

听起来你需要 Runtime.addShutdownHook()

注册一个新的虚拟机
关闭挂钩。

Java 虚拟机关闭
对两种事件的响应:

  • 当最后一个非守护线程退出或
    当退出时(相当于
    System.exit) 方法被调用,或者

  • 虚拟机响应用户而终止
    中断,例如键入 ^C,或
    系统范围的事件,例如用户注销
    或系统关闭。

我不确定您是否真的可以捕获它,您只需提供一个在虚拟机关闭时运行的Thread即可。我认为一旦开始你就无法阻止它。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文