(下面的示例代码是独立的,可运行的,可以尝试一下,它不会崩溃您的系统:)
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是否重新启动?
发布评论