无法为@override方法引发异常

时间:2020-02-20 06:25:52

标签: java android appium guice

@Override
protected void configure() throws IOException{

        Properties props = new Properties();
        props.load(new FileInputStream(System.getProperty("user.dir")+"/src/properties/android.properties"));
        Names.bindProperties(binder(), props);


}

我无法为上述方法抛出任何异常。我收到错误删除抛出异常。这是Guice模块的实现。

2 个答案:

答案 0 :(得分:2)

您无法更改Module界面,但是可以通过Guice以多种方式解决该问题。

您可以包装异常:

try (InputStream in = new FileInputStream(System.getProperty("user.dir")+"/src/properties/android.properties")) {
   Properties props = new Properties();
    props.load(in);
    Names.bindProperties(binder(), props);
} catch (IOException e) {
  // Yes, it's hard to create Guice exceptions. It's just like that.
  Message message = new Message(List.of(this), "Unable to load properties file", e);
  throw new ConfigurationException(List.of(message));
}

您可以将文件加载移出模块:

class MyModule extends AbstractModule {
  private final Properties properties;
  MyModule(Properties properties) {
    this.properties = properties;
  }
  @Override protected void configure() {
    Names.bind(binder(), props);
  }
}

并像这样创建您的模块。通常,您应该能够将异常抛出到模块实现之外。

Properties properties = new Properties();
try (InputStream in = FileInputStream(System.getProperty("user.dir")+"/src/properties/android.properties")) {
  properties.load(in);
}
Module module = new MyModule(properties);

答案 1 :(得分:1)

您是否在父方法上添加了相同的IOException异常?

如果您重写该方法,并且原始方法签名没有Throws IOException,则它将无法正常工作。

interface A {

  void A() throws Exception;

}

class B implements A{

  @override
  public void A() throws Exception{
  }

}

以上格式将有效。

相关问题