Lisp の例外処理超入門
Java の例外処理は以下のように書く。
public void sample(){ try { throwException(); } catch(Exception e){ e.printStackTrace(); } }
あらかじめ、このメソッドはこういう例外を投げる、というのを
把握しておかないといけない。まあ、JavaDoc があれば書いてあるだろうし、
なくてもコンパイラが教えてくれる。
これを Lisp に直してみる。
Lisp の try / catch は以下のように書ける。
(defun throw-func () (format t "Throw UserException.~%") (throw 'UserException 'done) (format t "This code will never invoked.~%")) (defun catch-func () (catch 'UserException (format t "Start~%") (throw-func) (format t "End~%"))) (catch-func)
実行結果は以下のようになった。
? (defun throw-func () (format t "Throw UserException.~%") (throw 'UserException 'done) (format t "This code will never invoked.~%")) THROW-FUNC ? (defun catch-func () (catch 'UserException (format t "Start!~%") (throw-func) (format t "End...~%"))) CATCH-FUNC ? (catch-func) Start! Throw UserException. DONE ?
throw の後は 'done としたけれど、これを変えると色々できたりするのだろうか。
http://psg.com/~dlamkins/sl/chapter09.htmlを参考にしてみたけれども、、、何か物足りない気がする。