gpt4 book ai didi

Java 断言从可运行对象调用时不发送到控制台

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:26:12 27 4
gpt4 key购买 nike

在我的程序中,我从一个可运行的程序中创建了一个断言——它的计算结果为假,但从未看到任何关于该断言的控制台输出。我想知道我的断言是否为假,但似乎 runnable 正在捕获所有抛出的断言?

下面是我可以编写的最简单的示例程序来演示。(断言已启用。如果未启用断言,程序将表现不同,并打印两行而不是仅打印一行)。程序的输出是。

即将断言为假

就是这样。在那之后,断言语句抛出并被某些东西捕获,我从来不知道。我想知道,我做错了什么?

import java.nio.ByteBuffer;
import java.util.concurrent.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

import java.awt.FlowLayout;
import javax.swing.*;

class App
{
private static final ScheduledExecutorService sExecutor =
Executors.newSingleThreadScheduledExecutor();

// Main
public static void main(String[] args)
{
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() { createAndShowGUI(); } });

}

// Swing GUI
private static void createAndShowGUI()
{
// Just create a swing thing. Boring
JFrame frame = new JFrame("Title String");
JLabel label = new JLabel("Hello World");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(label);
frame.getContentPane().setLayout(new FlowLayout());
frame.pack();
frame.setVisible(true);

// ********************************************
// INTERESTING CODE HERE. We schedule a runnable which assert's false
// but we never see a console assert error!
// ********************************************
sExecutor.schedule(new Runnable()
{ @Override public void run() { doAssertFalse(); }}, 0, TimeUnit.SECONDS);

}

public static void doAssertFalse()
{
System.out.println("About to assert False");
assert false;
System.out.println("Done asserting False");
}
}

最佳答案

这样做就可以了:

private static final ScheduledExecutorService sExecutor =
Executors.newSingleThreadScheduledExecutor();

// Main
public static void main(String[] args)
{
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
try {
createAndShowGUI();
} catch (ExecutionException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
} });

}

// Swing GUI
private static void createAndShowGUI() throws ExecutionException, InterruptedException {
// Just create a swing thing. Boring
JFrame frame = new JFrame("Title String");
JLabel label = new JLabel("Hello World");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(label);
frame.getContentPane().setLayout(new FlowLayout());
frame.pack();
frame.setVisible(true);

// ********************************************
// INTERESTING CODE HERE. We schedule a runnable which assert's false
// but we never see a console assert error!
// ********************************************
ScheduledFuture<?> future = sExecutor.schedule(new Runnable() {
@Override
public void run() {
doAssertFalse();
}
}, 0, TimeUnit.SECONDS);
future.get();

}

public static void doAssertFalse()
{
System.out.println("About to assert False");
assert false;
System.out.println("Done asserting False");
}

请注意,我正在将 schedule 的结果保存到 ScheduledFuture 变量中。在您将来调用 get() 方法之前,不会返回异常。所有异常都包含在 ExecutionException 中抛出。

不幸的是,这会阻塞,所以另一种获取异常的方法是这样的:

// Swing GUI
private static void createAndShowGUI() {
// Just create a swing thing. Boring
JFrame frame = new JFrame("Title String");
JLabel label = new JLabel("Hello World");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(label);
frame.getContentPane().setLayout(new FlowLayout());
frame.pack();
frame.setVisible(true);

// ********************************************
// INTERESTING CODE HERE. We schedule a runnable which assert's false
// but we never see a console assert error!
// ********************************************
sExecutor.schedule(new Runnable() {
@Override
public void run() {
try {
doAssertFalse();
} catch (Error e) {
e.printStackTrace();
}
}
}, 0, TimeUnit.SECONDS);

}

public static void doAssertFalse() {
System.out.println("About to assert False");
assert false;
System.out.println("Done asserting False");
}

请注意,我捕获的是错误,而不是异常。我这样做是因为断言抛出 java.lang.AssertionError,而不是 *Exception。

我很难在 Javadoc 中找到任何说明 ScheduledExecutorService 吞下异常的文档,除非您执行这些操作,但通过我的测试似乎是这样。

关于Java 断言从可运行对象调用时不发送到控制台,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14344901/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com