JavaScriptã§ã®ä¾å¤è¨è¨ãï¼throwï¼try-catch-finallyæ§æã®ã¤ã¡ã¼ã¸ã¨å©ç¨ãã¿ã¼ã³ï¼
JavaScriptã®ä¾å¤ï¼throw, try-catch-finallyæ§æï¼ã®ä½¿ãæ¹ã«ã¤ãã¦ï¼ä»¥ä¸ã®ç¹ãè«ããã
- ï¼ï¼¡ï¼breakã¨returnã¨throwã®æ¯è¼
- ï¼ï¼¢ï¼throwãå©ç¨ããè¨è¨ã®ã¤ã¡ã¼ã¸
- ï¼ï¼£ï¼throwã®ä½¿ãã©ãã
- ï¼ï¼¤ï¼ä¾å¤å¦çã®ãµã³ãã«ã³ã¼ãÃï¼
ï¼ï¼¡ï¼breakï¼continueï¼ã¨throwã¨returnã¯ä½ãªã®ã
ã©ãããgotoãã§ããã
ç¹ã«ï¼breakãGOTOã§ããäºã«ã¤ãã¦ã®ãµã³ãã«ã³ã¼ãï¼
HOGE:{ alert( "ãã®ã¡ãã»ã¼ã¸ã¯è¡¨ç¤ºããã¾ãã" ); // "GOTO"ã®å½¹ç®ãããã該å½ãããã¯ããæãã break HOGE; alert( "ãã®ã¡ãã»ã¼ã¸ã¯è¡¨ç¤ºããã¾ããã" ); }// end of HOGE alert( "HOGEãããã¯ãæãã¾ããã" );
http://d.hatena.ne.jp/GOLEM-XIV/20100427
- breakã¯ã©ãã«ä»ãã«ã¼ãï¼ã¾ãã¯ã©ãã«ä»ããããã¯ããæãããâã«ã¼ãã§ãªãã¦ãå©ç¨å¯ãªã®ã§ï¼GOTOã¨ãã¦ä½¿ããã
以ä¸ã¯returnã¨throwã®æ¯è¼ã
移åã®ã¹ã³ã¼ãï¼
- returnã¯ï¼å¿ ãã¡ã½ããã®é層ãï¼æ®µéæ»ãããåãé¢æ°å ã§ã®ç§»åãã¯ã§ããªããå¼ã³åºãå ã¸ç§»åãããï¼ã¯ãã¼ã¸ã£ã使ãã°ããç¨åº¦èéã¯å©ããï¼
- throwã¯ï¼åä¸ã¡ã½ããå ã§catchãã¦ããã°ï¼åä¸ã¡ã½ããå ã§ã®ç§»åãå¯è½ãå¼ã³åºãå ã¡ã½ããã辿ã£ã¦ããï¼ã©ããã§catchããã¦ããã°ï¼ããã«ç§»åã§ãããã¡ã½ããå¼ã³åºãã®ã¹ã¿ãã¯å ã®ä»»æã®é層ã«æ»ãããã«ããããã¨ãã§ããã
è¿ãå¤ï¼
- returnã¯ï¼ä½ã§ãè¿ããè¿ãããå¤ï¼ä¾ãã°ï¼ã¨ã©ã¼ã³ã¼ããªã©ï¼ã®åé¡ã®ããã«ã¯ï¼å¼ã³åºãå´ã§èªåã§switchæãæ¸ãå¿ è¦ãããã
- throwã¯ï¼ã¨ã©ã¼æ å ±ãè¿ãäºã«ç¹åããæ³å®ãJavaãï¼Firefoxä¸ã§ã®JavaScriptã§ããã°ï¼æ§æãã®ãã®ã«caseæçãªè¦ç´ ãç¹ãè¾¼ã¿æ¸ã¿ããã ãIE/JScriptã§ã¯ï¼catchç¯ãå¤æ®µæ¸ããã¨ãã§ããªãã®ã§ï¼èªåã§caseæãcatchç¯å ã«æ¸ããªãã¦ã¯ãªããªãã
Javaã®å ´å
http://d.hatena.ne.jp/unageanu/200708...
- ä¾å¤ã¯ã©ã¹ãè¤æ°ä½ãã®ã§ã¯ãªãã¨ã©ã¼ã³ã¼ãã§èå¥ããï¼ã¨ããæ¦ç¥ãããã«ã¯ããã
- â»ä¾å¤ã¯ã©ã¹ã®ç¶æ¿é層ãé©åã«è¨è¨ããã°ï¼ï¼åã ã®ã¨ã©ã¼ãé©åãªç²åº¦ã§ååé¡ããã°ï¼ï¼catchç¯ã10åç¶ããããªã³ã¼ãã«ã¯ãªããªãã
ï¼ï¼¤ï¼£ï¼æ¡ä»¶ä»catchç¯
https://developer.mozilla.org/ja/Core...
- 1 ã¤ä»¥ä¸ã®æ¡ä»¶ä»ã catch ç¯ã使ããã¨ãã§ãã¾ãããã®å ´åãç¹å®ã®ä¾å¤ãæããããã¨ãé©å㪠catch ç¯ã«å ¥ãã¾ãã
- ECMAScript ä»æ§ã®ä¸é¨ã§ã¯ããã¾ããã
以ä¸ï¼returnã¨æ¯ã¹ãå ´åã®throw (tryæ§æ)ã®ãã¤ã³ãã
ï¼ï¼¢ï¼throwãå©ç¨ããè¨è¨ã®ã¤ã¡ã¼ã¸
tryã®ã¤ã¡ã¼ã¸ãããã«å ·ä½åããã
- tryãæ¸ãã¦ããä¸ä½å¦ç ââ tryã§å ã¾ãã¦ããä¸ä½å¦ç
ãï¼ãã¾ãã¾ãªã¢ããã¸ã¼ã§è¡¨ç¾ãã¦ã¿ããã
æ¥å¸¸çæ´»ã«åºã¥ããã¢ããã¸ã¼ï¼
- 責任è ââ ç¡è²¬ä»»ã®ä½æ¥è
- é¢åãè¦ã¦ããã人 ââ å®å¿ãã¦å¤±æã§ãã人
- 親 ââ å
- ã¬ã¼ã ââ æ¨ã¦é§
- ã¸ã£ãã¸ï¼ä¸»å¬è ââ ææ¦è
ã¡ã½ããé層ã®å®è£ ã®ã¤ã¡ã¼ã¸ï¼
- å¼ã³åºãå ââ å¼ã³åºãå
- å¦çãä¸æããªã ââ å¦çãä¸æããã
- ç°å¸¸ç³»ã«å¯¾å¦ããè¡ãç¥ã£ã¦ãã人 ââ æ£å¸¸ç³»ã ãç¥ã£ã¦ããä½æ¥è ï¼ãããããã¡ã½ããï¼
- do_hoge_safe() ââ do_hoge_unsafe()
- Manager ââ Worker
- facadeï¼é çªã«å¼ã³åºãï¼ ââ åå¥function
ï¼ï¼£ï¼throwã®ä½¿ãã©ãã
JavaScriptã§ã®ã³ã¼ãã£ã³ã°æï¼ã©ã®ãããªæã«throw / try-catchã使ãã®ãã
ï¼ï¼ï¼APIãè¿ãå ´åã
JavaScriptã®ãã¤ãã£ãã¡ã½ããï¼ä¾ãã°evalã®ã¨ã©ã¼ã¨ãã
ãããã£ãé¢æ°ãï¼å¦çã®æå失æã®ãã©ã°ãè¿ãã®ã§ã¯ãªãä¾å¤æããã ãã¨ããå ´åã
âcatchãæ¸ããããå¾ãªãã
var str = "alert( 1 + );"; try { eval( str ); } catch( e ) { //alert( e.message ); alert( "ãã¼ã¹ã§ãã¾ããã" ); }
ï¼ï¼ï¼èªåã®æååã®è§£æãã¸ãã¯ã(èªåevalï¼
æååã®ãã¼ã¹ã®ã¢ã«ã´ãªãºã ã¯ç°å¸¸ç³»ãå¤ãããã®ã§äºæ¸¬ããããªãã
ãã¹ã¦ã®ç°å¸¸ãã¿ã¼ã³ãããããifæã®ããªãã¼ã·ã§ã³ã§æ¤ç¥ããã®ã¯éç¾å®çãï¼ï¼ãã©ãã¯ãªã¹ããä½ããããªãï¼ã¨ãããã¨ï¼
âããããæã«ï¼ç°å¸¸ç³»ãDRYã«æ¸ãããã«try-catchæ§æãå½¹ç«ã¤ã
æ£å¸¸ç³»ï¼ãã¯ã¤ããªã¹ãã®ãããªãã®ï¼ãtryç¯å ã«æ¸ãï¼ã ãã ã£ãã¨ãã®å¦çã¯catchå ã®ä¸ç®æã«ã¾ã¨ããã
ä¾å¤å¦çã®è¨è¨
http://yakinikunotare.boo.jp/orebase/...
- ä¾å¤ãçºçãããªãããã«ã³ã¼ãããã¨ãã³ã¼ãã®ã»ã¨ãã©ãä¾å¤æ¡ä»¶ãé¤å¤ããifæã®åµã«
- æ³å®ãããªãã¸ã§ã¯ãç¶æ 以å¤ãä¾å¤ã¨ãã¦å¦ç
- ã¡ã½ããã使ããã³ã«ãã§ãã¯ãã¸ãã¯ãæ¸ããªãã¦ããã®ã§ä¾¿å©ãDRYææ³ã«ãå½ã¦ã¯ã¾ã
- é»æ°åè·¯ã§ããã°ã©ã³ãã®ãããªä¸çªå¤å´ã«å¦çãæ¨ã¦ãå ´æ
ï¼ï¼ï¼æ£å¸¸ç³»ã®ã³ã¼ãããã§ã«ä¸ãããåä½ãã¦ãããããã«æãå ããªãã§ï¼ç°å¸¸ç³»ã«ã対å¦ãããã
ãåãã¦ããã³ã¼ãã«æãå ããããªãã人ã®å ´åã
âå¤å´ã«tryãããã¯ã§å²ãã°ãããtryç¯å ã«æãå ããªãã¦ããã
ãããã¯ï¼æ£å¸¸ç³»ã®ã¿ãèæ
®ããã¡ã½ãããå®æãã¦ããå ´åï¼ãããå¼ã³åºãã¡ã½ããã§try-catchããã
ï¼ï¼ï¼å¿ãã¦ã¯ãªããªãå¿ é ã®å ±éã¨ã©ã¼å¦çãæçµå¦çãããå ´åã
ãã¡ã¤ã«æä½æã¯ï¼ãã¡ã¤ã«ã®éãå¿ãã«æ³¨æãããä»ã®è¨èªã§ã¯ä¾å¤ãããããæã«ä½¿ãã
JavaScriptã§ãã¡ã¤ã«æä½ã¯ï¼ãã©ã¦ã¶ä¸ã§ã¯ï¼ããããªããï¼ä¼¼ããããªç¶æ³ã¯ããããã
ãµã¤æ¬ï¼4thï¼ try/catch/finallyã®è§£èª¬ã®æ«å°¾
http://docstore.mik.ua/orelly/webprog...
- whileæå ã§continueããæã«ï¼ã«ã¦ã³ã¿ã¼ãä¸ãå¿ããã¨ç¡éã«ã¼ãã«ãªãããããé²æ¢ããããã«ï¼finallyå ã«ã«ã¦ã³ãã¢ãããæ¸ãã
- ããtryå¥å ã®å¦çã大ããã¦ï¼continueããã¡ãã¡ã«æ¸ãããããªå ´åï¼ã«ã¦ã³ãã¢ããã®æ¸ãå¿ããèµ·ãããããªãããï¼ãã®æ¹æ³ãæå¹ã
âãå
±éã®æçµå¦çããæ¸ãããå ´åã¯finallyã使ãã
ï¼ï¼ï¼ãããã¯ã¼ã¯éä¿¡æ
ãããã¯ã¼ã¯å¦çã®ã³ã¼ãã¯ï¼è©²å½ãããããã³ã«å ã§ã®ããã¨ããæ£å¸¸ç³»ã§æ¸ãã¦ç©ã¿ä¸ããã
ãã®ããåãã®éä¸ã§ç°å¸¸ãçºçãããï¼éä¿¡ãéä¸ã§åãä¸ããï¼ã¾ãã«GOTOï¼
ãä¿¡é ¼æ§ãä½ããï¼åéã¯ãããããã¨ãããããã¯ã¼ã¯ç¹æ§ãããå ´åã¯ï¼ç¢ºç«æ¸ã¿ã³ãã¯ã·ã§ã³ã®ç ´æ£ããã¾ããããããªãã
åé ã®ããã¡ã¤ã«ã®éãå¿ããã¨åããï¼ã½ã±ããã®éãå¿ã対å¦ã
âJavaScriptã®å ´åã¯ï¼Ajaxã§ä¾å¤å¦çãå¿
é ã«ãªãã
æ®æ®µã¯ã©ã¤ãã©ãªãããã®ã§æèããªããï¼XmlHttpRequestãæ±ãã©ã¤ãã©ãªã®ä¸èº«ãèªãã¨åå¼·ã«ãªãã
ï¼ï¼ï¼å¼ã³åºãå ã¨å¼ã³åºãå ã§åæ¥ãã¦ããï¼ã³ãã¥ãã±ã¼ã·ã§ã³ãã¨ãã¥ããæ
âExceptionã®åé¡ãæ¯ãèããï¼å¦çé層éã®ã¤ã³ã¿ãã§ã¼ã¹ã¨ãã¦åã決ãã¦ããã
Javaã§ã¯ãµã¤ãã®ä½æ¥ã ãï¼JavaScriptã§ã¯ããã¾ã§ãããªããã
ï¼ï¼ï¼ã«ã¼ãæã§ã¯ãªãã¤ãã¬ã¼ã¿ã¡ã½ããã«ããã®ã ãã«ã¼ããéä¸ã§æ¢ãããå ´å
âbreakã®ä»£æ¿æ段ã¨ãã¦throwã使ãæãã
å復çãªã¡ã½ããã¸ã®å¿ç¨
http://ja.wikibooks.org/wiki/JavaScri...
ï¼ï¼Î±ï¼ä¾å¤ã使ããªãã»ããããå ´åã¨ã¯ï¼
ç¹°ãè¿ãå®è¡ããï¼é度ãè¦æ±ããããããªã¡ã½ããã®å ´åã
å¹çç㪠JavaScript, ECMAScript
http://dev.opera.com/articles/view/ef...
- æ§è½ã決ããé¢æ°ã§ try-catch-finally ã使ãã®ã¯ãããã
- catchç¯ã®åãã¨çµããã«ã¯å¤æ°ã®çæã»ç ´æ£å¦çãå ¥ãããã ãï¼ãå¤æ°ãå®è¡æã«çæ, ç ´æ£ããããã¨ããã®ã¯è¨èªã®ä¾å¤äºé ããã®ããéããªãå ´åãããã
- ä¾å¤å¦çã¯ãã¾ãéããã¨ã®ãªãã¹ã¯ãªããã®ä¸ã®ã¬ãã«ã«æ¸ããæ¹ããã.
ããã¯ï¼catchã¯ä¸ä½ã¬ã¤ã¤ã§ã¯ãªãä¸ä½ã§ãããã®ã ï¼ã¨ããæ®éã®ååã
ï¼ï¼¤ï¼ãµã³ãã«ã³ã¼ãï¼
ç°å¸¸ç³»ã®å¯¾å¦ã³ã¼ããï¼è¡ã«ã¾ã¨ãããããªãµã³ãã«ã
// ãã®è¡ã®ã³ã¡ã³ãã¢ã¦ããå¤ãã¨ï¼jsãã¡ã¤ã«ãç´æ¥å®è¡ã§ãã¾ãã //function alert(s){ WScript.Echo( s ); } // hogeãç¡ãå ´åã®ä¾å¤ã¯ã©ã¹ var HogeNaiyoException = function(){ // ã¨ã©ã¼çºçæå»ãè¨é² this.error_happenned_at = new Date(); }; HogeNaiyoException.prototype = { message : "ããã·ã¥å ã«hogeãããã¾ããã§ããã", rescue_me : function(){ alert( "hogeãç¡ãã£ãå ´åã®ææ¸å¦ç½®ãå®è¡ãã¾ãããï¼éä¿¡ã¨ãï¼" ); } }; // fugaãç¡ãå ´åã®ä¾å¤ã¯ã©ã¹ var FugaNaiyoException = function(){ // ã¨ã©ã¼çºçæå»ãè¨é² this.error_happenned_at = new Date(); }; FugaNaiyoException.prototype = { message : "ããã·ã¥å ã«fugaãããã¾ããã§ããã", rescue_me : function(){ alert( "fugaãç¡ãã£ãå ´åã®ææ¸å¦ç½®ãå®è¡ãã¾ãããï¼ï¼¤ï¼¯ï¼æä½ã¨ãï¼" ); } }; // ã¡ã¤ã³å¦ç var target_hash = { x : 1, y : 2, hoge : 3 }; try { if( ! ( "hoge" in target_hash ) ) { throw new HogeNaiyoException(); } else if( ! ( "fuga" in target_hash ) ) { throw new FugaNaiyoException(); } // ã } catch( e ) { // ã¨ã©ã¼å 容ã®ã¡ãã»ã¼ã¸ã表示 alert( e.message ); // ã¨ã©ã¼çºçæå»ã表示 alert( e.error_happenned_at.toString() ) // çºçããã¨ã©ã¼ã®ç¨®é¡ã«å¿ãã¦ï¼ææ¸å¦çãè¡ãªããã e.rescue_me(); }
catchç¯å
ã«ããªã¢ã¼ãã£ãºã ãã£ã½ãããæããã
ãµã³ãã«ã³ã¼ãï¼ï¼Firefoxéå®
Firefoxã§ã¯ï¼catchç¯ãå¤æ®µã«ã§ããã
åæ²ã®ãµã³ãã«ã¨catchç¯ã ãå¤ãã¦ããã
<script> // hogeãç¡ãå ´åã®ä¾å¤ã¯ã©ã¹ var HogeNaiyoException = function(){ // ã¨ã©ã¼çºçæå»ãè¨é² this.error_happenned_at = new Date(); }; HogeNaiyoException.prototype = { message : "ããã·ã¥å ã«hogeãããã¾ããã§ããã", rescue_me : function(){ alert( "hogeãç¡ãã£ãå ´åã®ææ¸å¦ç½®ãå®è¡ãã¾ãããï¼éä¿¡ã¨ãï¼" ); } }; // fugaãç¡ãå ´åã®ä¾å¤ã¯ã©ã¹ var FugaNaiyoException = function(){ // ã¨ã©ã¼çºçæå»ãè¨é² this.error_happenned_at = new Date(); }; FugaNaiyoException.prototype = { message : "ããã·ã¥å ã«fugaãããã¾ããã§ããã", rescue_me : function(){ alert( "fugaãç¡ãã£ãå ´åã®ææ¸å¦ç½®ãå®è¡ãã¾ãããï¼ï¼¤ï¼¯ï¼æä½ã¨ãï¼" ); } }; // ã¡ã¤ã³å¦ç var target_hash = { x : 1, y : 2 }; try { if( ! ( "hoge" in target_hash ) ) { throw new HogeNaiyoException(); } else if( ! ( "fuga" in target_hash ) ) { throw new FugaNaiyoException(); } // ã } catch( e if e instanceof HogeNaiyoException ) { alert( e.message ); e.rescue_me(); alert( "hogeãç¡ãã£ãå ´åã®ã¨ã©ã¼å¦çãçµããã¾ããã" ); } catch( e if e instanceof FugaNaiyoException ) { alert( e.message ); e.rescue_me(); alert( "fugaãç¡ãã£ãå ´åã®ã¨ã©ã¼å¦çãçµããã¾ããã" ); } </script>
é¢é£ããè¨äºï¼
JavaScriptã§ï¼ã¯ã©ã¹ãç¶æ¿ããæ¹æ³ãï¼è¤æ°ã®ãµãã¯ã©ã¹ããå
±éã¯ã©ã¹ã®ãããã¿ã¤ããåç
§ããï¼
http://language-and-engineering.hatenablog.jp/entry/20100924/p1
JavaScriptã§ï¼ã°ãã¼ãã«å¤æ°ã®åå¨å¤å®ãããï¼ã¤ã®æ¹æ³ãï¼"window"ã®å®ç¾©ç¶æ³ã確èªãããï¼
http://language-and-engineering.hatenablog.jp/entry/20090412/p1
JavaScriptã§ï¼ãªãã¸ã§ã¯ããã¯ã©ã¹ã®åæ©ãç解ãã¦ãããï¼å®åã確ãããããã®ï¼ã¤ã®è³ªåãï¼ãµã³ãã«ã³ã¼ãä»ãï¼
http://language-and-engineering.hatenablog.jp/entry/20100921/p1