Consider this seemingly simple code
SwingWorker w = new SwingWorker() {
@Override
protected Object doInBackground() throws Exception
{
throw new RuntimeException("Catch me");
}
};
w.execute();
Of course this throws an exception, right? In fact, it doesn't (well, it does but it never bubbles up so it would go completely unnoticed).
The way to ensure the exception is thrown is by calling get in the done method.
@Override
protected void done()
{
try
{
get();
}
catch (InterruptedException e)
{
throw new RuntimeException(e);
}
catch (ExecutionException e)
{
// throw the cause since the execution exception wraps the
// underlying exception
throw new RuntimeException(e.getCause());
}
}
It's an ugly solution though since the get method throws those checked exceptions but at least the exception gets thrown.
This is a very common problem when the swing worker is not returning any value and just performing a task, so you never call the get method.
Very good illustration, thanks.
ReplyDelete