rethrowing exception handled in preceding catch block
在iIC站点上写入(
In detail, in Java SE 7 and later, when you declare one or more
exception types in a catch clause, and rethrow the exception handled
by this catch block, the compiler verifies that the type of the rethrown exception meets the following conditions:-The try block is able to throw it.
-There are no other preceding catch blocks that can handle it.
-It is a subtype or supertype of one of the catch clause's exception parameters.
请注意第二点(
研究以下代码:
1 2 3 4 5 6 7 8 9 | static private void foo() throws FileNotFoundException { try { throw new FileNotFoundException(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { throw e; } } |
这段代码编译得很好。根据我的观点,在阅读了上述文章的引言后,我希望看到编译器会验证它,并且我会得到编译器错误。
第二点我理解错了吗?
这是非常好的,因为
编辑:
1 2 3 4 5 6 7 8 9 | static private void foo() throws FileNotFoundException { try { throw new FileNotFoundException(); } catch (IOException e) { e.printStackTrace(); } catch (FileNotFoundException e) { throw e; } } |