本文介绍了Swing Worker中的优雅异常处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我正在通过Swing Worker类在应用程序中使用线程。它工作正常,但我对于在try-catch块中显示错误消息对话有一个不好的感觉。它可能会阻止应用程序吗?这是现在的样子: SwingWorker< Void,Void> worker = new SwingWorker< Void,Void>(){ //在后台线程中执行 public Void doInBackground(){ try { DoFancyStuff(); } catch(Exception e){ e.printStackTrace(); String msg = String.format(意外问题:%s,e .toString()); // TODO:在后台线程中执行,应在EDT中执行? JOptionPane.showMessageDialog(Utils.getActiveFrame(), msg,Error,JOptionPane.ERROR_MESSAGE, errorIcon); } // END:try-catch return null; } //在事件调度线程中执行 public void done(){ System.out.println(Done); } }; 可以使用Swing Worker框架以安全的方式完成吗?在这里覆盖publish()方法是一个很好的领导者。 编辑: 这样做: } catch(final Exception e){ SwingUtilities.invokeLater(new Runnable(){ public void run(){ e.printStackTrace(); String msg = String.format(意外问题:%s,e.toString()); JOptionPane.showMessageDialog(Utils .getActiveFrame(),msg,Error, JOptionPane。 ERROR_MESSAGE,errorIcon); } }); } 调用get方法将导致两次try-catch块,因为计算部分抛出异常,所以我认为这是最干净的。解决方案一个选项是使用 SwingUtilities.invokeLater (...) 在 EDT上发布操作 SwingUtilities.invokeLater(new Runnable(){ @Override public void run(){ JOptionPane.showMessageDialog( Utils.getActiveFrame (), msg,错误, JOptionPane.ERROR_MESSAGE, errorIcon); } }); 正如你所说, SwingWorker 能够报告中间结果,但您需要覆盖 处理(.. 。) ,当您调用 发布(...) 。 无论如何,如果发生异常,为什么不设置标志,如果设置了该标志,请在 done()因为它在 EDT 中安全执行? I am using threading in application through Swing Worker class. It works fine, yet I have a bad feeling about showing an error message dialog in try-catch block. Can it potentially block the application? This is what it looks right now:SwingWorker<Void, Void> worker = new SwingWorker<Void, Void>() { // Executed in background thread public Void doInBackground() { try { DoFancyStuff(); } catch (Exception e) { e.printStackTrace(); String msg = String.format("Unexpected problem: %s", e .toString()); //TODO: executed in background thread and should be executed in EDT? JOptionPane.showMessageDialog(Utils.getActiveFrame(), msg, "Error", JOptionPane.ERROR_MESSAGE, errorIcon); }//END: try-catch return null; } // Executed in event dispatch thread public void done() { System.out.println("Done"); }};Can it be done in a safe way using Swing Worker framework? Is overriding publish() method a good lead here?EDIT:Did it like this:} catch (final Exception e) { SwingUtilities.invokeLater(new Runnable() { public void run() { e.printStackTrace(); String msg = String.format( "Unexpected problem: %s", e.toString()); JOptionPane.showMessageDialog(Utils .getActiveFrame(), msg, "Error", JOptionPane.ERROR_MESSAGE, errorIcon); } });}Calling get in done method would result in two try-catch blocks, as the computational part throws exceptions, so I think this is cleaner in the end. 解决方案 One option is to use SwingUtilities.invokeLater(...) to post the action on the EDTSwingUtilities.invokeLater(new Runnable(){ @Override public void run(){ JOptionPane.showMessageDialog( Utils.getActiveFrame(), msg, "Error", JOptionPane.ERROR_MESSAGE, errorIcon); }});And as you noted, SwingWorker is capable of reporting intermediate results, but you'll need to override process(...), which is called when you invoke publish(...).Regardless, why not just set a flag if an exception occurs, and if that flag is set, show the dialog in done() since it's executed safely in the EDT? 这篇关于Swing Worker中的优雅异常处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-29 05:20