ã²ã¨ã¤åã®è¨äºã§ç´¹ä»ãã Java SE 7 and JDK 7 Compatibility - Oracle ã®ãªãã«ãé¢ç½ãéäºææ§ãããã¾ããã
Synopsis: Improved Exception Handling May Cause source Incompatibility
æ¦è¦: ä¾å¤ãã³ããªã³ã°ã®æ¹åãåå ã®ã½ã¼ã¹éäºææ§ Java SE 7 and JDK 7 Compatibility
ã©ãããããã¨ï¼
ã¾ãã¯åæã«ãªã話ããã
Java7 ã§å¤æ´ãããè¨èªä»æ§ã« Rethrowing Exceptions with More Inclusive Type Checking(例外再スロー時のより包含的な型チェック) ã¨ãããã®ãããã¾ãã
ãã®å¤æ´ã«ãã£ã¦ãJava7 ã ã¨ä»¥ä¸ã®ãããªã³ã¼ããã³ã³ãã¤ã«ã§ããããã«ãªãã¾ããã*1
static class FirstException extends Exception { } static class SecondException extends Exception { } public void rethrowException(String exceptionName) throws FirstException, SecondException { try { if (exceptionName.equals("First")) { throw new FirstException(); } else { throw new SecondException(); } } catch (Exception e) { throw e; } }
ãã®ä¾ã§ã¯ãFirstException 㨠SecondException ãã¾ã¨ãã¦å¦çããããã«ããã®ä¸ä½ã®åã§ãã Exception ã§ãã£ãããã¦ãã¾ãã
ãã®ããã§ããã® Exception ããã®ã¾ã¾å¼ã³å
ã®ã¡ã½ããã«åã¹ãã¼( throw e; )ãã¦ãã¾ãã
ãã®ã¨ããåã¹ãã¼ãã e ã®å㯠Exception ãªã®ã§ãã¡ã½ããã¨ãã¦ã¯ throws Exception ã¨å®£è¨ãã¦ããå¿
è¦ãããã¾ããã
ãã ãçºçããå¯è½æ§ã®ãããã£ããä¾å¤ã¯ FirstException ã SecondException ãªã®ã«ãå¼ã³å
ã§ã¯ Exception ã«ãªã£ã¦ããã®ã¯ä¸èªç¶ã§ãã
ããã§ãJava7 ã§ã¯ catch(Exception e) ã¨å®£è¨ããã¦ãã¦ããthrow e; ã§ã¯å®éã« try æã®ä¸ã§çºçããå¯è½æ§ã®ãããã£ããä¾å¤ã®åããã®ã¾ã¾å¼ãç¶ãããã«ãªããthrows FirstException, SecondException ã¨å®£è¨ã§ããããã«ãªãã¾ããã
å¤æ´ã«ããå¼å®³
ä¸æ¹ã§ããã®è¨èªä»æ§ã®å¤æ´ã«ã¯å¼å®³ãããã¾ããã
ãããå
ã»ã©ã®è©±ã§ãJava6 ã§ã¯ã³ã³ãã¤ã«ã§ãã¦ããã³ã¼ãããJava7 ã§ã³ã³ãã¤ã«ã§ããªããªãã¨ããéäºæã§ãã
Description: The following example shows two source incompatibilities:
class Foo extends Exception {}
class SonOfFoo extends Foo {}
class DaughterOfFoo extends Foo {}
...
try {
throw new DaughterOfFoo();
} catch (final Foo exception) {
try {
throw exception; // used to throw Foo, now throws DaughterOfFoo
} catch (SonOfFoo anotherException) {
// Reachable?
}
}
The first incompatibility is that the throw exception; statement throws a Foo exception in JDK 6, but throws a DaughterOfFoo exception in Java SE 7.
The second incompatibility is that the catch (SonOfFoo ...) statement compiled under JDK 6 but, under Java SE 7, gets the following error:
error: exception SonOfFoo is never thrown in body of corresponding try statement
Such code is likely to be rare, but should be fixed by removing the non-reachable statements.
æ¦è¦: 以ä¸ã®ä¾ã¯ã2ã¤ã®ã½ã¼ã¹ã®äºææ§ã®åé¡ã示ãã¦ãã¾ãã
ï¼ä¸ç¥ï¼
1ã¤ç®ã®éäºææ§ã¯ãthrow exception; æã JDK 6 ã 㨠Foo exception ãã¹ãã¼ããã®ã«ãJava SE 7 ã 㨠DaughterOfFoo exception ãã¹ãã¼ããã¨ãããã¨ã§ãã
2ã¤ç®ã®éäºææ§ã¯ãcatch (SonOfFoo ...) æã JDK 6 ã ã¨ã³ã³ãã¤ã«ã§ããã®ã«ãJava SE 7 ã ã¨æ¬¡ã®ã¨ã©ã¼ã«ãªãã¨ãããã¨ã§ãã
ã¨ã©ã¼: ä¾å¤SonOfFooã¯å¯¾å¿ããtryæã®æ¬ä½ã§ã¯ã¹ãã¼ããã¾ãã
ãã®ãããªã³ã¼ãã¯ç¨ã§ããå¯è½æ§ãé«ãã§ãããéå°éå¯è½ãªæãåé¤ãããã¨ã§ä¿®æ£ãããã¹ãã§ãã
Java SE 7 and JDK 7 Compatibility
ã throw exception; ãã¨ããæã§ãexception ã®å㯠Java 6 ã ã¨å®£è¨éã Foo ã ã£ãã®ã«ãJava 7 ã 㨠DaughterOfFoo ã¨ããåã¨ã¿ãªãããããã«ãªãã¾ããã
ãããªãã¨ãDaughterOfFoo ã¨ç¶æ¿é¢ä¿ã®ãªã SonOfFoo ãçºçãããã¨ã¯ãªãã®ã§ããããcatch (SonOfFoo anotherException) ã¨ããããã«ãã£ãããããã¨ãã¦ãå°éä¸è½ã§ã¨ã©ã¼ã«ãªããã¨ãããã¨ã ããã§ãã
ã¨ã¯ãããã¨ã©ã¼ã«ãªã£ã¦ããåæã¯ãã¨ãã¨å°éä¸è½ãªã³ã¼ããªã®ã§ãæ¶ãã¦ãã¾ã£ã¦åé¡ã¯ãªãã¯ãã§ãã
ãªã®ã§ãã½ã¼ã¹ã³ã¼ãéäºæã§ããããã®ã¾ã¾ã«ãªã£ãããã§ãã
ã»ãã«ãâ¦
ã³ã³ãã¤ã©ã®ãã°ãç´ããããã§ã³ã³ãã¤ã«ã§ããªããªããã¿ã¼ã³ãããããã§ã*2ã
èªãã§ãã¦ãè¨èªä»æ§ã®åå¼·ã«ãªãã¾ããï½
*1:Java6 ã ã¨ã³ã³ãã¤ã«ã¨ã©ã¼ã§ãã
*2:ã¨ãã£ã¦ãããã¡ããç¨ãªãã¿ã¼ã³ã ã¨æãã¾ãã