In my program I made an assert - which evaluates to false - from a runnable, but never see any console output about the assert. I want to know if my asserts are false, but it seems the runnable is catching all the thrown asserts?
Below is the simplest example program I could write to demonstrate. (Assertions are enabled. The program would behave differently if they weren't and print both lines instead of just the one). The output of the program is.
About to assert False
That's it. After that the assert statement throws and is caught by something and I never know about it. I want to know about it, what am I doing wrong?
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");
}
}
UncaughtExceptionHandler
associated with your thread. Have you tried setting a custom one, to see if you can see the assertion exception pass by? – ToiletryThread
s for you without giving you access to them (afaik) and you need to put anUncaughtExceptionHandler
on theThread
instance. – CrumThread
is aRunnable
, so you could create your ownThread
implementation with your code, and associate a customUncaughtExceptionHandler
to this thread, then use it as a parameter when calling the executor. Don't know if it would make a difference. In any case, you have provided an answer which is probably way more efficient than my suggestion. – Toiletry