{"id":1041,"date":"2012-03-02T19:39:23","date_gmt":"2012-03-02T11:39:23","guid":{"rendered":"http:\/\/leegorous.net\/blog\/?p=1041"},"modified":"2012-03-03T17:46:26","modified_gmt":"2012-03-03T09:46:26","slug":"condition-system-vs-exception-handling","status":"publish","type":"post","link":"http:\/\/leegorous.net\/blog\/2012\/03\/02\/condition-system-vs-exception-handling\/","title":{"rendered":"\u72b6\u51b5\u7cfb\u7edf \u4e0e \u5f02\u5e38\u5904\u7406"},"content":{"rendered":"<blockquote><p>\u201cLisp \u7684\u72b6\u51b5\u7cfb\u7edf\uff08condition system\uff09\u662f\u5b83\u6700\u4f1f\u5927\u7684\u7279\u6027\u4e4b\u4e00\u3002\u201d<\/p><\/blockquote>\n<p>\u6709\u6ca1\u6709\u6700\u4f1f\u5927\u6211\u8fd8\u4e0d\u662f\u5f88\u6e05\u695a\uff0c\u4f46\u53ef\u4ee5\u80af\u5b9a\u7684\u662f\u7ed9\u4e86\u6211\u8033\u76ee\u4e00\u65b0\u7684\u611f\u89c9\u3002<\/p>\n<p>\u4f1a Java \u7684\u5e94\u8be5\u90fd\u5bf9\u4e86\u89e3\u5176\u5f02\u5e38\u5904\u7406\u7684\u673a\u5236\uff0c\u8fd9\u4e00\u7c7b\u578b\u7684\u5f02\u5e38\u5904\u7406\u65b9\u5f0f\u4f30\u8ba1\u662f\u4f7f\u7528\u6700\u5e7f\u6cdb\uff08Python, JavaScript, etc\uff09\u7684\u4e86\u3002<br \/>\n\u5176\u8868\u8fbe\u5f62\u5f0f\u4e5f\u6bd4\u8f83\u7b80\u5355\uff1a\uff08\u4ee5 Java \u4e3a\u4f8b\uff09<\/p>\n<pre class=\"brush: java\">\r\ntry {\r\n  \/\/ do something could cause exception\r\n} catch (Exception e) {\r\n  \/\/ handle exception\r\n}\r\n<\/pre>\n<p>\u800c\u4e14\uff0c\u4ea7\u751f\u7684\u5f02\u5e38\u5982\u679c\u4e0d\u597d\u5904\u7406\u8fd8\u53ef\u4ee5\u5f80\u4e0a\u629b\uff08<strong>throw<\/strong> exception\uff09\uff0c\u5f53\u5f02\u5e38\u5411\u4e0a\u5c42\u629b\u51fa\u7684\u65f6\u5019\uff0c\u5f02\u5e38\u4fe1\u606f\u4ee5\u5f02\u5e38\u5bf9\u8c61\u7684\u5f62\u5f0f\u5411\u4e0a\u4f20\u9012\uff0c\u8c03\u7528\u6808\u4f1a\u5c55\u5f00\uff08unwind\uff09\uff0c\u5982\u679c\u67d0\u4e00\u5c42\u6ca1 catch \u4f4f\u6216\u8005\u5904\u7406\u4e0d\u4e86\u7ee7\u7eed\u5f80\u4e0a\u629b\u3002\u76f8\u5f53\u4e0d\u9519\u7684\u6a21\u578b\uff0c\u7b80\u6d01\u6613\u7528\u3002<\/p>\n<p>\u4f3c\u4e4e\u4e00\u5207\u5f02\u5e38\u90fd\u9003\u4e0d\u51fa\u624b\u638c\u5fc3\u4e86\u3002\u5230\u73b0\u5b9e\u4e2d\u7684\u4ee3\u7801\u7ffb\u7ffb\u770b\u3002\u5148\u6392\u9664\u6389\u54ea\u4e9b catch \u5757\u91cc\u9762\u6ca1\u6709\u4efb\u4f55\u4e1c\u897f\uff0c\u6216\u8005\u4e0d\u5199 try catch \u76f4\u63a5\u58f0\u660e throw \u7684\u60c5\u51b5\uff0c\u56e0\u4e3a\u4e0d\u4f7f\u7528\u5c31\u6ca1\u6709\u53d1\u8a00\u6743\u3002\u53ef\u4ee5\u89c2\u5bdf\u5230\u4e0b\u9762\u7684\u60c5\u51b5\u51fa\u73b0\u5f97\u76f8\u5f53\u7684\u591a\u3002<\/p>\n<pre class=\"brush: java\">\r\ne.printStackTrace();\r\nthrow e;\r\n<\/pre>\n<p>\u987f\u65f6\u6709\u79cd\u6cea\u6d41\u6ee1\u9762\u7684\u611f\u89c9\uff0c\u6559\u79d1\u4e66\u4e0a\u7684\u5185\u5bb9\u8fd8\u8bb0\u5f97 T_T<br \/>\n\u597d\u5427\uff0c\u90a3\u8bc1\u660e\u4e0d\u4e86\u4ec0\u4e48\uff0c\u53ea\u662f\u8bf4\u660e\u4e86\u6c34\u5e73\u4e0d\u591f\uff0c\u6ca1\u80fd\u9886\u609f\u5f02\u5e38\u5904\u7406\u7684\u771f\u8c1b\u3002<\/p>\n<p>\u5047\u8bbe\u6211\u4eec\u5df2\u7ecf\u5728 catch \u91cc\u82b1\u4e86\u8bb8\u591a\u5fc3\u601d\u5904\u7406\u5f02\u5e38\uff0c\u5f02\u5e38\u53ea\u6709\u4e00\u79cd\u5904\u7406\u65b9\u5f0f\u7684\u65f6\u5019\u8fd8\u597d\uff0c\u4e00\u65e6\u6709\u591a\u79cd\u5904\u7406\u65b9\u5f0f\uff0c\u5728\u4ec0\u4e48\u60c5\u51b5\u4e0b\u9009\u62e9\u600e\u4e48\u5904\u7406\u5c31\u53d8\u5f97\u68d8\u624b\u8d77\u6765\uff0c\u7531\u4e8e\u6211\u4eec\u5c3d\u91cf\u671f\u671b\u4e00\u4e2a\u65b9\u6cd5\u53ea\u505a\u5b83\u81ea\u5df1\u4e8b\u60c5\uff0c\u9009\u62e9\u54ea\u4e2a\u5904\u7406\u65b9\u5f0f\u53c8\u662f\u5f88\u53ef\u80fd\u4e0e\u4e0a\u5c42\u8c03\u7528\u73af\u5883\u6709\u5173\uff0c\u8981\u4e48\u662f\u5728\u8c03\u7528\u65f6\u4e00\u5e76\u4f20\u5165\u66f4\u591a\u4e0a\u4e0b\u6587\u4fe1\u606f\uff0c\u8981\u4e48\u662f\u629b\u51fa\u5f02\u5e38\uff0c\u7531\u5916\u5c42\u53bb\u9009\u62e9\u548c\u5904\u7406\uff0c\u53ea\u662f\u600e\u4e48\u5904\u7406\u5f02\u5e38\u60c5\u51b5\u5e94\u8be5\u662f\u4e0b\u5c42\u7684\u65b9\u6cd5\u4e86\u89e3\u5f97\u6700\u6e05\u695a\uff0c\u4e00\u65e6\u5f02\u5e38\u629b\u51fa\uff0c\u6808\u5df2\u7ecf\u88ab\u5c55\u5f00\uff0c\u524d\u7f18\u96be\u7eed\u3002\u5f53\u7136\uff0c\u6a21\u578b\u672c\u8eab\u662f\u6ca1\u6709\u4ec0\u4e48\u5bf9\u9519\u7684\uff0c\u53ea\u662f\u770b\u600e\u4e48\u7528\uff0c\u7528\u8d77\u6765\u662f\u5426\u8d39\u52b2\u3002<\/p>\n<p>\u4ece\u6982\u5ff5\u4e0a\u6bd4\u8f83\uff0cLisp \u7684\u72b6\u51b5\u7cfb\u7edf\u7406\u89e3\u8d77\u6765\u662f\u6709\u70b9\u8d39\u52b2\u7684\uff0c\u4f46\u7528\u6cd5\u5374\u6bd4\u8f83\u7b80\u5355\u3002\u8c8c\u4f3c\u5b83\u7684\u4e00\u4e9b\u5176\u5b83\u7279\u6027\u4e5f\u662f\u8fd9\u6837\u5b50\u3002<\/p>\n<p>\u72b6\u51b5\u7cfb\u7edf\u5c06\u6574\u4e2a\u8fc7\u7a0b\u5206\u6210\uff1a\u4ea7\u751f\uff08<strong>signaling<\/strong>\uff09\u72b6\u51b5\uff0c\u5904\u7406\uff08<strong>handling<\/strong>\uff09\u5b83\u4ee5\u53ca\u518d\u542f\u52a8\uff08<strong>restarting<\/strong>\uff09\u3002\u4e0e\u7a81\u51fa\u5f02\u5e38\u7684\u60c5\u51b5\u4e0d\u540c\uff0cLisp \u66f4\u503e\u5411\u4e8e\u5904\u7406\u5e76\u4ece\u9519\u8bef\u72b6\u51b5\u4e2d\u6062\u590d\u3002\u4f46\u662f\uff0c\u5e95\u5c42\u51fd\u6570\u4e2d\u662f\u53ef\u4ee5\u4e0d\u51b3\u5b9a\u9009\u62e9\u54ea\u79cd\u6062\u590d\u7b56\u7565\uff0c\u5c06\u51b3\u5b9a\u6743\u7559\u7ed9\u9ad8\u5c42\u51fd\u6570\u7684\u4ee3\u7801\u3002\u6211\u4eec\u8981\u505a\u7684\u4e8b\u60c5\u5c31\u662f\u601d\u8003\u4f1a\u51fa\u73b0\u4ec0\u4e48\u72b6\u51b5\uff0c\u51fa\u73b0\u72b6\u51b5\u7684\u65f6\u5019\u6709\u4ec0\u4e48\u7b56\u7565\u6765\u5e94\u5bf9\uff0c\u4ee5\u4f7f\u7a0b\u5e8f\u53ef\u4ee5\u7ee7\u7eed\u8fd0\u884c\u4e0b\u53bb\u3002\u8fd9\u8981\u6c42\u6bd4\u7b80\u5355\u6355\u6349\u6216\u629b\u51fa\u5f02\u5e38\u8981\u66f4\u8fdb\u4e86\u4e00\u6b65\u3002<\/p>\n<p>\u4e0b\u9762 Lisp \u4ee3\u7801\u5f15\u7528\u81ea <a href=\"http:\/\/book.douban.com\/subject\/6859720\/\" target=\"_blank\">\u300a\u5b9e\u7528 Common Lisp \u7f16\u7a0b\u300b<\/a>\u3002<\/p>\n<pre class=\"brush: clj\">\r\n(define-condition malformed-log-entry-error (error)\r\n  ((text :initarg :text :reader text)))\r\n\r\n(defun skip-log-entry (c)\r\n  (invoke-restart &#039;skip-log-entry))\r\n\r\n;; set up the conditions\r\n(defun parse-log-entry (text)\r\n  (if (well-formed-log-entry-p text)\r\n    (make-instance &#039;log-entry \u2026)\r\n    (restart-case (error &#039;malformed-log-entry-error :text text)\r\n      (use-value (value) value)\r\n      (reparse-entry (fixed-text) (parse-log-entry fixed-text)))))\r\n\r\n;; &#039;skip-log-entry&#039; will be shown in the debugger when error occur\r\n(defun parse-log-file (file)\r\n  (with-open-file (in file :direction :input)\r\n    (loop for text = (read-line in nil nil) while text\r\n       for entry = (restart-case (parse-log-entry text)\r\n                     (skip-log-entry () nil))\r\n       when entry collect it)))\r\n\r\n(defun analyze-log (log)\r\n  (dolist (entry (parse-log-file log))\r\n    (analyze-entry entry)))\r\n\r\n;; handle the condition\r\n(defun log-analyzer ()\r\n  (handler-bind ((malformed-log-entry-error\r\n                  #&#039;(lambda (c)\r\n                      (use-value\r\n                       (make-instance &#039;malformed-log-entry :text (text c))))))\r\n    (dolist (log (find-all-logs))\r\n      (analyze-log log))))\r\n<\/pre>\n<p>\u8c03\u7528\u662f\u4ece log-analyzer > analyze-log > parse-log-file > parse-log-entry \u9010\u5c42\u6df1\u5165\u3002\u5728 parse-log-entry \u4ea7\u751f\u72b6\u51b5\uff08malformed-log-entry-error\uff09\u3002\u7136\u540e\u53ef\u4ee5\u4f7f\u7528 <strong>HANDLER-CASE<\/strong> \u5b8f\u6765\u5b9a\u4e49\u72b6\u51b5\u5904\u7406\u5668\uff08handler\uff09\uff0c\u4ee5\u54cd\u5e94\u72b6\u51b5\u7684\u4fe1\u53f7\u5904\u7406\uff0c\u4f46\u5982\u679c\u53ea\u662f\u8fd9\u6837\u7684\u8bdd\uff0c\u5c31\u548c\u666e\u901a\u5f02\u5e38\u5904\u7406\u6ca1\u4ec0\u4e48\u533a\u522b\u3002<\/p>\n<p>\u72b6\u51b5\u7cfb\u7edf\u5141\u8bb8\u6211\u4eec\u5c06\u5904\u7406\u4ee3\u7801\u62c6\u5206\u6210\u4e24\u90e8\u5206\uff1a\u4ece\u9519\u8bef\u4e2d\u6062\u590d\u7528\u7684\u518d\u542f\u52a8\uff08restart\uff09\uff0c\u548c\u72b6\u51b5\u5904\u7406\u5668\uff08handler\uff09\u3002\u524d\u8005\u53ef\u4ee5\u4f4d\u4e8e\u8f83\u4e0b\u5c42\u7684\u51fd\u6570\uff0c\u540e\u8005\u4f4d\u4e8e\u66f4\u4e0a\u5c42\u3002<\/p>\n<p>Restart \u63d0\u4f9b\u4e86\u53ef\u9009\u7684\u5904\u7406\u7b56\u7565\uff0c\u56e0\u4e3a\u5e76\u4e0d\u662f\u6240\u6709\u7684\u5e94\u7528\u90fd\u4f1a\u91c7\u53d6\u67d0\u79cd\u56fa\u5b9a\u7684\u5904\u7406\u65b9\u5f0f\u3002\u6bd4\u5982\uff0c\u4f8b\u5b50\u4e2d parse-log-entry \u63d0\u4f9b\u4e86 use-value \u548c reparse-entry \u4e24\u79cd\u5904\u7406\u65b9\u5f0f\uff0c\u53ef\u4ee5\u76f4\u63a5\u63d0\u4f9b\u4e00\u4e2a\u503c\uff0c\u6216\u8005\u4fee\u590d\u6570\u636e\u540e\u91cd\u65b0\u5206\u6790\u3002\u591a\u4e2a\u5904\u7406\u7b56\u7565\u6709\u65f6\u662f\u5f88\u6709\u7528\u751a\u81f3\u662f\u5fc5\u987b\u7684\u3002\u5e94\u7528\u9700\u8981\u8fd0\u884c\u5728\u6d4b\u8bd5\u73af\u5883\u4e0e\u751f\u4ea7\u73af\u5883\u7b49\u4e0d\u540c\u73af\u5883\u4e2d\u65f6\uff0c\u5c31\u6709\u8bb8\u591a\u9700\u8981\u4e0d\u540c\u7684\u5904\u7406\u7b56\u7565\u7684\u60c5\u51b5\u3002<\/p>\n<p>log-analyzer \u662f\u9876\u5c42\u51fd\u6570\uff0c\u51fd\u6570\u5185\u901a\u8fc7 <strong>HANDLER-BIND<\/strong> \u5b8f\u7ed1\u5b9a\u72b6\u51b5\u5904\u7406\u5668\u3002\u91cd\u70b9\u5728\u4e8e\uff0c\u5f53\u4e0b\u5c42\u51fd\u6570\u51fa\u73b0\u72b6\u51b5\u65f6\uff0c\u4e0a\u5c42\u51fd\u6570\u53ef\u4ee5\u9009\u62e9\u5904\u7406\u7b56\u7565\uff0c\u800c\u6267\u884c\u5904\u7406\u7b56\u7565\u7684\u8fd8\u662f\u5728\u4e0b\u5c42\u51fd\u6570\u7684\u8c03\u7528\u6808\u5185\uff0c\u56e0\u4e3a\u6574\u4e2a\u8fc7\u7a0b\u91cc\u6808\u662f\u4e0d\u5c55\u5f00\u7684\uff0c\u53ef\u4ee5\u5728\u539f\u6709\u4e0a\u4e0b\u6587\u73af\u5883\u4e0b\u7ee7\u7eed\u8fdb\u884c\u5904\u7406\u3002<\/p>\n<p>\u67d0\u79cd\u610f\u4e49\u4e0a\u8bb2\uff0c\u72b6\u51b5\u7cfb\u7edf\u4e0e\u5f02\u5e38\u5904\u7406\u6839\u672c\u5c31\u4e0d\u662f\u4e00\u56de\u4e8b\u3002<\/p>\n<p>\u6211\u611f\u89c9\u5c31\u662f\u4e24\u79cd\u5b8c\u5168\u4e0d\u540c\u7684\u5904\u7406\u65b9\u5f0f\u3002\u5f53\u4f60\u4e0e\u4e00\u4e2a\u670b\u53cb\u56e0\u4e3a\u4e00\u4e9b\u72b6\u51b5\u53d1\u751f\u4e89\u6267\u65f6\uff0c\u5982\u679c\u8fd9\u4e8b\u5728\u4e24\u4eba\u4e4b\u95f4\u80fd\u5904\u7406\u6389\u90a3\u662f\u6700\u597d\uff0c\u5904\u7406\u4e0d\u4e86\u6216\u8005\u65e0\u6cd5\u51b3\u5b9a\u5904\u7406\u529e\u6cd5\uff0c\u62fc\u547d\u5730\u5f80\u4e0a\u51b2\u7a81\uff0c\u670b\u53cb\u5173\u7cfb\u53ef\u80fd\u5c31\u56e0\u6b64\u7834\u88c2\u4e86\uff0c\u5c31\u7b97\u540e\u9762\u518d\u52aa\u529b\u633d\u56de\uff0c\u91cd\u65b0\u5efa\u7acb\u7684\u5df2\u7ecf\u4e0d\u662f\u4ee5\u524d\u7684\u90a3\u4efd\u53cb\u60c5\u4e86\u3002\u4f46\u5982\u679c\u53ea\u8981\u6709\u4fee\u590d\u7684\u53ef\u80fd\uff0c\u4e0a\u5c42\u6709\u6bd4\u5982\u73cd\u60dc\u5f7c\u6b64\u591a\u5e74\u60c5\u8c0a\u8fd9\u6837\u7684\u5927\u524d\u63d0\uff0c\u662f\u80fd\u51b3\u51fa\u4e00\u79cd\u5904\u7406\u529e\u6cd5\u6765\u4fee\u590d\uff0c\u4ece\u800c\u5f88\u597d\u5730\u5ef6\u7eed\u4e86\u53cb\u60c5\u3002<\/p>\n<p>\u4e00\u5207\u90fd\u5728\u7ee7\u7eed\u3002<\/p>\n<p><em>PS: \u521d\u5b66 Lisp\uff0c\u5b9e\u8df5\u4e0d\u591a\uff0c\u5982\u679c\u7406\u89e3\u5f97\u4e0d\u6b63\u786e\uff0c\u8fd8\u671b\u770b\u5b98\u4eec\u591a\u591a\u63d0\u70b9\u3002<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u201cLisp \u7684\u72b6\u51b5\u7cfb\u7edf\uff08condition system\uff09\u662f\u5b83\u6700\u4f1f\u5927\u7684\u7279\u6027\u4e4b\u4e00\u3002\u201d \u6709\u6ca1\u6709\u6700\u4f1f\u5927\u6211\u8fd8\u4e0d\u662f\u5f88\u6e05\u695a &hellip; <\/p>\n<p class=\"link-more\"><a href=\"http:\/\/leegorous.net\/blog\/2012\/03\/02\/condition-system-vs-exception-handling\/\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">\u201c\u72b6\u51b5\u7cfb\u7edf \u4e0e \u5f02\u5e38\u5904\u7406\u201d<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[72],"tags":[65],"_links":{"self":[{"href":"http:\/\/leegorous.net\/blog\/wp-json\/wp\/v2\/posts\/1041"}],"collection":[{"href":"http:\/\/leegorous.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/leegorous.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/leegorous.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/leegorous.net\/blog\/wp-json\/wp\/v2\/comments?post=1041"}],"version-history":[{"count":6,"href":"http:\/\/leegorous.net\/blog\/wp-json\/wp\/v2\/posts\/1041\/revisions"}],"predecessor-version":[{"id":1046,"href":"http:\/\/leegorous.net\/blog\/wp-json\/wp\/v2\/posts\/1041\/revisions\/1046"}],"wp:attachment":[{"href":"http:\/\/leegorous.net\/blog\/wp-json\/wp\/v2\/media?parent=1041"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/leegorous.net\/blog\/wp-json\/wp\/v2\/categories?post=1041"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/leegorous.net\/blog\/wp-json\/wp\/v2\/tags?post=1041"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}