Next: Errors, Previous: Catch and Throw, Up: Nonlocal Exits [Contents][Index]
catch
¤Èthrow
¤ÎÎãcatch
¤Èthrow
¤Î»È¤¤Êý¤Î1¤Ä¤Ï¡¢
2½Å¤Î¥ë¡¼¥×¤«¤é¤Îæ½Ð¤Ç¤¹¡£
¡Ê¤Û¤È¤ó¤É¤Î¸À¸ì¤Ç¤Ï¡¢¤³¤ì¤ò¡Øgo to¡Ù¤Ç¹Ô¤¦¤Ç¤¢¤í¤¦¡£¡Ë
¤³¤³¤Ç¤Ï¡¢i¤Èj¤ò0¤«¤é9¤ËÊѤ¨¤Ê¤¬¤é¡¢
(foo i j)
¤ò·×»»¤·¤Þ¤¹¡£
(defun search-foo () (catch 'loop (let ((i 0)) (while (< i 10) (let ((j 0)) (while (< j 10) (if (foo i j) (throw 'loop (list i j))) (setq j (1+ j)))) (setq i (1+ i))))))
foo
¤¬¤¢¤ë»þÅÀ¤Çnil
°Ê³°¤òÊÖ¤¹¤È¡¢
¤¿¤À¤Á¤Ë»ß¤Þ¤Ã¤Æi¤Èj¤Î¥ê¥¹¥È¤òÊÖ¤·¤Þ¤¹¡£
foo
¤¬¤Ä¤Í¤Ënil
¤òÊÖ¤¹¤È¡¢
catch
¤ÏÄ̾ï¤É¤ª¤ê¤ËÌá¤Ã¤Æ¡¢¤½¤ÎÃͤÏnil
¤Ç¤¹¡£
¤È¤¤¤¦¤Î¤Ï¡¢while
¤Î·ë²Ì¤Ïnil
¤À¤«¤é¤Ç¤¹¡£
2¤Ä¤Î¹ªÌ¯¤ÊÎã¤ò¤¢¤²¤Þ¤·¤ç¤¦¡£
¿¾¯°Û¤Ê¤ë2¤Ä¤ÎÌá¤ê°ÌÃÖ¤¬Æ±»þ¤Ë¸ºß¤·¤Þ¤¹¡£
¤Þ¤º¡¢Æ±¤¸¥¿¥°hack
¤Ç2¤Ä¤ÎÌá¤ê°ÌÃÖ¤¬¤¢¤ê¤Þ¤¹¡£
(defun catch2 (tag) (catch tag (throw 'hack 'yes))) ⇒ catch2
(catch 'hack (print (catch2 'hack)) 'no) -| yes ⇒ no
¤É¤Á¤é¤ÎÌá¤ê°ÌÃÖ¤âthrow
¤Ë°ìÃפ¹¤ë¥¿¥°¤Ê¤Î¤Ç¡¢
Æâ¦¤Î¤â¤Î¡¢¤Ä¤Þ¤ê¡¢catch2
¤Ç³ÎΩ¤·¤¿¤â¤Î¤ËÌá¤ê¤Þ¤¹¡£
¤·¤¿¤¬¤Ã¤Æ¡¢catch2
¤ÏÃÍyes
¤ÇÄ̾ï¤É¤ª¤êÌá¤ê¡¢
¤³¤ÎÃͤ¬É½¼¨¤µ¤ì¤Þ¤¹¡£
ºÇ¸å¤Ë¡¢³°Â¦¤Îcatch
¤Î2ÈÖÌܤÎËÜÂÎ¥Õ¥©¡¼¥à¡¢
¤Ä¤Þ¤ê¡¢'no
¤¬É¾²Á¤µ¤ì¡¢³°Â¦¤Îcatch
¤«¤éÌá¤ê¤Þ¤¹¡£
º£Å٤ϡ¢catch2
¤Ë»ØÄꤹ¤ë°ú¿ô¤òÊѹ¹¤·¤Æ¤ß¤Þ¤¹¡£
(defun catch2 (tag) (catch tag (throw 'hack 'yes))) ⇒ catch2
(catch 'hack (print (catch2 'quux)) 'no) ⇒ yes
¤³¤³¤Ç¤â2¤Ä¤ÎÌá¤ê°ÌÃÖ¤¬¤¢¤ê¤Þ¤¹¤¬¡¢
º£Å٤ϳ°Â¦¤Î¤â¤Î¤À¤±¤¬¥¿¥°hack
¤Ç¤¹¡£
Æâ¦¤Î¤â¤Î¤Ï¥¿¥°quux
¤Ç¤¹¡£
¤·¤¿¤¬¤Ã¤Æ¡¢throw
¤Ë¤è¤ê¡¢³°Â¦¤Îcatch
¤¬ÃÍyes
¤òÊÖ¤·¤Þ¤¹¡£
´Ø¿ôprint
¤Ï¤±¤Ã¤·¤Æ¸Æ¤Ð¤ì¤º¡¢
ËÜÂÎ¥Õ¥©¡¼¥à'no
¤â¤±¤Ã¤·¤ÆÉ¾²Á¤µ¤ì¤Þ¤»¤ó¡£