将检查的异常包装到Java中的未检查的异常中? [英] Wrapping a checked exception into an unchecked exception in Java?

查看:279
本文介绍了将检查的异常包装到Java中的未检查的异常中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  public static属性getConfigFactory() if(config == null){
InputStream in = Class.forName(PACKAGE_NAME).getResourceAsStream(CONFIG_PROP);
config = new Properties();
config.load(in);
}
return config;
}

我想将两个检查的异常转换为未检查的异常。这个最好的方法是什么?



我应该捕获异常,并使用捕获的异常作为内部异常抛出一个新的RuntimeException?



有没有更好的方法来做到这一点,还是应该尝试这样做呢?



编辑:

只是为了澄清。这些例外将是致命的,因为配置文件本质上是对程序的操作,所有的异常将被捕获并记录在我程序的顶层。



我的目的是避免不必要的抛出异常,异常添加到调用我工厂的每个方法的签名。

解决方案

A <$只有当客户端无法从任何问题中恢复时,才应使用C $ c> RuntimeException 。做一些你正在谈论的事情有时候是适当的,但更常见的是不合适。



如果你使用JDK> = 1.4,那么你可以做一些事情如:

 
try {
//可能抛出异常的代码
} catch(IOException e){
抛出新的RuntimeException(e);
} catch(ClassNotFoundException e){
throw new RuntimeException(e);
}

和重新引导的 RuntimeException 将包含原始原因在里面。通过这种方式,线程顶端的某个人捕获 RuntimeException - 您的线程会抓住 RuntimeException ,这样他们就不会只是默默地死,对吧? - 至少可以打印出完整的堆栈跟踪的原因。



但是,正如其他人所说,并会说,异常被检查的原因。只有当您积极的情况下,您的客户无法从您重新发布的问题中恢复为未经检查的异常。



注意:优于 RuntimeException 将使用更具体的未检查异常(如果有)。例如,如果您的方法可以抛出 ClassNotFoundException 的唯一原因是因为配置文件丢失,您可以重新抛出$ code MissingResourceException ,这是一个未经检查的异常,但提供了更多关于为什么抛出它的信息。其他好的 RuntimeException s使用如果他们描述你正在重新推出的问题是 IllegalStateException TypeNotPresentException UnsupportedOperationException



另请注意,它始终是一个好主意,捕获RuntimeException和至少记录它。至少这样你就明白为什么你的线程要走了。


I have this factory method in java:

public static Properties getConfigFactory() throws ClassNotFoundException, IOException {
    if (config == null) {
        InputStream in = Class.forName(PACKAGE_NAME).getResourceAsStream(CONFIG_PROP);
        config = new Properties();
        config.load(in);
    }
    return config;
}

And I want to transform the two checked exceptions into unchecked exceptions. What is the best way to go about this?

Should I just catch the exception and throw a new RuntimeException using the caught exception as the inner exception?

Is there a better way to do this or should I even be attempting to do this in the first place?

EDIT:
Just to clarify. These exceptions will be fatal, as the configuration file is essentially to the operation of the program and all exception will be caught and logged at the top level of my program.

My purpose is to avoid an unnecessary throws exception, exception added to the signature of every method that calls my factory.

解决方案

A RuntimeException should be used only when the client cannot recover from whatever the problem is. It is occasionally appropriate to do what you are talking about, but more often it is not appropriate.

If you are using a JDK >= 1.4, then you can do something like:

try {
  // Code that might throw an exception
} catch (IOException e) {
  throw new RuntimeException(e);
} catch (ClassNotFoundException e) {
  throw new RuntimeException(e);
}

and the rethrown RuntimeException will have the original cause included inside it. This way, someone at the top of the thread catching the RuntimeException -- your threads do catch RuntimeException so they don't just silently die, right? -- can at least print out the FULL stack trace of the cause.

But as others have said and will say, exceptions are checked for a reason. Only do this when you are positive that your clients cannot recover from the problem that you are rethrowing as an unchecked exception.

NOTE: Better than just RuntimeException would be to use a more specific unchecked exception if one is available. For example, if the only reason your method could throw a ClassNotFoundException is because a configuration file is missing, you could rethrow a MissingResourceException, which is an unchecked exception but gives more information about why you are throwing it. Other good RuntimeExceptions to use if they describe the problem you are rethrowing are IllegalStateException, TypeNotPresentException and UnsupportedOperationException.

Also note that it is ALWAYS a good idea for your threads to catch RuntimeException and at a minimum log it. At least this way you understand why your threads are going away.

这篇关于将检查的异常包装到Java中的未检查的异常中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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