【问题标题】:Java, try...catch in init or runJava,尝试...在 init 中捕获或运行
【发布时间】:2013-07-26 18:26:09
【问题描述】:

当我添加一个 throw Exception 来运行或初始化时,我有一个错误提示:

ray.java:115: error run() in xray cannot implement run() in Runnable

public void run() throws Exception(

          ^ 

overriden method does not throw Exception 

1 error

我需要 try{..}catch 以获取 URL 作品。 try{...}catch 工作需要抛出异常。

【问题讨论】:

  • 彻底阅读覆盖的概念。

标签: java url


【解决方案1】:

编译错误是正确的。您不能将该方法声明为引发已检查异常。而是在方法中捕获异常。

@Override
public void run() {
    try {
       // invoke methods, etc
    } catch (SpecificException1 se1) {
      // error handling specific to SpecificException1  
    } catch (SpecificException2 se2) {
      // error handling specific to SpecificException2 
    }
    ...
}

按顺序处理特定异常,以便对其进行相应处理。

【讨论】:

    【解决方案2】:

    由于run()的原始签名没有抛出任何异常,所以不能抛出已检查异常。


    根据经验:

    1. 如果超级方法不抛出异常,则被覆盖的方法只能:
      • 不要抛出任何异常。
      • 抛出未经检查的异常。
    2. 如果超级方法抛出未经检查的异常,则类似于案例1:
      • 不要抛出任何异常。
      • 抛出未经检查的异常。
    3. 如果超级方法抛出检查异常,则被覆盖的方法只能:
      • 不要抛出任何异常。
      • 抛出未经检查的异常。
      • 抛出已检查异常,但它应该与父类或子类抛出的异常相同。

    【讨论】:

      【解决方案3】:

      Runnable.run() 不会引发检查异常。因此,您不能实现run() throws Exception,因为它会违反Runnable 的合同,抛出意外异常

      interface Runnable {
        // guarantees no checked exception is thrown
        public void run();
      }
      
      class Foo implements Runnable {
        @Override
        public void run() throws Exception {} // violates the guarantee
      }
      

      可以做的事情通常是相反的(虽然不适用于Runnable):

      interface Foo {
        // Exception might be thrown, but does not have to
        public void bar() throws Exception;
      }
      
      class FooImpl implements Foo {
        // FooImpl does not throw exception, so you can omit
        // the throws; it does not hurt if consumer expect an
        // exception that is never thrown
        @Override
        public void bar();
      }
      

      解决您的实施问题,您要么必须捕获并处理异常(很好的解决方案),要么将其包装到运行时异常中(不太好,但不时完成)。运行时异常不需要在方法签名中声明:

      class Foo implements Runnable {
        @Override
        public void run() {
          try {
          } catch (Exception e) {
            // either handle it properly if you can, or ...
            throw new RuntimeException(e);
          }
        }
      }
      

      【讨论】:

        【解决方案4】:

        试试这个..你不能委派来自run()的错误

        public void run(){
            try{
                  //something which CAN throw an exception
            }catch(Exception e){
                  e.printStackTrace();
            }
          }
        

        【讨论】:

          【解决方案5】:

          void run() from Runnable 接口不会抛出异常,即contract。如果您在运行中的代码抛出异常,要么立即处理它,要么让运行时将其向上传递。

          【讨论】:

            【解决方案6】:

            接口 Runnable 包含 void run(),因为它是这样定义的,所以不能通过向覆盖方法添加 throws Exception 来更改它。

            【讨论】:

              【解决方案7】:

              重写方法不得抛出比重写方法声明的新异常或更广泛的已检查异常。例如,声明 FileNotFoundException 的方法不能被声明 SQLException、Exception 或任何其他非运行时异常的方法覆盖,除非它是 FileNotFoundException 的子类。

              run() 的原始方法签名不会引发任何异常,因此您不能在类中覆盖它并开始引发异常。

              【讨论】:

                猜你喜欢
                • 2017-09-12
                • 1970-01-01
                • 1970-01-01
                • 2021-12-13
                • 1970-01-01
                • 1970-01-01
                • 2021-08-13
                • 2020-06-19
                • 1970-01-01
                相关资源
                最近更新 更多