【问题标题】:Java - throws not working for a subtype of an exception that is handledJava - 抛出不适用于处理的异常的子类型
【发布时间】:2017-08-23 05:43:23
【问题描述】:

以此为例:

public class TestClass {

    public static void test() throws SSLHandshakeException {
        throw new SSLHandshakeException("I'm a SSL Exception");
    }

    public static void main(String[] args) throws SSLHandshakeException {
        try {
            test ();        
        } catch (IOException ioe) {
            System.out.println("I`m handling IO exception");
        }       
    }
}

所以,我有我的测试方法,我只是抛出一个SSLHandshakeException,它是IOException 的子类型。

输出是“我正在处理 IO 异常”。

为什么会这样?我预计我的方法会抛出SSLHandshakeException。有没有规定catchthrows 更重要?

我只是想避免使用

try {
   test ();     
} catch (SSLHandshakeException se) { 
   throw se; 
} catch (IOException ioe) {
   System.out.println("I`m handling IO exception");
}   

因为我认为它不太可读

【问题讨论】:

    标签: java exception try-catch handle throws


    【解决方案1】:

    有没有一条规则是接球比投球更重要?

    它们是两个完全不同的东西。 throws 关键字是 方法签名 的一部分,表示“此方法 抛出此异常,因此每个调用者都应明确处理。”
    该方法是否实际抛出该异常在这里无关紧要。

    至于catch 声明。 SSLHandshakeException 是一个 IOException,因此按预期捕获。

    要获得您想要的行为,您可以编写:

        try {
            test ();
        } catch (SSLHandshakeException sslExc) {
            throw sslExc;
        } catch (IOException ioe) {
            System.out.println("I`m handling IO exception that is not an SSLHandshakeException");
        }
    

    编辑:你说你觉得这不太可读。但老实说,这只是最好的方法。如果它的行为方式与您建议的方式相同,那么您将永远无法在可能会抛出它的方法中捕获 SSLHandshakeException ?如果你想在某些条件下抓住它,但把它扔到其他条件下怎么办?这太局限和不直观。

    另一种选择是这样的;但在我看来,这更不可读:

        try {
            test ();
        } catch (IOException ioe) {
            if(ioe instanceof SSLHandshakeException)
                throw ioe;
            System.out.println("I`m handling IO exception that is not an SSLHandshakeException");
        }
    

    【讨论】:

    • 好的,我现在明白了。问题是我需要我的主要方法(在​​这种情况下)知道如何处理 SSLHandshakeException 以外的 IOExceptions,并将对此的责任传递给其他调用方法。
    • 我根据您对问题的编辑进行了编辑。如果您需要捕获的异常是 IOException 的 other 子类,那将是理想的。然后你可以抓住那些,ssl 异常就会通过。否则这是要走的路。
    【解决方案2】:

    SSLHandshakeExceptionjavax.net.ssl.SSLException 的子类,它是 java.io.IOException 的子类。

    所以这段代码:

    public static void main(String[] args) throws SSLHandshakeException {
         try {
              test ();        
             } catch (IOException ioe) {
                System.out.println("I`m handling IO exception");
            }       
    }
    

    会捕获 IOException,从而打印消息“我正在处理 IO 异常”。

    【讨论】:

      【解决方案3】:

      这可能是因为您打印了自定义字符串而不是异常消息。试试这个:

      public static void main(String[] args) throws SSLHandshakeException {
          try {
              test ();        
          } catch (IOException ioe) {
              System.out.println("I`m handling IO exception");
              System.out.println(ioe.getMessage());
      
          }       
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-02-21
        • 1970-01-01
        • 1970-01-01
        • 2018-03-23
        • 2019-07-19
        • 2011-02-07
        • 2013-07-24
        相关资源
        最近更新 更多