在Swing UI中处理异常(低级到高级和异常包装) [英] Handling exceptions in a Swing UI (low level to high level and exception wrapping)

查看:165
本文介绍了在Swing UI中处理异常(低级到高级和异常包装)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



下面是一个 actionPerformed 方法。 UiUtils#showError 显示一个JOptionPane,并显示/隐藏堆栈跟踪。 ApplicationException是将低级异常转换为用户将理解的东西的自定义类。



一个问题是,我不知道如何处理 NullPointerException ,如果用户在此代码之前没有在 JFileChooser 中选择一个文件,则会传播。 exportData 方法有目的地检查条目上的空值,因此没有文件处理完成。



此外,似乎它将是一个很好的方法来包装一个低级别的异常(例如从数据层)。我想修改 ApplicationException 保留一个实例,而不是在 showError 方法中执行此操作。



最后,有一件事困扰着我,这就是几个异常可能同时发生的可能性。我不知道如何处理这个,所以我可以接受任何建议。

  try {
dataService.exportData (list,selectedFile);
} catch(IOException e){
UiUtils.showError(new ApplicationException(Input / Ouput error),e);
} finally {

if(list == null){
UiUtils.showError(new ApplicationException(No data to export),null);
}

if(selectedFile == null){
UiUtils.showError(new ApplicationException(No file selected),null);
}
}


解决方案

捕获NullPointerException(或任何RuntimeException)以及您的IOExceptionjust捕获最常见的异常类型:

  try {
dataService.exportData(list,selectedFile);
} catch(Exception e){
UiUtils.showError(new ApplicationException(e),e);
}终于{
...
}

你可以将原始异常包装到ApplicationException中,将其作为cause参数添加到构造函数中,例如:

  public class ApplicationException extends RuntimeException {

public ApplicationException(异常原因){
super(cause);
}

...
}

那么原始的例外可以通过getCause()方法总是可用。



如果你的程序不是多线程,我不用担心同时发生的异常。

I wish to handle Exceptions in a meaningful way in a Swing application.

The following is inside an actionPerformed method. The UiUtils#showError displays a JOptionPane with a button that shows/hides the stack trace. ApplicationException is a custom class to translate low level Exceptions to something a user would understand.

One issue is that I'm not sure how to handle a NullPointerException that propagates up if a user doesn't select a file in the JFileChooser before this code. The exportData method purposefully checks for null on entry so no file handling is done.

Also, it seems that it would be a good approach to wrap a low level Exception (from the data layer for example). I'd like to modify ApplicationException to keep an instance instead of doing this in the showError method.

Finally, there's one thing that's bothering me and that's the possibility that a few Exceptions could happen simultaneously. I have no idea how to handle this so I'm open to any suggestions.

try {
    dataService.exportData(list, selectedFile);
} catch (IOException e) {
    UiUtils.showError(new ApplicationException("Input/Ouput error"), e );
} finally {

    if( list == null){
        UiUtils.showError(new ApplicationException("No data to export"), null );
    }

    if( selectedFile == null ){
        UiUtils.showError(new ApplicationException("No file selected"), null );
    }       
}

解决方案

To catch NullPointerException (or any RuntimeException for that matter) as well as your IOExceptionjust catch the most general exception type:

try {
   dataService.exportData(list, selectedFile);
} catch (Exception e) {
   UiUtils.showError(new ApplicationException(e), e );
} finally {
  ...
}

You can wrap the original exception into your ApplicationException by adding it as a "cause" parameter to the constructor, e.g.:

public class ApplicationException extends RuntimeException {

   public ApplicationException(Exception cause) {
       super(cause);
   }

   ...
}

Then the original exception would be always available via the getCause() method.

If you program is not multithreaded, I wouldn't worry about simultaneous exceptions.

这篇关于在Swing UI中处理异常(低级到高级和异常包装)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆