java – 拦截异常

我想使用自定义异常,以便在发生任何排序异常时出现用户友好的消息.

这样做的直接方式是什么?我是否应该采取额外的预防措施来避免干扰Swing的EDT?

解决方法

例外翻译:

最好不要使用对最终用户没有意义的消息来污染您的应用程序,而是创建有意义的异常和消息,这些异常和消息将转换在应用程序实现深处发生的异常/错误.

根据@ Romain的评论,您可以使用Exception(Throwable cause)构造函数来跟踪较低级别的异常.

从Effective Java第2版,第61项:

[…] higher layers should catch
lower-level exceptions and,in their
place,throw exceptions that can be
explained in terms of the higher-level
abstraction. This idiom is kNown as
exception translation:

// Exception Translation
    try {
         // Use lower-level abstraction to do our bidding
         ...
    } catch(LowerLevelException e) {
         throw new HigherLevelException(...);
    }

相关文章

最近看了一下学习资料,感觉进制转换其实还是挺有意思的,尤...
/*HashSet 基本操作 * --set:元素是无序的,存入和取出顺序不...
/*list 基本操作 * * List a=new List(); * 增 * a.add(inde...
/* * 内部类 * */ 1 class OutClass{ 2 //定义外部类的成员变...
集合的操作Iterator、Collection、Set和HashSet关系Iterator...
接口中常量的修饰关键字:public,static,final(常量)函数...