å°å ¥ãã代ããã«
ãã®èšäºã®èè ã§ããAlan Keefer 1ã¯ãä¿éºããžãã¹ãœãããŠã§ã¢äŒç€Ÿã§ããGuidewire Software 2ã®ãªãŒãã¢ãŒããã¯ãã§ãã 圌ã¯ãŸã äžçŽéçºè ã§ããããGosu 3èšèªã®äœæ¥ã«åå ããŸããã ç¹ã«ãAlanã¯èšèªãJavaãã€ãã³ãŒãã«ã³ã³ãã€ã«ããåé¡ã«å¯ŸåŠããŸããã
ãã®èšäºã¯2009幎ã«æžããããã®ã§ãJVMããŒãžã§ã³1.6ã§ã®try / catch /ã®å®è£ ã®è©³çŽ°ã«å°å¿µããŠããŸãã ãããèªãã«ã¯ãJavaæ§æã®åºæ¬çãªç¥èãå¿ èŠã§ãããŸããã·ãŒããã«ãããããŠãããã€ãã³ãŒãã®ç®çãç解ããå¿ èŠããããŸãã ãŸãããã®èšäºã®æåŸã«ã¯ãSCJPã®ããªãããŒãªã¿ã¹ã¯ã«äŒŒãäžé£ã®äŸããããŸãã
JVMå éš
çŸåšåãçµãã§ããããšã®1ã€ã¯ããããŒã ãèšèªãJavaãã€ãã³ãŒãã«ã³ã³ãã€ã«ããããšã§ãã ïŒåèïŒãã€çµäºãããã¯èšããŸãããããããã§ããå°æ¥ã®ãªãªãŒã¹ã«èœã¡ãŠããïŒãããããã®ã¯ãJVMã®å éšã調ã¹ãŠãããªãèªèº«ã®èšèªã®ãã¹ãŠã®ã¯ãœã®éãã³ãŒããŒãèŠã€ããããšã§ãã ãããããã¹ãŠã®ã楜ããããšéãã³ãŒããŒã®ã»ãšãã©ã¯ãtry / catch / finallyãªã©ã®æŒç®åããæ¥ãŠããŸãã ãããã£ãŠãä»åã¯ãå²åŠãã¢ãžã£ã€ã«ã«ã¯è§ŠããŸããã ãã®ä»£ããã«ãç§ã¯JVMãæãäžããŸããã»ãšãã©ã®å Žåãæãäžããå¿ èŠã¯ãããŸããïŒããããªãïŒã
2é±éåã«ã€ãã«ãããã¯ã«ã€ããŠå°ããããå Žåããããã®åŠçã¯JVMã«å®è£ ãããããšããå§ãããŸããããã¯èšèªã®åºæ¬çãªéšåã§ãããçµã¿èŸŒã¿ã§ããå¿ èŠããããŸãã ç§ãç¥ã£ããšãã®é©ããæ³åããŠãã ããïŒããããããã§ã¯ãããŸããã å®éãfinally-blocksã¯ãtry-ãŸãã¯é¢é£ä»ããããcatch-blocksã®åŸã«å¯èœãªãã¹ãŠã®å Žæã§åçŽã«çœ®ãæããããŸãã ãããã®ãããã¯ã¯ãcatchïŒThrowableïŒãã§ã©ãããããfinallyãããã¯ãçµäºãããšäŸå€ãã¹ããŒãããŸãã æ®ã£ãŠããã®ã¯ãäŸå€ããŒãã«ãããã£ãŠã眮æãããfinallyãããã¯ãã¹ããããããããã«ããããšã ãã§ãã ãŸãã©ãïŒ ïŒå°ããªèŠåïŒJVM 1.6ããåã§ã¯ãfinallyãªãã¬ãŒã¿ãŒã«ã€ããŠã¯ãå®å šãªçœ®æã®ä»£ããã«ãµãã«ãŒãã³ã䜿çšãããããã§ããããããããã§ã¯ãäžèšã®ãã¹ãŠãé©çšãããããŒãžã§ã³1.6ã«ã€ããŠèª¬æããŠããŸããïŒ
ãã®ã¢ãããŒããçã«ããªã£ãŠãããã©ãããç解ããã«ã¯ãå°ãå·»ãæ»ããŠãJVMãäŸå€ãåŠçããæ¹æ³ã確èªããŠãã ããã ãããã®åŠçã¯ãç¹å¥ãªã¡ãœããã䜿çšããŠtry / catchãããã¯ã宣èšãã圢åŒã§JVMã«çµã¿èŸŒãŸããŠããŸãã å¿ èŠãªããšã¯ãããã€ã³ãAãšãã€ã³ãBã®éãã¿ã€ãEã®äŸå€ã¯ãã¹ãŠããã€ã³ãCã®ã³ãŒãã§åŠçããå¿ èŠãããããšèšãããšã ãã§ãã ãããã®å®£èšã¯å¿ èŠãªæ°ã ãæã€ããšãã§ããŸãã ãã®ã¡ãœããã«äŸå€ãæž¡ããããšãJVMã¯ãã®ã¿ã€ãã«å¿ããŠé©åãªcatchãããã¯ãèŠã€ããŸãã
try / catchãããã¯ã®ç°¡åãªäŸ
ç°¡åãªäŸãèããŠã¿ãŸãããã
public void simpleTryCatch() { try { callSomeMethod(); } catch (RuntimeException e) { handleException(e); } }
圌ã«ãšã£ãŠã¯ã以äžã®ãã€ãã³ãŒãã«ãªããŸãã ïŒJVMã¡ã«ããºã ãåŠç¿ããããã®è²ŽéãªããŒã«ã§ããASM EclipseãæäŸãããã©ãŒãããã䜿çšããŠããŸãããã®ãã©ãŒãããã®ã³ãŒãã¯éåžžã«èªã¿ãããããã§ãããL0ããªã©ã¯ã³ãŒãã©ãã«ã§ããïŒ
public simpleTryCatch()V TRYCATCHBLOCK L0 L1 L2 java/lang/RuntimeException L0 ALOAD 0 INVOKEVIRTUAL test/SimpleTryCatch.callSomeMethod()V L1 GOTO L3 L2 ASTORE 1 ALOAD 0 ALOAD 1 INVOKEVIRTUAL test/SimpleTryCatch.handleException(Ljava/lang/RuntimeException;)V L3 RETURN
ãããã£ãŠãcatchã¹ããŒãã¡ã³ãã«tryãããã¯å šäœãã«ããŒããããã«æ瀺ããŸãïŒãã ããæåŸã®GOTOã¹ããŒãã¡ã³ãã¯ã«ããŒããŸããïŒãRuntimeExceptionã®å Žåã¯ãL2ã«å¶åŸ¡ãæž¡ããŸãã tryã¹ããŒãã¡ã³ããå®äºããããcatchã¹ããŒãã¡ã³ããé£ã³è¶ããŠå®è¡ãç¶ç¶ããå¿ èŠããããŸãã RuntimeExceptionãã³ãã©ãŒãåŒã³åºãããå ŽåãäŸå€ã¯ã¹ã¿ãã¯ã®äžçªäžã«ãããããŒã«ã«å€æ°ã«ä¿åããŸãã 次ã«ããthisããžã®ãã€ã³ã¿ãšäŸå€ããã®é åºã§ããŒãããŠãhandleExceptionã¡ãœãããåŒã³åºããŸãã ãã®åŸãæ®ãã®ã³ãŒããæåŸãŸã§å®è¡ãããŸãã ãã ããè¿œå ã®catchãããã¯ãããã°ããžã£ã³ãããŠããŸããã
try / catch / finallyãããã¯ã®äŸ
ããã§ãfinallyãããã¯ãšè¿œå ã®catchã¹ããŒãã¡ã³ããè¿œå ãããã€ãã³ãŒãã§äœãèµ·ãããã確èªããŸãã 次ã®å®å šã«äžèªç¶ãªäŸãåãäžããŸãã
public void tryCatchFinally(boolean arg) { try { callSomeMethod(); if (arg) { return; } callSomeMethod(); } catch (RuntimeException e) { handleException(e); } catch (Exception e) { return; } finally { callFinallyMethod(); } }
ãã®å Žåãæ確ãªãã€ãã³ãŒãã¯ã¯ããã«å°ãªããªããŸãã
public tryCatchFinally(Z)V TRYCATCHBLOCK L0 L1 L2 java/lang/RuntimeException TRYCATCHBLOCK L3 L4 L2 java/lang/RuntimeException TRYCATCHBLOCK L0 L1 L5 java/lang/Exception TRYCATCHBLOCK L3 L4 L5 java/lang/Exception TRYCATCHBLOCK L0 L1 L6 TRYCATCHBLOCK L3 L7 L6 TRYCATCHBLOCK L5 L8 L6 L0 ALOAD 0 INVOKEVIRTUAL test/SimpleTryCatch.callSomeMethod()V L9 ILOAD 1 IFEQ L3 L1 ALOAD 0 INVOKEVIRTUAL test/SimpleTryCatch.callFinallyMethod()V L10 RETURN L3 ALOAD 0 INVOKEVIRTUAL test/SimpleTryCatch.callSomeMethod()V L4 GOTO L11 L2 ASTORE 2 L12 ALOAD 0 ALOAD 2 INVOKEVIRTUAL test/SimpleTryCatch.handleException(Ljava/lang/RuntimeException;)V L7 ALOAD 0 INVOKEVIRTUAL test/SimpleTryCatch.callFinallyMethod()V GOTO L13 L5 ASTORE 2 L8 ALOAD 0 INVOKEVIRTUAL test/SimpleTryCatch.callFinallyMethod()V RETURN L6 ASTORE 3 ALOAD 0 INVOKEVIRTUAL test/SimpleTryCatch.callFinallyMethod()V ALOAD 3 ATHROW L11 ALOAD 0 INVOKEVIRTUAL test/SimpleTryCatch.callFinallyMethod()V L13 RETURN
ããã§äœãèµ·ãã£ãŠããã®ã§ããããïŒ ïŒã©ãã«ã«ã¯ãã³ãŒãã«è¡šç€ºãããé åºã§ã¯ãªããã³ã³ãã€ã©ã«ãã£ãŠäœæãããé åºã§çªå·ãä»ããããŠããããšã«æ³šæããŠãã ãããïŒãŸããäž¡æ¹ã®äŸå€åŠçãããã¯ãL0ããL1ããã³L3ã®2ã€ã«åå²ãããŸãã L4ãžã ããã¯ãreturnã¹ããŒãã¡ã³ãã«ãããfinallyãããã¯ãL1ãšL3ã®éã«æ¿å ¥ãããããã«çºçããŸããã
finallyãããã¯ããã¹ããŒãããäŸå€ã¯ãåãtryã¹ããŒãã¡ã³ãã«é¢é£ä»ããããcatchãããã¯ã§åŠçãã¹ãã§ã¯ãªãããã察å¿ããç¯å²ã¯äŸå€ããŒãã«ããåé€ãããŠããŸãã äŸå€ã¿ã€ãã®ãªãããŒãã«ã®ã¬ã³ãŒãã¯ãfinallyãããã¯ãåç §ããŸãã tryã¹ããŒãã¡ã³ããŸãã¯catchãããã¯ããã¹ããŒãããã¿ã€ãã®äŸå€ãåŠçãã眮æãããfinallyãããã¯ãç¡èŠããå¿ èŠããããŸãã ãããã£ãŠãfinallyãããã¯ã¯ãåãfinallyãããã¯ã«ãã£ãŠã¹ããŒãããäŸå€ããã£ããããŸããã tryãããã¯å ã«æåŸã«æ¿å ¥ãããããšã«å ããŠãcatchïŒExceptionïŒãããã¯ã«ã¯returnã¹ããŒãã¡ã³ããå«ãŸããããããã®ãããªãšã³ããªã3ã€ãããŸããã
ãŸããfinallyãããã¯ãã³ãŒãã§5åïŒ5åïŒçºçããããšã«ãé©ããããããããŸããã tryãããã¯ã®returnã¹ããŒãã¡ã³ãã«å¯Ÿå¿ããæåã«æåŸã«çœ®æããããã®ã¯ãL1ãšL3ã®éã§çºçããŸãã 2çªç®ã®finallyãããã¯ã¯ããå°ããããã«ããã§ããæåã®catchãããã¯ã®æåŸã«æ¿å ¥ãããfinallyã³ãŒãã®æ®ãã®éšåããžã£ã³ãããŸãã ïŒå人çã«ã¯ãããã§ã¯æ¬¡ã®åã蟌ã¿ã§ã¯ãªãæåŸã«ç§»è¡ããå¿ èŠããã£ããšæããŸããïŒ3åç®ã¯ã2çªç®ã®catchãããã¯ã®returnã¹ããŒãã¡ã³ãã®åã«L8ãšL6ã®éã«çŸããŸãã äŸå€ã®å Žåã«å¯Ÿå¿ããL6ãšL11ã®éã®ã³ãŒãã«4åç®ã®finallyãããã¯ã衚瀺ãããŸãïŒtryãããã¯ãŸãã¯catchãããã¯ã§ã¹ããŒãããæªåŠçã®äŸå€ãçºçããå Žåãfinallyãããã¯ãå®è¡ãããããšã確èªããå¿ èŠããããŸãã äŸå€ã¯äœãèµ·ãããªãã£ãããã«ä¿åãããfinallyã¹ããŒãã¡ã³ããåŒã³åºãããåŸãäŸå€ãããŒããããŠåã³ã¹ããŒãããããšã«æ³šæããŠãã ããã æåŸã®finallyãããã¯ã§ã¯ãtryãããã¯ã®çµããããå¶åŸ¡ãæž¡ãããŸãã
try / catchãŸãã¯try / finallyãããã¯ããã¹ãããŠããå Žåããã¹ãŠãããã«å¥åŠã«ãªããŸãã å éštryãããã¯ã®returnã¹ããŒãã¡ã³ãã§ã¯ãå éštryãšå€éštryã®äž¡æ¹ã®finallyãããã¯ã§çœ®ãæããå¿ èŠããããŸãã äŸå€ããŒãã«ã¯ãå éšã®finalã«ãã£ãŠã¹ããŒãããäŸå€ãå€éšã®catchã¹ããŒãã¡ã³ããšfinallyã¹ããŒãã¡ã³ãã«ãã£ãŠãã£ãããããå€éšã®finalã«ãã£ãŠã¹ããŒãããäŸå€ã誰ã«ããã£ãããããªãããã«æ§æããå¿ èŠããããŸãã ä»ãããªãã¯ãããããã³ã³ãã€ã©ãã©ã®ãããªç¶æ ã®ã»ãããèªåèªèº«ã§éã°ãªããã°ãªããªãã®ããæ³åããããšããŠããŸãã
å°ãªããšãç§ã«ãšã£ãŠãJVMã®äœæè ããä»®æ³ãã·ã³ã«åã蟌ãã®ã§ã¯ãªããfinallyã¹ããŒãã¡ã³ããã³ã³ãã€ã©ãŒã«åºå®ããããšãã©ã®ããã«æ±ºå®ããããç¥ãããšã¯èå³æ·±ãã§ãããã æããã«ãã³ã³ãã€ã©ã§ãã®äœæ¥ãè¡ããšãä»®æ³ãã·ã³ãå€§å¹ ã«ç°¡çŽ åãããŸãããJVMçšã«å¥ã®èšèªãäœæããç§ãã¡ã®ãããªäººã ã«ãšã£ãŠã¯ãç掻ãå°ãé£ãããªããŸãã
ã«ã¹ã¿ã äŸ
ã³ã³ãã€ã©ã®å®è£ æ¹æ³ãç解ããããšã«ãããããã€ãã®éæšæºçãªã±ãŒã¹ãç解ãããããªããŸãã äŸïŒ
try { return "foo"; } finally { return "bar"; }
çµæã¯ãbarãã«ãªããŸããããã¯ãfinallyã¹ããŒãã¡ã³ããreturnã¹ããŒãã¡ã³ãã®åã«çœ®æãããããã§ããã€ãŸããfinallyãããã¯ããã®æ»ããæåã«åŒã³åºãããtryãããã¯ããã®æ»ãã¯ãŸã£ããåŒã³åºãããŸããã
String value = "foo"; try { return value; } finally { value = "bar"; }
çµæã¯ãfooãã«ãªããŸããããã¯ãfinallyã¹ããŒãã¡ã³ããåŒã³åºãããåã«returnã¹ããŒãã¡ã³ãã®å€ãããã·ã¥ããããã®åŸã§åŸ©å ãããŠè¿ãããããã§ãã ïŒç§ã®äŸã§ã¯ããã瀺ããŠããŸããããããã¯ãã€ãã³ãŒããèŠããšè¡šç€ºããããã®ã§ããïŒãããã£ãŠãfinallyãããã¯ã®ãvalueãã®å€ãå€æŽããŠããreturnã¹ããŒãã¡ã³ãã®å€ã¯ãããŸããã ãããŠæåŸã«ã次ã®ãããªãã®ïŒ
while(true) { try { return "foo"; } finally { break; } } return "bar";
çµæã¯ãããŒãã«ãªããŸãã ç§ã«ãšã£ãŠãé©ãã§ãããããã€ãã³ãŒãã§breakã¹ããŒãã¡ã³ããGOTOã«éããªãããšãç¥ã£ãŠããã°ããã¹ãŠãè«ççã§ãã ã€ãŸã finallyãããã¯ãå éšreturnã¹ããŒãã¡ã³ãã®äžéšãšããŠçœ®æããããšãGOTOã¹ããŒãã¡ã³ããRETURNã¹ããŒãã¡ã³ããããåã«åŒã³åºãããã«ãŒããçµäºããŸãã ïŒfinallyãããã¯å ã®continueã¹ããŒãã¡ã³ãã«ã€ããŠãåæ§ã§ããïŒ
ãããã«
CïŒã§è¡ãããŠããããã«ãæªå®çŸ©ã®ã»ãã³ãã£ã¯ã¹ã®ãããfinallyãããã¯å ã§returnãbreakãcontinueã¹ããŒãã¡ã³ããçŠæ¢ããããšã«ããŸããã ïŒãããŠãç§ã¯ãã®ãããªæ±ºå®ãäžãã人ã ããè¯ãäŒç€ŸãéãŸã£ããšæããŠããŸããïŒ
誰ãããã®èšäºãæçã ãšæã£ããããã€ãã³ãŒããçæãããšãã«åºäŒã£ãä»ã®èå³æ·±ãããšã«ã€ããŠããã€ãã¡ã¢ãæžãã€ããã§ãã ãããã¯ãJVMã«çŽæ¥é¢é£ããã»ããã¯ããŒãžã£ãå€éšé¢æ°ããžã§ããªãã¯ãªã©ãèšèªãšJavaã®éã®ããŸããŸãªäžæŽåã«é¢é£ããŸãã
çšèªé
ãµãã«ãŒãã³=ãµãã«ãŒãã³
ã¹ããŒãã¡ã³ã=ã¹ããŒãã¡ã³ã
眮æ=ã€ã³ã©ã€ã³å
äŸå€=äŸå€
å€éšæ©èœ= æ¡åŒµ = æ¡åŒµæ©èœ = ããã¯ã¹ã€ã³
åç §è³æ
[1] devblog.guidewire.com/author/akeefer
[2] www.guidewire.com
[3] gosu-lang.org