当抛出异常时,EDT是否重新启动?

编程入门 行业动态 更新时间:2024-10-25 18:31:06
本文介绍了当抛出异常时,EDT是否重新启动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

(下面的示例代码是独立的,可运行的,可以尝试一下,它不会崩溃您的系统:)

Tom Hawtin在这里提出了一个问题:为什么人们在事件队列中运行Java GUI

Tom Hawtin commented on the question here: Why do people run Java GUI's on the Event Queue

EDT不太可能会崩溃。 EDT分派中抛出的未经检查的异常被捕获,转储并且线程继续。

有人可以解释我这里发生了什么(每次点击

Can someone explain me what is going on here (every time you click on the "throw an unchecked exception" button, a divide by zero is performed, on purpose):

import javax.swing.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; public class CrashEDT extends JFrame { public static void main(String[] args) { final CrashEDT frame = new CrashEDT(); frame.addWindowListener(new WindowAdapter() { public void windowClosing( WindowEvent e) { System.exit(0); } }); final JButton jb = new JButton( "throw an unchecked exception" ); jb.addActionListener( new ActionListener() { public void actionPerformed( ActionEvent e ) { System.out.println( "Thread ID:" + Thread.currentThread().getId() ); System.out.println( 0 / Math.abs(0) ); } } ); frame.add( jb ); frame.setSize(300, 150); frame.setVisible(true); } }

我收到以下消息(其中是我期望的):

I get the following message (which is what I'd expect):

Exception in thread "AWT-EventQueue-0" java.lang.ArithmeticException: / by zero

对我来说,这是一个未经检查的异常吗?

and to me this is an unchecked exception right?

您可以看到每次触发崩溃时,线程ID都会增加。

You can see that the thread ID is getting incremented every time you trigger the crash.

因此,每次抛出未经检查的异常时,EDT都会自动重新启动或者是未经检查的例外情况,如汤姆·夏婷(Tom Hawtin)评论的那样,被捕获,倾销,线程继续进行

So is the EDT automatically restarted every time an unchecked exception is thrown or are unchecked exceptions "caught, dumped and the thread goes on" like Tom Hawtin commented?

这里发生了什么? >

What is going on here?

推荐答案

为了参考,这个机器是实现依赖的。例如,我的平台上的线程ID保持不变。在 AWT线程问题 ,是指至少有一个可显示组件时JVM不会退出。

For reference, "The particular behavior of this machinery is implementation-dependent." For example, the thread ID remains unchanged on my platform. The net effect, discussed in AWT Threading Issues, is that "the JVM will not exit while there is at least one displayable component."

更多推荐

当抛出异常时,EDT是否重新启动?

本文发布于:2023-10-28 17:53:30,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1537355.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:重新启动   抛出   异常   EDT

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!