{"id":194,"date":"2016-07-31T08:06:07","date_gmt":"2016-07-31T08:06:07","guid":{"rendered":""},"modified":"2016-07-31T08:06:07","modified_gmt":"2016-07-31T08:06:07","slug":"","status":"publish","type":"post","link":"http:\/\/weizn.net\/?p=194","title":{"rendered":"[\u8f6c]Python\u5f02\u5e38\u5904\u7406"},"content":{"rendered":"<p style=\"line-height:25.2px;margin:10px auto;white-space:normal;\">\n\t<span style=\"font-size:14px;\"><\/p>\n<p style=\"margin:10px auto;padding:0px;line-height:24px;white-space:normal;\">\n\t\t<span style=\"font-size:14px;\">#!\/usr\/bin\/python<\/span>\n\t<\/p>\n<p style=\"margin:10px auto;padding:0px;line-height:24px;white-space:normal;\">\n\t\t<span style=\"font-size:14px;\">import traceback<\/span><br style=\"margin:0px;padding:0px;\" \/><br \/>\n<span style=\"font-size:14px;\">try:<\/span><br style=\"margin:0px;padding:0px;\" \/><br \/>\n<span style=\"font-size:14px;\">&nbsp;1\/0<\/span><br style=\"margin:0px;padding:0px;\" \/><br \/>\n<span style=\"font-size:14px;\">#except Exception,e:<\/span><br style=\"margin:0px;padding:0px;\" \/><br \/>\n<span style=\"font-size:14px;\">#&nbsp;print traceback.format_exc()<\/span><br style=\"margin:0px;padding:0px;\" \/><br \/>\n<span style=\"font-size:14px;\">&nbsp;&nbsp;<\/span><br style=\"margin:0px;padding:0px;\" \/><br \/>\n<span style=\"font-size:14px;\">except Exception as e:<\/span><br style=\"margin:0px;padding:0px;\" \/><br \/>\n<span style=\"font-size:14px;\">&nbsp;print e<\/span>\n\t<\/p>\n<p style=\"margin:10px auto;padding:0px;line-height:24px;white-space:normal;\">\n\t\t<span style=\"font-size:14px;\">#!\/usr\/bin\/python<\/span><br style=\"margin:0px;padding:0px;\" \/><br \/>\n<span style=\"font-size:14px;\">import traceback<\/span><br style=\"margin:0px;padding:0px;\" \/><br \/>\n<span style=\"font-size:14px;\">try:<\/span><br style=\"margin:0px;padding:0px;\" \/><br \/>\n<span style=\"font-size:14px;\">&nbsp;1\/0<\/span><br style=\"margin:0px;padding:0px;\" \/><br \/>\n<span style=\"font-size:14px;\">#except Exception,e:<\/span><br style=\"margin:0px;padding:0px;\" \/><br \/>\n<span style=\"font-size:14px;\">#&nbsp;print traceback.format_exc()<\/span><br style=\"margin:0px;padding:0px;\" \/><br \/>\n<span style=\"font-size:14px;\">&nbsp;&nbsp;<\/span><br style=\"margin:0px;padding:0px;\" \/><br \/>\n<span style=\"font-size:14px;\">except Exception , e:<\/span><br style=\"margin:0px;padding:0px;\" \/><br \/>\n<span style=\"font-size:14px;\">&nbsp;print e<\/span>\n\t<\/p>\n<p style=\"margin:10px auto;padding:0px;line-height:24px;white-space:normal;\">\n\t\t<span style=\"font-size:14px;\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Python\u7684\u5f02\u5e38\u5904\u7406\u80fd\u529b\u662f\u5f88\u5f3a\u5927\u7684\uff0c\u53ef\u5411\u7528\u6237\u51c6\u786e\u53cd\u9988\u51fa\u9519\u4fe1\u606f\u3002\u5728Python\u4e2d\uff0c\u5f02\u5e38\u4e5f\u662f\u5bf9\u8c61\uff0c\u53ef\u5bf9\u5b83\u8fdb\u884c\u64cd\u4f5c\u3002\u6240\u6709\u5f02\u5e38\u90fd\u662f\u57fa\u7c7bException\u7684\u6210\u5458\u3002\u6240\u6709\u5f02\u5e38\u90fd\u4ece\u57fa\u7c7bException\u7ee7\u627f\uff0c\u800c\u4e14\u90fd\u5728exceptions\u6a21\u5757\u4e2d\u5b9a\u4e49\u3002Python\u81ea\u52a8\u5c06\u6240\u6709\u5f02\u5e38\u540d\u79f0\u653e\u5728\u5185\u5efa\u547d\u540d\u7a7a\u95f4\u4e2d\uff0c\u6240\u4ee5\u7a0b\u5e8f\u4e0d\u5fc5\u5bfc\u5165exceptions\u6a21\u5757\u5373\u53ef\u4f7f\u7528\u5f02\u5e38\u3002\u4e00\u65e6\u5f15\u53d1\u800c\u4e14\u6ca1\u6709\u6355\u6349SystemExit\u5f02\u5e38\uff0c\u7a0b\u5e8f\u6267\u884c\u5c31\u4f1a\u7ec8\u6b62\u3002\u5982\u679c\u4ea4\u4e92\u5f0f\u4f1a\u8bdd\u9047\u5230\u4e00\u4e2a\u672a\u88ab\u6355\u6349\u7684SystemExit\u5f02\u5e38\uff0c\u4f1a\u8bdd\u5c31\u4f1a\u7ec8\u6b62\u3002<\/span>\n\t<\/p>\n<p style=\"margin:10px auto;padding:0px;line-height:24px;white-space:normal;\">\n\t\t<span style=\"font-size:14px;\">\u65b9\u5f0f\u4e00:try\u8bed\u53e5:<\/span>\n\t<\/p>\n<p style=\"margin:10px auto;padding:0px;line-height:24px;white-space:normal;\">\n\t\t<span style=\"font-size:14px;\">1\u4f7f\u7528try\u548cexcept\u8bed\u53e5\u6765\u6355\u83b7\u5f02\u5e38<\/span>\n\t<\/p>\n<p style=\"margin:10px auto;padding:0px;line-height:24px;white-space:normal;\">\n\t\t<span style=\"font-size:14px;\">try:<\/span><br style=\"margin:0px;padding:0px;\" \/><br \/>\n<span style=\"font-size:14px;\">&nbsp;&nbsp; block<\/span><br style=\"margin:0px;padding:0px;\" \/><br \/>\n<span style=\"font-size:14px;\">except [exception,[data\u2026]]:<\/span><br style=\"margin:0px;padding:0px;\" \/><br \/>\n<span style=\"font-size:14px;\">&nbsp;&nbsp; block<\/span>\n\t<\/p>\n<p style=\"margin:10px auto;padding:0px;line-height:24px;white-space:normal;\">\n\t\t<span style=\"font-size:14px;\">try:<\/span><br style=\"margin:0px;padding:0px;\" \/><br \/>\n<span style=\"font-size:14px;\">block<\/span><br style=\"margin:0px;padding:0px;\" \/><br \/>\n<span style=\"font-size:14px;\">except [exception,[data&#8230;]]:<\/span><br style=\"margin:0px;padding:0px;\" \/><br \/>\n<span style=\"font-size:14px;\">&nbsp;&nbsp; block<\/span><br style=\"margin:0px;padding:0px;\" \/><br \/>\n<span style=\"font-size:14px;\">else:<\/span><br style=\"margin:0px;padding:0px;\" \/><br \/>\n<span style=\"font-size:14px;\">&nbsp;&nbsp; block<\/span>\n\t<\/p>\n<p style=\"margin:10px auto;padding:0px;line-height:24px;white-space:normal;\">\n\t\t<span style=\"font-size:14px;\">\u8be5\u79cd\u5f02\u5e38\u5904\u7406\u8bed\u6cd5\u7684\u89c4\u5219\u662f\uff1a<\/span>\n\t<\/p>\n<p style=\"margin:10px auto;padding:0px;line-height:24px;white-space:normal;\">\n\t\t<span style=\"font-size:14px;\">\u00b7&nbsp;&nbsp; \u6267\u884ctry\u4e0b\u7684\u8bed\u53e5\uff0c\u5982\u679c\u5f15\u53d1\u5f02\u5e38\uff0c\u5219\u6267\u884c\u8fc7\u7a0b\u4f1a\u8df3\u5230\u7b2c\u4e00\u4e2aexcept\u8bed\u53e5\u3002<\/span>\n\t<\/p>\n<p style=\"margin:10px auto;padding:0px;line-height:24px;white-space:normal;\">\n\t\t<span style=\"font-size:14px;\">\u00b7&nbsp;&nbsp; \u5982\u679c\u7b2c\u4e00\u4e2aexcept\u4e2d\u5b9a\u4e49\u7684\u5f02\u5e38\u4e0e\u5f15\u53d1\u7684\u5f02\u5e38\u5339\u914d\uff0c\u5219\u6267\u884c\u8be5except\u4e2d\u7684\u8bed\u53e5\u3002<\/span>\n\t<\/p>\n<p style=\"margin:10px auto;padding:0px;line-height:24px;white-space:normal;\">\n\t\t<span style=\"font-size:14px;\">\u00b7&nbsp;&nbsp; \u5982\u679c\u5f15\u53d1\u7684\u5f02\u5e38\u4e0d\u5339\u914d\u7b2c\u4e00\u4e2aexcept\uff0c\u5219\u4f1a\u641c\u7d22\u7b2c\u4e8c\u4e2aexcept\uff0c\u5141\u8bb8\u7f16\u5199\u7684except\u6570\u91cf\u6ca1\u6709\u9650\u5236\u3002<\/span>\n\t<\/p>\n<p style=\"margin:10px auto;padding:0px;line-height:24px;white-space:normal;\">\n\t\t<span style=\"font-size:14px;\">\u00b7&nbsp;&nbsp; \u5982\u679c\u6240\u6709\u7684except\u90fd\u4e0d\u5339\u914d\uff0c\u5219\u5f02\u5e38\u4f1a\u4f20\u9012\u5230\u4e0b\u4e00\u4e2a\u8c03\u7528\u672c\u4ee3\u7801\u7684\u6700\u9ad8\u5c42try\u4ee3\u7801\u4e2d\u3002<\/span>\n\t<\/p>\n<p style=\"margin:10px auto;padding:0px;line-height:24px;white-space:normal;\">\n\t\t<span style=\"font-size:14px;\">\u00b7&nbsp;&nbsp; \u5982\u679c\u6ca1\u6709\u53d1\u751f\u5f02\u5e38\uff0c\u5219\u6267\u884celse\u5757\u4ee3\u7801\u3002<\/span>\n\t<\/p>\n<p style=\"margin:10px auto;padding:0px;line-height:24px;white-space:normal;\">\n\t\t<span style=\"font-size:14px;\">\u4f8b:<\/span>\n\t<\/p>\n<p style=\"margin:10px auto;padding:0px;line-height:24px;white-space:normal;\">\n\t\t<span style=\"font-size:14px;\">try:<\/span>\n\t<\/p>\n<p style=\"margin:10px auto;padding:0px;line-height:24px;white-space:normal;\">\n\t\t<span style=\"font-size:14px;\">&nbsp;&nbsp; f = open(\u201cfile.txt\u201d,\u201dr\u201d)<\/span><br style=\"margin:0px;padding:0px;\" \/><br \/>\n<span style=\"font-size:14px;\">except IOError, e:<\/span><br style=\"margin:0px;padding:0px;\" \/><br \/>\n<span style=\"font-size:14px;\">&nbsp;&nbsp; print e<\/span>\n\t<\/p>\n<p style=\"margin:10px auto;padding:0px;line-height:24px;white-space:normal;\">\n\t\t<span style=\"font-size:14px;\">\u6355\u83b7\u5230\u7684IOError\u9519\u8bef\u7684\u8be6\u7ec6\u539f\u56e0\u4f1a\u88ab\u653e\u7f6e\u5728\u5bf9\u8c61e\u4e2d,\u7136\u540e\u8fd0\u884c\u8be5\u5f02\u5e38\u7684except\u4ee3\u7801\u5757<\/span>\n\t<\/p>\n<p style=\"margin:10px auto;padding:0px;line-height:24px;white-space:normal;\">\n\t\t<span style=\"font-size:14px;\">\u6355\u83b7\u6240\u6709\u7684\u5f02\u5e38<\/span>\n\t<\/p>\n<p style=\"margin:10px auto;padding:0px;line-height:24px;white-space:normal;\">\n\t\t<span style=\"font-size:14px;\">try:<\/span><br style=\"margin:0px;padding:0px;\" \/><br \/>\n<span style=\"font-size:14px;\">&nbsp;&nbsp; a=b<\/span><br style=\"margin:0px;padding:0px;\" \/><br \/>\n<span style=\"font-size:14px;\">&nbsp;&nbsp; b=c<\/span><br style=\"margin:0px;padding:0px;\" \/><br \/>\n<span style=\"font-size:14px;\">except Exception,ex:<\/span><br style=\"margin:0px;padding:0px;\" \/><br \/>\n<span style=\"font-size:14px;\">&nbsp;&nbsp; print Exception,&#8221;:&#8221;,ex<\/span>\n\t<\/p>\n<p style=\"margin:10px auto;padding:0px;line-height:24px;white-space:normal;\">\n\t\t<span style=\"font-size:14px;\">\u4f7f\u7528except\u5b50\u53e5\u9700\u8981\u6ce8\u610f\u7684\u4e8b\u60c5\uff0c\u5c31\u662f\u591a\u4e2aexcept\u5b50\u53e5\u622a\u83b7\u5f02\u5e38\u65f6\uff0c\u5982\u679c\u5404\u4e2a\u5f02\u5e38\u7c7b\u4e4b\u95f4\u5177\u6709\u7ee7\u627f\u5173\u7cfb\uff0c\u5219\u5b50\u7c7b\u5e94\u8be5\u5199\u5728\u524d\u9762\uff0c\u5426\u5219\u7236\u7c7b\u5c06\u4f1a\u76f4\u63a5\u622a\u83b7\u5b50\u7c7b\u5f02\u5e38\u3002\u653e\u5728\u540e\u9762\u7684\u5b50\u7c7b\u5f02\u5e38\u4e5f\u5c31\u4e0d\u4f1a\u6267\u884c\u5230\u4e86\u3002<\/span>\n\t<\/p>\n<p style=\"margin:10px auto;padding:0px;line-height:24px;white-space:normal;\">\n\t\t<span style=\"font-size:14px;\">2 \u4f7f\u7528try\u8ddffinally:<\/span>\n\t<\/p>\n<p style=\"margin:10px auto;padding:0px;line-height:24px;white-space:normal;\">\n\t\t<span style=\"font-size:14px;\">\u8bed\u6cd5\u5982\u4e0b:<\/span>\n\t<\/p>\n<p style=\"margin:10px auto;padding:0px;line-height:24px;white-space:normal;\">\n\t\t<span style=\"font-size:14px;\">try:<\/span><br style=\"margin:0px;padding:0px;\" \/><br \/>\n<span style=\"font-size:14px;\">&nbsp;&nbsp; block<\/span><br style=\"margin:0px;padding:0px;\" \/><br \/>\n<span style=\"font-size:14px;\">finally:<\/span><br style=\"margin:0px;padding:0px;\" \/><br \/>\n<span style=\"font-size:14px;\">&nbsp;&nbsp; block<\/span>\n\t<\/p>\n<p style=\"margin:10px auto;padding:0px;line-height:24px;white-space:normal;\">\n\t\t<span style=\"font-size:14px;\">\u8be5\u8bed\u53e5\u7684\u6267\u884c\u89c4\u5219\u662f\uff1a<\/span>\n\t<\/p>\n<p style=\"margin:10px auto;padding:0px;line-height:24px;white-space:normal;\">\n\t\t<span style=\"font-size:14px;\">\u00b7&nbsp;&nbsp; \u6267\u884ctry\u4e0b\u7684\u4ee3\u7801\u3002<\/span>\n\t<\/p>\n<p style=\"margin:10px auto;padding:0px;line-height:24px;white-space:normal;\">\n\t\t<span style=\"font-size:14px;\">\u00b7&nbsp;&nbsp; \u5982\u679c\u53d1\u751f\u5f02\u5e38\uff0c\u5728\u8be5\u5f02\u5e38\u4f20\u9012\u5230\u4e0b\u4e00\u7ea7try\u65f6\uff0c\u6267\u884cfinally\u4e2d\u7684\u4ee3\u7801\u3002<\/span>\n\t<\/p>\n<p style=\"margin:10px auto;padding:0px;line-height:24px;white-space:normal;\">\n\t\t<span style=\"font-size:14px;\">\u00b7&nbsp;&nbsp; \u5982\u679c\u6ca1\u6709\u53d1\u751f\u5f02\u5e38\uff0c\u5219\u6267\u884cfinally\u4e2d\u7684\u4ee3\u7801\u3002<\/span>\n\t<\/p>\n<p style=\"margin:10px auto;padding:0px;line-height:24px;white-space:normal;\">\n\t\t<span style=\"font-size:14px;\">\u7b2c\u4e8c\u79cdtry\u8bed\u6cd5\u5728\u65e0\u8bba\u6709\u6ca1\u6709\u53d1\u751f\u5f02\u5e38\u90fd\u8981\u6267\u884c\u4ee3\u7801\u7684\u60c5\u51b5\u4e0b\u662f\u5f88\u6709\u7528\u7684\u3002\u4f8b\u5982\u6211\u4eec\u5728python\u4e2d\u6253\u5f00\u4e00\u4e2a\u6587\u4ef6\u8fdb\u884c\u8bfb\u5199\u64cd\u4f5c\uff0c\u6211\u5728\u64cd\u4f5c\u8fc7\u7a0b\u4e2d\u4e0d\u7ba1\u662f\u5426\u51fa\u73b0\u5f02\u5e38\uff0c\u6700\u7ec8\u90fd\u662f\u8981\u628a\u8be5\u6587\u4ef6\u5173\u95ed\u7684\u3002<\/span>\n\t<\/p>\n<p style=\"margin:10px auto;padding:0px;line-height:24px;white-space:normal;\">\n\t\t<span style=\"font-size:14px;\">\u8fd9\u4e24\u79cd\u5f62\u5f0f\u76f8\u4e92\u51b2\u7a81\uff0c\u4f7f\u7528\u4e86\u4e00\u79cd\u5c31\u4e0d\u5141\u8bb8\u4f7f\u7528\u53e6\u4e00\u79cd\uff0c\u800c\u529f\u80fd\u53c8\u5404\u5f02<\/span>\n\t<\/p>\n<p style=\"margin:10px auto;padding:0px;line-height:24px;white-space:normal;\">\n\t\t<span style=\"font-size:14px;\">2. \u7528raise\u8bed\u53e5\u624b\u5de5\u5f15\u53d1\u4e00\u4e2a\u5f02\u5e38:<\/span>\n\t<\/p>\n<p style=\"margin:10px auto;padding:0px;line-height:24px;white-space:normal;\">\n\t\t<span style=\"font-size:14px;\">raise [exception[,data]]<\/span>\n\t<\/p>\n<p style=\"margin:10px auto;padding:0px;line-height:24px;white-space:normal;\">\n\t\t<span style=\"font-size:14px;\">\u5728Python\u4e2d\uff0c\u8981\u60f3\u5f15\u53d1\u5f02\u5e38\uff0c\u6700\u7b80\u5355\u7684\u5f62\u5f0f\u5c31\u662f\u8f93\u5165\u5173\u952e\u5b57raise\uff0c\u540e\u8ddf\u8981\u5f15\u53d1\u7684\u5f02\u5e38\u7684\u540d\u79f0\u3002\u5f02\u5e38\u540d\u79f0\u6807\u8bc6\u51fa\u5177\u4f53\u7684\u7c7b\uff1aPython\u5f02\u5e38\u662f\u90a3\u4e9b\u7c7b\u7684\u5bf9\u8c61\u3002\u6267\u884craise\u8bed\u53e5\u65f6\uff0cPython\u4f1a\u521b\u5efa\u6307\u5b9a\u7684\u5f02\u5e38\u7c7b\u7684\u4e00\u4e2a\u5bf9\u8c61\u3002raise\u8bed\u53e5\u8fd8\u53ef\u6307\u5b9a\u5bf9\u5f02\u5e38\u5bf9\u8c61\u8fdb\u884c\u521d\u59cb\u5316\u7684\u53c2\u6570\u3002\u4e3a\u6b64\uff0c\u8bf7\u5728\u5f02\u5e38\u7c7b\u7684\u540d\u79f0\u540e\u6dfb\u52a0\u4e00\u4e2a\u9017\u53f7\u4ee5\u53ca\u6307\u5b9a\u7684\u53c2\u6570\uff08\u6216\u8005\u7531\u53c2\u6570\u6784\u6210\u7684\u4e00\u4e2a\u5143\u7ec4\uff09\u3002<\/span>\n\t<\/p>\n<p style=\"margin:10px auto;padding:0px;line-height:24px;white-space:normal;\">\n\t\t<span style=\"font-size:14px;\">\u4f8b:<\/span>\n\t<\/p>\n<p style=\"margin:10px auto;padding:0px;line-height:24px;white-space:normal;\">\n\t\t<span style=\"font-size:14px;\">try:<\/span><br style=\"margin:0px;padding:0px;\" \/><br \/>\n<span style=\"font-size:14px;\">&nbsp;&nbsp;&nbsp; raise MyError #\u81ea\u5df1\u629b\u51fa\u4e00\u4e2a\u5f02\u5e38<\/span><br style=\"margin:0px;padding:0px;\" \/><br \/>\n<span style=\"font-size:14px;\">except MyError:<\/span><br style=\"margin:0px;padding:0px;\" \/><br \/>\n<span style=\"font-size:14px;\">&nbsp;&nbsp;&nbsp; print &#8216;a error&#8217;<\/span>\n\t<\/p>\n<p style=\"margin:10px auto;padding:0px;line-height:24px;white-space:normal;\">\n\t\t<span style=\"font-size:14px;\">raise ValueError,\u2019invalid argument\u2019<\/span><br style=\"margin:0px;padding:0px;\" \/><br \/>\n<span style=\"font-size:14px;\">\u6355\u6349\u5230\u7684\u5185\u5bb9\u4e3a:<\/span>\n\t<\/p>\n<p style=\"margin:10px auto;padding:0px;line-height:24px;white-space:normal;\">\n\t\t<span style=\"font-size:14px;\">type = VauleError<\/span><br style=\"margin:0px;padding:0px;\" \/><br \/>\n<span style=\"font-size:14px;\">message = invalid argument<\/span>\n\t<\/p>\n<p style=\"margin:10px auto;padding:0px;line-height:24px;white-space:normal;\">\n\t\t<span style=\"font-size:14px;\">3.&nbsp;&nbsp; \u91c7\u7528traceback(\u8ddf\u8e2a)\u6a21\u5757\u67e5\u770b\u5f02\u5e38<\/span>\n\t<\/p>\n<p style=\"margin:10px auto;padding:0px;line-height:24px;white-space:normal;\">\n\t\t<span style=\"font-size:14px;\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \u53d1\u751f\u5f02\u5e38\u65f6\uff0cPython\u80fd\u201c\u8bb0\u4f4f\u201d\u5f15\u53d1\u7684\u5f02\u5e38\u4ee5\u53ca\u7a0b\u5e8f\u7684\u5f53\u524d\u72b6\u6001\u3002Python\u8fd8\u7ef4\u62a4\u7740traceback\uff08\u8ddf\u8e2a\uff09\u5bf9\u8c61\uff0c\u5176\u4e2d\u542b\u6709\u5f02\u5e38\u53d1\u751f\u65f6\u4e0e\u51fd\u6570\u8c03\u7528\u5806\u6808\u6709\u5173\u7684\u4fe1\u606f\u3002\u8bb0\u4f4f\uff0c\u5f02\u5e38\u53ef\u80fd\u5728\u4e00\u7cfb\u5217\u5d4c\u5957\u8f83\u6df1\u7684\u51fd\u6570\u8c03\u7528\u4e2d\u5f15\u53d1\u3002\u7a0b\u5e8f\u8c03\u7528\u6bcf\u4e2a\u51fd\u6570\u65f6\uff0cPython\u4f1a\u5728\u201c\u51fd\u6570\u8c03\u7528\u5806\u6808\u201d\u7684\u8d77\u59cb\u5904\u63d2\u5165\u51fd\u6570\u540d\u3002\u4e00\u65e6\u5f02\u5e38\u88ab\u5f15\u53d1\uff0cPython\u4f1a\u641c\u7d22\u4e00\u4e2a\u76f8\u5e94\u7684\u5f02\u5e38\u5904\u7406\u7a0b\u5e8f\u3002\u5982\u679c\u5f53\u524d\u51fd\u6570\u4e2d\u6ca1\u6709\u5f02\u5e38\u5904\u7406\u7a0b\u5e8f\uff0c\u5f53\u524d\u51fd\u6570\u4f1a\u7ec8\u6b62\u6267\u884c\uff0cPython\u4f1a\u641c\u7d22\u5f53\u524d\u51fd\u6570\u7684\u8c03\u7528\u51fd\u6570\uff0c\u5e76\u4ee5\u6b64\u7c7b\u63a8\uff0c\u76f4\u5230\u53d1\u73b0\u5339\u914d\u7684\u5f02\u5e38\u5904\u7406\u7a0b\u5e8f\uff0c\u6216\u8005Python\u62b5\u8fbe\u4e3b\u7a0b\u5e8f\u4e3a\u6b62\u3002\u8fd9\u4e00\u67e5\u627e\u5408\u9002\u7684\u5f02\u5e38\u5904\u7406\u7a0b\u5e8f\u7684\u8fc7\u7a0b\u5c31\u79f0\u4e3a\u201c\u5806\u6808\u8f97\u8f6c\u5f00\u89e3\u201d\uff08Stack Unwinding\uff09\u3002\u89e3\u91ca\u5668\u4e00\u65b9\u9762\u7ef4\u62a4\u7740\u4e0e\u653e\u7f6e\u5806\u6808\u4e2d\u7684\u51fd\u6570\u6709\u5173\u7684\u4fe1\u606f\uff0c\u53e6\u4e00\u65b9\u9762\u4e5f\u7ef4\u62a4\u7740\u4e0e\u5df2\u4ece\u5806\u6808\u4e2d\u201c\u8f97\u8f6c\u5f00\u89e3\u201d\u7684\u51fd\u6570\u6709\u5173\u7684\u4fe1\u606f\u3002<\/span>\n\t<\/p>\n<p style=\"margin:10px auto;padding:0px;line-height:24px;white-space:normal;\">\n\t\t<span style=\"font-size:14px;\">&nbsp;&nbsp; \u683c\u5f0f:<\/span>\n\t<\/p>\n<p style=\"margin:10px auto;padding:0px;line-height:24px;white-space:normal;\">\n\t\t<span style=\"font-size:14px;\">try:<\/span><br style=\"margin:0px;padding:0px;\" \/><br \/>\n<span style=\"font-size:14px;\">block<\/span><br style=\"margin:0px;padding:0px;\" \/><br \/>\n<span style=\"font-size:14px;\">except:<\/span><br style=\"margin:0px;padding:0px;\" \/><br \/>\n<span style=\"font-size:14px;\">&nbsp;&nbsp; traceback.print_exc()<\/span>\n\t<\/p>\n<p style=\"margin:10px auto;padding:0px;line-height:24px;white-space:normal;\">\n\t\t<span style=\"font-size:14px;\">\u793a\u4f8b:\u2026excpetion\/traceback.py<\/span>\n\t<\/p>\n<p style=\"margin:10px auto;padding:0px;line-height:24px;white-space:normal;\">\n\t\t<span style=\"font-size:14px;\">4. \u91c7\u7528sys\u6a21\u5757\u56de\u6eaf\u6700\u540e\u7684\u5f02\u5e38<\/span>\n\t<\/p>\n<p style=\"margin:10px auto;padding:0px;line-height:24px;white-space:normal;\">\n\t\t<span style=\"font-size:14px;\">import sys<\/span><br style=\"margin:0px;padding:0px;\" \/><br \/>\n<span style=\"font-size:14px;\">try:<\/span><br style=\"margin:0px;padding:0px;\" \/><br \/>\n<span style=\"font-size:14px;\">&nbsp;&nbsp; block<\/span><br style=\"margin:0px;padding:0px;\" \/><br \/>\n<span style=\"font-size:14px;\">except:<\/span><br style=\"margin:0px;padding:0px;\" \/><br \/>\n<span style=\"font-size:14px;\">&nbsp;&nbsp; info=sys.exc_info()<\/span><br style=\"margin:0px;padding:0px;\" \/><br \/>\n<span style=\"font-size:14px;\">&nbsp;&nbsp; print info[0],&#8221;:&#8221;,info[1]<\/span>\n\t<\/p>\n<p style=\"margin:10px auto;padding:0px;line-height:24px;white-space:normal;\">\n\t\t<span style=\"font-size:14px;\">\u6216\u8005\u4ee5\u5982\u4e0b\u7684\u5f62\u5f0f:<\/span>\n\t<\/p>\n<p style=\"margin:10px auto;padding:0px;line-height:24px;white-space:normal;\">\n\t\t<span style=\"font-size:14px;\">import sys<\/span><br style=\"margin:0px;padding:0px;\" \/><br \/>\n<span style=\"font-size:14px;\">&nbsp;&nbsp;&nbsp; tp,val,td = sys.exc_info()<\/span>\n\t<\/p>\n<p style=\"margin:10px auto;padding:0px;line-height:24px;white-space:normal;\">\n\t\t<span style=\"font-size:14px;\">sys.exc_info()\u7684\u8fd4\u56de\u503c\u662f\u4e00\u4e2atuple, (type, value\/message, traceback)<\/span>\n\t<\/p>\n<p style=\"margin:10px auto;padding:0px;line-height:24px;white-space:normal;\">\n\t\t<span style=\"font-size:14px;\">\u8fd9\u91cc\u7684type &#8212;- \u5f02\u5e38\u7684\u7c7b\u578b<\/span>\n\t<\/p>\n<p style=\"margin:10px auto;padding:0px;line-height:24px;white-space:normal;\">\n\t\t<span style=\"font-size:14px;\">value\/message &#8212;- \u5f02\u5e38\u7684\u4fe1\u606f\u6216\u8005\u53c2\u6570<\/span>\n\t<\/p>\n<p style=\"margin:10px auto;padding:0px;line-height:24px;white-space:normal;\">\n\t\t<span style=\"font-size:14px;\">traceback &#8212;- \u5305\u542b\u8c03\u7528\u6808\u4fe1\u606f\u7684\u5bf9\u8c61\u3002<\/span>\n\t<\/p>\n<p style=\"margin:10px auto;padding:0px;line-height:24px;white-space:normal;\">\n\t\t<span style=\"font-size:14px;\">\u4ece\u8fd9\u70b9\u4e0a\u53ef\u4ee5\u770b\u51fa\u6b64\u65b9\u6cd5\u6db5\u76d6\u4e86traceback.<\/span>\n\t<\/p>\n<p style=\"margin:10px auto;padding:0px;line-height:24px;white-space:normal;\">\n\t\t<span style=\"font-size:14px;\">5. \u5f02\u5e38\u5904\u7406\u7684\u4e00\u4e9b\u5176\u5b83\u7528\u9014<\/span>\n\t<\/p>\n<p style=\"margin:10px auto;padding:0px;line-height:24px;white-space:normal;\">\n\t\t<span style=\"font-size:14px;\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \u9664\u4e86\u5904\u7406\u5b9e\u9645\u7684\u9519\u8bef\u6761\u4ef6\u4e4b\u5916\uff0c\u5bf9\u4e8e\u5f02\u5e38\u8fd8\u6709\u8bb8\u591a\u5176\u5b83\u7684\u7528\u5904\u3002\u5728\u6807\u51c6 Python \u5e93\u4e2d\u4e00\u4e2a\u666e\u901a\u7684\u7528\u6cd5\u5c31\u662f\u8bd5\u7740\u5bfc\u5165\u4e00\u4e2a\u6a21\u5757\uff0c\u7136\u540e\u68c0\u67e5\u662f\u5426\u5b83\u80fd\u4f7f\u7528\u3002\u5bfc\u5165\u4e00\u4e2a\u5e76\u4e0d\u5b58\u5728\u7684\u6a21\u5757\u5c06\u5f15\u53d1\u4e00\u4e2a ImportError \u5f02\u5e38\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528\u8fd9\u79cd\u65b9\u6cd5\u6765\u5b9a\u4e49\u591a\u7ea7\u522b\u7684\u529f\u80fd\u2015\u2015\u4f9d\u9760\u5728\u8fd0\u884c\u65f6\u54ea\u4e2a\u6a21\u5757\u662f\u6709\u6548\u7684\uff0c\u6216\u652f\u6301\u591a\u79cd\u5e73\u53f0 (\u5373\u5e73\u53f0\u7279\u5b9a\u4ee3\u7801\u88ab\u5206\u79bb\u5230\u4e0d\u540c\u7684\u6a21\u5757\u4e2d)\u3002<\/span>\n\t<\/p>\n<p style=\"margin:10px auto;padding:0px;line-height:24px;white-space:normal;\">\n\t\t<span style=\"font-size:14px;\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \u4f60\u4e5f\u80fd\u901a\u8fc7\u521b\u5efa\u4e00\u4e2a\u4ece\u5185\u7f6e\u7684 Exception \u7c7b\u7ee7\u627f\u7684\u7c7b\u5b9a\u4e49\u4f60\u81ea\u5df1\u7684\u5f02\u5e38\uff0c\u7136\u540e\u4f7f\u7528 raise \u547d\u4ee4\u5f15\u53d1\u4f60\u7684\u5f02\u5e38\u3002\u5982\u679c\u4f60\u5bf9\u6b64\u611f\u5174\u8da3\uff0c\u8bf7\u770b\u8fdb\u4e00\u6b65\u9605\u8bfb\u7684\u90e8\u5206\u3002<\/span>\n\t<\/p>\n<p style=\"margin:10px auto;padding:0px;line-height:24px;white-space:normal;\">\n\t\t<span style=\"font-size:14px;\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \u4e0b\u9762\u7684\u4f8b\u5b50\u6f14\u793a\u4e86\u5982\u4f55\u4f7f\u7528\u5f02\u5e38\u652f\u6301\u7279\u5b9a\u5e73\u53f0\u529f\u80fd\u3002\u4ee3\u7801\u6765\u81ea getpass \u6a21\u5757\uff0c\u4e00\u4e2a\u4ece\u7528\u6237\u83b7\u5f97\u53e3\u4ee4\u7684\u5c01\u88c5\u6a21\u5757\u3002\u83b7\u5f97\u53e3\u4ee4\u5728 UNIX\u3001Windows \u548c Mac OS \u5e73\u53f0\u4e0a\u7684\u5b9e\u73b0\u662f\u4e0d\u540c\u7684\uff0c\u4f46\u662f\u8fd9\u4e2a\u4ee3\u7801\u5c01\u88c5\u4e86\u6240\u6709\u7684\u4e0d\u540c\u4e4b\u5904\u3002<\/span>\n\t<\/p>\n<p style=\"margin:10px auto;padding:0px;line-height:24px;white-space:normal;\">\n\t\t<span style=\"font-size:14px;\">\u4f8b\u652f\u6301\u7279\u5b9a\u5e73\u53f0\u529f\u80fd<\/span>\n\t<\/p>\n<p style=\"margin:10px auto;padding:0px;line-height:24px;white-space:normal;\">\n\t\t<span style=\"font-size:14px;\"># Bind the name getpass to the appropriate function<\/span>\n\t<\/p>\n<p style=\"margin:10px auto;padding:0px;line-height:24px;white-space:normal;\">\n\t\t<span style=\"font-size:14px;\">try:<\/span><br style=\"margin:0px;padding:0px;\" \/><br \/>\n<span style=\"font-size:14px;\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; import termios, TERMIOS&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><br style=\"margin:0px;padding:0px;\" \/><br \/>\n<span style=\"font-size:14px;\">except ImportError:<\/span><br style=\"margin:0px;padding:0px;\" \/><br \/>\n<span style=\"font-size:14px;\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; try:<\/span><br style=\"margin:0px;padding:0px;\" \/><br \/>\n<span style=\"font-size:14px;\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; import msvcrt&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><br style=\"margin:0px;padding:0px;\" \/><br \/>\n<span style=\"font-size:14px;\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; except ImportError:<\/span><br style=\"margin:0px;padding:0px;\" \/><br \/>\n<span style=\"font-size:14px;\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; try:<\/span><br style=\"margin:0px;padding:0px;\" \/><br \/>\n<span style=\"font-size:14px;\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; from EasyDialogs import AskPassword<\/span><br style=\"margin:0px;padding:0px;\" \/><br \/>\n<span style=\"font-size:14px;\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; except ImportError:<\/span><br style=\"margin:0px;padding:0px;\" \/><br \/>\n<span style=\"font-size:14px;\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; getpass = default_getpass&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><br style=\"margin:0px;padding:0px;\" \/><br \/>\n<span style=\"font-size:14px;\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; else:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><br style=\"margin:0px;padding:0px;\" \/><br \/>\n<span style=\"font-size:14px;\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; getpass = AskPassword<\/span><br style=\"margin:0px;padding:0px;\" \/><br \/>\n<span style=\"font-size:14px;\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; else:<\/span><br style=\"margin:0px;padding:0px;\" \/><br \/>\n<span style=\"font-size:14px;\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; getpass = win_getpass<\/span><br style=\"margin:0px;padding:0px;\" \/><br \/>\n<span style=\"font-size:14px;\">else:<\/span><br style=\"margin:0px;padding:0px;\" \/><br \/>\n<span style=\"font-size:14px;\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; getpass = unix_getpass<\/span>\n\t<\/p>\n<p style=\"margin:10px auto;padding:0px;line-height:24px;white-space:normal;\">\n\t\t<span style=\"font-size:14px;\">&nbsp;&nbsp;&nbsp;<\/span>\n\t<\/p>\n<p style=\"margin:10px auto;padding:0px;line-height:24px;white-space:normal;\">\n\t\t<span style=\"font-size:14px;\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; termios \u662f UNIX \u72ec\u6709\u7684\u4e00\u4e2a\u6a21\u5757\uff0c\u5b83\u63d0\u4f9b\u4e86\u5bf9\u4e8e\u8f93\u5165\u7ec8\u7aef\u7684\u5e95\u5c42\u63a7\u5236\u3002\u5982\u679c\u8fd9\u4e2a\u6a21\u5757\u65e0\u6548 (\u56e0\u4e3a\u5b83\u4e0d\u5728\u4f60\u7684\u7cfb\u7edf\u4e0a\uff0c\u6216\u4f60\u7684\u7cfb\u7edf\u4e0d\u652f\u6301\u5b83)\uff0c\u5219\u5bfc\u5165\u5931\u8d25\uff0cPython \u5f15\u53d1\u6211\u4eec\u6355\u6349\u7684 ImportError \u5f02\u5e38\u3002<\/span>\n\t<\/p>\n<p style=\"margin:10px auto;padding:0px;line-height:24px;white-space:normal;\">\n\t\t<span style=\"font-size:14px;\">&nbsp;&nbsp;&nbsp;<\/span>\n\t<\/p>\n<p style=\"margin:10px auto;padding:0px;line-height:24px;white-space:normal;\">\n\t\t<span style=\"font-size:14px;\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; OK\uff0c\u6211\u4eec\u6ca1\u6709 termios\uff0c\u6240\u4ee5\u8ba9\u6211\u4eec\u8bd5\u8bd5 msvcrt\uff0c\u5b83\u662f Windows \u72ec\u6709\u7684\u4e00\u4e2a\u6a21\u5757\uff0c\u53ef\u4ee5\u63d0\u4f9b\u5728 Microsoft Visual C++ \u8fd0\u884c\u670d\u52a1\u4e2d\u7684\u8bb8\u591a\u6709\u7528\u7684\u51fd\u6570\u7684\u4e00\u4e2aAPI\u3002\u5982\u679c\u5bfc\u5165\u5931\u8d25\uff0cPython \u4f1a\u5f15\u53d1\u6211\u4eec\u6355\u6349\u7684 ImportError \u5f02\u5e38\u3002<\/span>\n\t<\/p>\n<p style=\"margin:10px auto;padding:0px;line-height:24px;white-space:normal;\">\n\t\t<span style=\"font-size:14px;\">&nbsp;&nbsp;&nbsp;<\/span>\n\t<\/p>\n<p style=\"margin:10px auto;padding:0px;line-height:24px;white-space:normal;\">\n\t\t<span style=\"font-size:14px;\">\u5982\u679c\u524d\u4e24\u4e2a\u4e0d\u80fd\u5de5\u4f5c\uff0c\u6211\u4eec\u8bd5\u7740\u4ece EasyDialogs \u5bfc\u5165\u4e00\u4e2a\u51fd\u6570\uff0c\u5b83\u662f Mac OS \u72ec\u6709\u7684\u4e00\u4e2a\u6a21\u5757\uff0c\u63d0\u4f9b\u4e86\u5404\u79cd\u5404\u6837\u7c7b\u578b\u7684\u5f39\u51fa\u5bf9\u8bdd\u6846\u3002\u518d\u4e00\u6b21\uff0c\u5982\u679c\u5bfc\u5165\u5931\u8d25\uff0cPython \u4f1a\u5f15\u53d1\u4e00\u4e2a\u6211\u4eec\u6355\u6349\u7684 ImportError \u5f02\u5e38\u3002<\/span>\n\t<\/p>\n<p style=\"margin:10px auto;padding:0px;line-height:24px;white-space:normal;\">\n\t\t<span style=\"font-size:14px;\">&nbsp;&nbsp;&nbsp;<\/span>\n\t<\/p>\n<p style=\"margin:10px auto;padding:0px;line-height:24px;white-space:normal;\">\n\t\t<span style=\"font-size:14px;\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \u8fd9\u4e9b\u5e73\u53f0\u7279\u5b9a\u7684\u6a21\u5757\u6ca1\u6709\u4e00\u4e2a\u6709\u6548 (\u6709\u53ef\u80fd\uff0c\u56e0\u4e3a Python \u5df2\u7ecf\u79fb\u690d\u5230\u4e86\u8bb8\u591a\u4e0d\u540c\u7684\u5e73\u53f0\u4e0a\u4e86)\uff0c\u6240\u4ee5\u6211\u4eec\u9700\u8981\u56de\u5934\u4f7f\u7528\u4e00\u4e2a\u7f3a\u7701\u53e3\u4ee4\u8f93\u5165\u51fd\u6570 (\u8fd9\u4e2a\u51fd\u6570\u5b9a\u4e49\u5728 getpass \u6a21\u5757\u4e2d\u7684\u522b\u7684\u5730\u65b9)\u3002\u6ce8\u610f\u6211\u4eec\u5728\u8fd9\u91cc\u6240\u505a\u7684\uff1a\u6211\u4eec\u5c06\u51fd\u6570 default_getpass \u8d4b\u7ed9\u53d8\u91cf getpass\u3002\u5982\u679c\u4f60\u8bfb\u4e86\u5b98\u65b9 getpass \u6587\u6863\uff0c\u5b83\u4f1a\u544a\u8bc9\u4f60 getpass \u6a21\u5757\u5b9a\u4e49\u4e86\u4e00\u4e2a getpass \u51fd\u6570\u3002\u5b83\u662f\u8fd9\u6837\u505a\u7684\uff1a\u901a\u8fc7\u7ed1\u5b9a getpass \u5230\u6b63\u786e\u7684\u51fd\u6570\u6765\u9002\u5e94\u4f60\u7684\u5e73\u53f0\u3002\u7136\u540e\u5f53\u4f60\u8c03\u7528 getpass \u51fd\u6570\u65f6\uff0c\u4f60\u5b9e\u9645\u4e0a\u8c03\u7528\u4e86\u5e73\u53f0\u7279\u5b9a\u7684\u51fd\u6570\uff0c\u662f\u8fd9\u6bb5\u4ee3\u7801\u5df2\u7ecf\u4e3a\u4f60\u8bbe\u7f6e\u597d\u7684\u3002\u4f60\u4e0d\u9700\u8981\u77e5\u9053\u6216\u5173\u5fc3\u4f60\u7684\u4ee3\u7801\u6b63\u8fd0\u884c\u5728\u4f55\u79cd\u5e73\u53f0\u4e0a\uff1b\u53ea\u8981\u8c03\u7528 getpass\uff0c\u5219\u5b83\u603b\u80fd\u6b63\u786e\u5904\u7406\u3002<\/span>\n\t<\/p>\n<p style=\"margin:10px auto;padding:0px;line-height:24px;white-space:normal;\">\n\t\t<span style=\"font-size:14px;\">&nbsp;&nbsp;&nbsp;<\/span>\n\t<\/p>\n<p style=\"margin:10px auto;padding:0px;line-height:24px;white-space:normal;\">\n\t\t<span style=\"font-size:14px;\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \u4e00\u4e2a try&#8230;except \u5757\u53ef\u4ee5\u6709\u4e00\u6761 else \u5b50\u53e5\uff0c\u5c31\u50cf if \u8bed\u53e5\u3002\u5982\u679c\u5728 try \u5757\u4e2d\u6ca1\u6709\u5f02\u5e38\u5f15\u53d1\uff0c\u7136\u540e else \u5b50\u53e5\u88ab\u6267\u884c\u3002\u5728\u672c\u4f8b\u4e2d\uff0c\u90a3\u5c31\u610f\u5473\u7740\u5982\u679c from EasyDialogs import AskPassword \u5bfc\u5165\u53ef\u5de5\u4f5c\uff0c\u6240\u4ee5\u6211\u4eec\u5e94\u8be5\u7ed1\u5b9a getpass \u5230 AskPassword \u51fd\u6570\u3002\u5176\u5b83\u6bcf\u4e2a try&#8230;except \u5757\u6709\u7740\u76f8\u4f3c\u7684 else \u5b50\u53e5\uff0c\u5f53\u6211\u4eec\u53d1\u73b0\u4e00\u4e2a import \u53ef\u7528\u65f6\uff0c\u5c31\u7ed1\u5b9a getpass \u5230\u9002\u5408\u7684\u51fd\u6570\u3002<\/span>\n\t<\/p>\n<p style=\"margin:10px auto;padding:0px;line-height:24px;white-space:normal;\">\n\t\t<span style=\"font-size:14px;\">\u672c\u6587\u6765\u81eaCSDN\u535a\u5ba2\uff0c\u8f6c\u8f7d\u8bf7\u6807\u660e\u51fa\u5904\uff1a<\/span><a href=\"http:\/\/blog.csdn.net\/JINXINXIN_BEAR_OS\/archive\/2011\/02\/23\/6202784.aspx\" style=\"margin:0px;padding:0px;border-bottom-width:1px;border-bottom-style:dotted;border-bottom-color:#333333;\"><span style=\"font-size:14px;\">http:\/\/blog.csdn.net\/JINXINXIN_BEAR_OS\/archive\/2011\/02\/23\/6202784.aspx<\/span><\/a>\n\t<\/p>\n<p><span style=\"font-size:14px;\"><\/span><br \/>\n<\/span>\n<\/p>\n<p style=\"line-height:25.2px;margin:10px auto;white-space:normal;\">\n\t<span style=\"font-size:14px;\">&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<\/span>\n<\/p>\n<p style=\"line-height:25.2px;margin:10px auto;white-space:normal;\">\n\t<span style=\"font-size:14px;\">\u4e4b\u524d\u5728\u5b66\u4e60python\u7684\u65f6\u5019\u6709\u6574\u7406\u8fc7python\u5f02\u5e38\u5904\u7406\u7684\u6587\u7ae0\uff0c\u4e0d\u591f\u7b80\u5355\u4e5f\u4e0d\u591f\u5b8c\u6574\uff0c\u6240\u4ee5\u51b3\u5b9a\u518d\u6574\u7406\u4e00\u7bc7\uff0c\u7b97\u505a\u8865\u5145\u3002<\/span>\n<\/p>\n<p style=\"line-height:25.2px;margin:10px auto;white-space:normal;\">\n\t<a href=\"http:\/\/www.cnblogs.com\/fnng\/archive\/2013\/04\/28\/3048356.html\" style=\"outline:none;border-bottom-width:1px;border-bottom-style:dashed;\"><span style=\"font-size:14px;\">http:\/\/www.cnblogs.com\/fnng\/archive\/2013\/04\/28\/3048356.html<\/span><\/a>\n<\/p>\n<p style=\"line-height:25.2px;margin:10px auto;white-space:normal;\">\n\t<span style=\"font-size:14px;\"><strong>python shell<\/strong><\/span>\n<\/p>\n<div class=\"cnblogs_code\" style=\"border:1px solid #CCCCCC;padding:5px;overflow:auto;margin:5px 0px;white-space:normal;\">\n<pre style=\"margin-top:0px;margin-bottom:0px;white-space:pre-wrap;word-wrap:break-word;max-width:600px;\">&gt;&gt;&gt; open('abc.txt','r')\nTraceback (most recent call last):\n  File \"&lt;stdin&gt;\", line 1, in &lt;module&gt; IOError: [Errno 2] No such file or directory: 'abc.txt'<\/pre>\n<\/div>\n<p class=\"p0\" style=\"line-height:25.2px;margin:10px auto;white-space:normal;\">\n\t<span style=\"font-size:14px;\"> \u6253\u5f00\u4e00\u4e2a\u4e0d\u5b58\u5728\u7684\u6587\u4ef6<\/span><span style=\"font-size:14px;\">abc.txt&nbsp;<\/span><span style=\"font-size:14px;\">\u6587\u4ef6\uff0c\u5f53\u7cfb\u7edf\u627e\u4e0d\u5230<\/span><span style=\"font-size:14px;\">abc.txt&nbsp;<\/span><span style=\"font-size:14px;\">\u6587\u4ef6\u65f6\uff0c\u5c31\u4f1a\u629b\u51fa\u7ed9\u6211\u4eec\u4e00\u4e2a<\/span><span style=\"font-size:14px;\">IOError\u7c7b\u578b\u7684\u9519\u8bef\uff0cNo&nbsp;such&nbsp;file&nbsp;or&nbsp;directory\uff1a<\/span><span style=\"font-size:14px;\">abc.txt&nbsp;<\/span><span style=\"font-size:14px;\">\uff08\u6ca1\u6709<\/span><span style=\"font-size:14px;\">abc.txt<\/span><span style=\"font-size:14px;\">\u8fd9\u6837\u7684\u6587\u4ef6\u6216\u76ee\u5f55\uff09<\/span>\n<\/p>\n<p class=\"p0\" style=\"line-height:25.2px;margin:10px auto;white-space:normal;\">\n\t<span style=\"font-size:14px;\"><strong>Try&#8230;except&#8230;<\/strong><\/span>\n<\/p>\n<p class=\"p0\" style=\"line-height:25.2px;margin:10px auto;white-space:normal;\">\n\t<span style=\"font-size:14px;\"> \u5047\u5982\uff0c\u6211\u4eec\u5df2\u7ecf\u77e5\u9053\u8fd9\u79cd\u7c7b\u578b\u7684\u9519\u8bef\uff0c\u90a3\u4e48\u5c31\u53ef\u4ee5\u901a\u8fc7\u4e00\u4e2a\u5f02\u5e38\u6251\u6349\u6765\u6251\u6349\u8fd9\u4e2a\u9519\u8bef\u3002\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7<\/span><span style=\"font-size:14px;\">try&#8230;except&nbsp;<\/span><span style=\"font-size:14px;\">\u6765\u63a5\u6536\u8fd9\u4e2a\u9519\u8bef\u3002\u6253\u5f00\u6587\u4ef6\u5199\u5165\uff1a<\/span>\n<\/p>\n<div class=\"cnblogs_code\" style=\"border:1px solid #CCCCCC;padding:5px;overflow:auto;margin:5px 0px;white-space:normal;\">\n<pre style=\"margin-top:0px;margin-bottom:0px;white-space:pre-wrap;word-wrap:break-word;max-width:600px;\">try:\n    open(\"abc.txt\",'r') except IOError: pass <\/pre>\n<\/div>\n<p class=\"p0\" style=\"line-height:25.2px;margin:10px auto;white-space:normal;\">\n\t<span style=\"font-size:14px;\">&nbsp;\u3000\u3000\u518d\u6765\u8fd0\u884c\u7a0b\u5e8f\u5c31\u4f1a\u770b\u4e0d\u5230\u4efb\u4f55\u9519\u8bef\uff0c\u56e0\u4e3a\u6211\u4eec\u7528<\/span><span style=\"font-size:14px;\">except&nbsp;<\/span><span style=\"font-size:14px;\">\u63a5\u6536\u4e86\u8fd9\u4e2a<\/span><span style=\"font-size:14px;\">IOError<\/span><span style=\"font-size:14px;\">\u9519\u8bef\u3002<\/span><span style=\"font-size:14px;\">pass&nbsp;<\/span><span style=\"font-size:14px;\">\u8868\u793a\u5b9e\u73b0\u4e86\u76f8\u5e94\u7684\u5b9e\u73b0\uff0c\u4f46\u4ec0\u4e48\u4e5f\u4e0d\u505a\u3002<\/span>\n<\/p>\n<p class=\"p0\" style=\"line-height:25.2px;margin:10px auto;white-space:normal;\">\n\t<span style=\"font-size:14px;\"> \u5047\u5982\u6211\u4e0d\u662f\u6253\u5f00\u4e00\u4e2a\u6587\u4ef6\uff0c\u800c\u662f\u8f93\u51fa\u4e00\u4e2a\u6ca1\u6709\u5b9a\u4e49\u7684\u53d8\u91cf\u5462\uff1f<\/span><span style=\"font-size:14px;\">&nbsp;<\/span>\n<\/p>\n<div class=\"cnblogs_code\" style=\"border:1px solid #CCCCCC;padding:5px;overflow:auto;margin:5px 0px;white-space:normal;\">\n<pre style=\"margin-top:0px;margin-bottom:0px;white-space:pre-wrap;word-wrap:break-word;max-width:600px;\">try: print aa except IOError: pass <\/pre>\n<\/div>\n<p class=\"p0\" style=\"line-height:25.2px;margin:10px auto;white-space:normal;\">\n\t<span style=\"font-size:14px;\">\u663e\u7136\uff0c\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\u6211\u5e76\u6ca1\u6709\u5bf9<\/span><span style=\"font-size:14px;\">aa&nbsp;<\/span><span style=\"font-size:14px;\">\u8d4b\u503c\uff0c\u8fd0\u884c\u7ed3\u679c\uff1a<\/span>\n<\/p>\n<div class=\"cnblogs_code\" style=\"border:1px solid #CCCCCC;padding:5px;overflow:auto;margin:5px 0px;white-space:normal;\">\n<pre style=\"margin-top:0px;margin-bottom:0px;white-space:pre-wrap;word-wrap:break-word;max-width:600px;\">Traceback (most recent call last):\n  File \"\/home\/fnngj\/py_se\/tryy.py\", line 3, in &lt;module&gt; print aa\nNameError: name 'aa' is not defined<\/pre>\n<\/div>\n<p class=\"p0\" style=\"line-height:25.2px;margin:10px auto;white-space:normal;\">\n\t<span style=\"font-size:14px;\"> \u6211\u4eec\u5df2\u7ecf\u7528<\/span><span style=\"font-size:14px;\">except&nbsp;<\/span><span style=\"font-size:14px;\">\u63a5\u6536\u9519\u8bef\u4e86\uff0c\u4e3a\u4ec0\u4e48\u9519\u8bef\u8fd8\u662f\u8fd8\u662f\u629b\u51fa\u6765\u4e86\u3002\u5982\u679c\u4f60\u7ec6\u5fc3\u4f1a\u53d1\u73b0\u8fd9\u4e00\u6b21\u7684\u9519\u8bef\u7c7b\u578b\u662f<\/span><span style=\"font-size:14px;\">NameError&nbsp;\uff0c\u800c\u6211\u63a5\u6536\u7c7b\u578b\u662f<\/span><span style=\"font-size:14px;\">IOError&nbsp;<\/span><span style=\"font-size:14px;\">\uff0c\u6240\u4ee5\u8981\u60f3\u63a5\u6536\u8fd9\u4e2a<\/span><span style=\"font-size:14px;\">print<\/span><span style=\"font-size:14px;\">\u7684\u9519\u8bef\uff0c\u90a3\u4e48\u9700\u8981\u4fee\u6539\u63a5\u6536\u9519\u8bef\u7684\u7c7b\u578b\u4e3a<\/span><span style=\"font-size:14px;\">NameError<\/span>\n<\/p>\n<p class=\"p0\" style=\"line-height:25.2px;margin:10px auto;white-space:normal;\">\n\t<span style=\"font-size:14px;\"> \u867d\u7136\uff0c\u6211\u77e5\u9053<\/span><span style=\"font-size:14px;\">print&nbsp;<\/span><span style=\"font-size:14px;\">\u8bed\u53e5\u662f\u53ef\u80fd\u4f1a\u629b\u4e00\u4e2a<\/span><span style=\"font-size:14px;\">NameError&nbsp;\u7c7b\u578b\u7684\u9519\u8bef\uff0c\u867d\u7136\u63a5\u6536\u4e86\u8fd9\u4e2a\u7c7b\u578b\u9519\u8bef\uff0c\u4f46\u6211\u4e0d\u77e5\u9053\u5177\u4f53\u7684\u9519\u8bef\u63d0\u793a\u4fe1\u606f\u662f\u4ec0\u4e48\u3002\u90a3\u4e48\uff0c\u6211\u80fd\u4e0d\u80fd\u628a\u9519\u8bef\u4fe1\u606f\u6253\u5370\u51fa\u6765\u5462\uff1f\u5f53\u7136\u53ef\u4ee5\uff1a<\/span>\n<\/p>\n<div class=\"cnblogs_code\" style=\"border:1px solid #CCCCCC;padding:5px;overflow:auto;margin:5px 0px;white-space:normal;\">\n<pre style=\"margin-top:0px;margin-bottom:0px;white-space:pre-wrap;word-wrap:break-word;max-width:600px;\">try: print aa except NameError, msg: print msg<\/pre>\n<\/div>\n<p class=\"p0\" style=\"line-height:25.2px;margin:10px auto;white-space:normal;\">\n\t<span style=\"font-size:14px;\"> \u6211\u4eec\u5728\u63a5\u6536\u9519\u8bef\u7c7b\u578b\u7684\u540e\u9762\u5b9a\u4e49\u4e00\u4e2a\u53d8\u91cf<\/span><span style=\"font-size:14px;\">msg<\/span><span style=\"font-size:14px;\">\u7528\u4e8e\u63a5\u6536\u5177\u4f53\u9519\u8bef\u4fe1\u606f<\/span><span style=\"font-size:14px;\">,&nbsp;<\/span><span style=\"font-size:14px;\">\u7136\u540e\u5c06<\/span><span style=\"font-size:14px;\">msg<\/span><span style=\"font-size:14px;\">\u63a5\u6536\u7684\u9519\u8bef\u4fe1\u606f\u6253\u5370\u3002\u518d\u6765\u8fd0\u884c\u7a0b\u5e8f\uff1a<\/span>\n<\/p>\n<div class=\"cnblogs_code\" style=\"border:1px solid #CCCCCC;padding:5px;overflow:auto;margin:5px 0px;white-space:normal;\">\n<pre style=\"margin-top:0px;margin-bottom:0px;white-space:pre-wrap;word-wrap:break-word;max-width:600px;\">name 'aa' is not defined<\/pre>\n<\/div>\n<p class=\"p0\" style=\"line-height:25.2px;margin:10px auto;white-space:normal;\">\n\t<span style=\"font-size:14px;\">\u73b0\u5728\u53ea\u6253\u5370\u4e86\u4e00\u884c\u5177\u4f53\u9519\u8bef\u4fe1\u606f\u3002<\/span>\n<\/p>\n<p class=\"p0\" style=\"line-height:25.2px;margin:10px auto;white-space:normal;\">\n\t<span style=\"font-size:14px;\"><strong>\u5f02\u5e38\u7684\u629b\u51fa\u673a\u5236\uff1a<\/strong><\/span>\n<\/p>\n<p class=\"p0\" style=\"line-height:25.2px;margin:10px auto;white-space:normal;\">\n\t<span style=\"font-size:14px;\">1\u3001\u5982\u679c\u5728\u8fd0\u884c\u65f6\u53d1\u751f\u5f02\u5e38\uff0c\u89e3\u91ca\u5668\u4f1a\u67e5\u627e\u76f8\u5e94\u7684\u5904\u7406\u8bed\u53e5\uff08\u79f0\u4e3a<\/span><span style=\"font-size:14px;\">handler<\/span><span style=\"font-size:14px;\">\uff09<\/span><span style=\"font-size:14px;\">.<\/span>\n<\/p>\n<p class=\"p0\" style=\"line-height:25.2px;margin:10px auto;white-space:normal;\">\n\t<span style=\"font-size:14px;\">2\u3001\u8981\u662f\u5728\u5f53\u524d\u51fd\u6570\u91cc\u6ca1\u6709\u627e\u5230\u7684\u8bdd\uff0c\u5b83\u4f1a\u5c06\u5f02\u5e38\u4f20\u9012\u7ed9\u4e0a\u5c42\u7684\u8c03\u7528\u51fd\u6570\uff0c\u770b\u770b\u90a3\u91cc\u80fd\u4e0d\u80fd\u5904\u7406\u3002<\/span>\n<\/p>\n<p class=\"p0\" style=\"line-height:25.2px;margin:10px auto;white-space:normal;\">\n\t<span style=\"font-size:14px;\">3\u3001\u5982\u679c\u5728\u6700\u5916\u5c42\uff08\u5168\u5c40<\/span><span style=\"font-size:14px;\">\u201cmain\u201d<\/span><span style=\"font-size:14px;\">\uff09\u8fd8\u662f\u6ca1\u6709\u627e\u5230\u7684\u8bdd\uff0c\u89e3\u91ca\u5668\u5c31\u4f1a\u9000\u51fa\uff0c\u540c\u65f6\u6253\u5370\u51fa<\/span><span style=\"font-size:14px;\">traceback<\/span><span style=\"font-size:14px;\">\u4ee5\u4fbf\u8ba9\u7528\u6237\u627e\u5230\u9519\u8bef\u4ea7\u751f\u7684\u539f\u56e0\u3002<\/span>\n<\/p>\n<p class=\"p0\" style=\"line-height:25.2px;margin:10px auto;white-space:normal;\">\n\t<span style=\"font-size:14px;\">\u6ce8\u610f\uff1a\u867d\u7136\u5927\u591a\u6570\u9519\u8bef\u4f1a\u5bfc\u81f4\u5f02\u5e38\uff0c\u4f46\u4e00\u4e2a\u5f02\u5e38\u4e0d\u4e00\u5b9a\u4ee3\u8868\u9519\u8bef\uff0c\u6709\u65f6\u5019\u5b83\u4eec\u53ea\u662f\u4e00\u4e2a\u8b66\u544a\uff0c\u6709\u65f6\u5019\u5b83\u4eec\u53ef\u80fd\u662f\u4e00\u4e2a\u7ec8\u6b62\u4fe1\u53f7\uff0c\u6bd4\u5982\u9000\u51fa\u5faa\u73af\u7b49\u3002<\/span>\n<\/p>\n<p class=\"p0\" style=\"line-height:25.2px;margin:10px auto;white-space:normal;\">\n\t<span style=\"font-size:14px;\"><strong>try&#8230;finally&#8230;<\/strong><\/span>\n<\/p>\n<p class=\"p0\" style=\"line-height:25.2px;margin:10px auto;white-space:normal;\">\n\t<span style=\"font-size:14px;\">try&#8230;finally&#8230;\u5b50\u53e5\u7528\u6765\u8868\u8fbe\u8fd9\u6837\u7684\u60c5\u51b5\uff1a<\/span>\n<\/p>\n<p class=\"p0\" style=\"line-height:25.2px;margin:10px auto;white-space:normal;\">\n\t<span style=\"font-size:14px;\">\u6211\u4eec\u4e0d\u7ba1\u7ebf\u6355\u6349\u5230\u7684\u662f\u4ec0\u4e48\u9519\u8bef\uff0c\u65e0\u8bba\u9519\u8bef\u662f\u4e0d\u662f\u53d1\u751f\uff0c\u8fd9\u4e9b\u4ee3\u7801<\/span><span style=\"font-size:14px;\">\u201c<\/span><span style=\"font-size:14px;\">\u5fc5\u987b<\/span><span style=\"font-size:14px;\">\u201d<\/span><span style=\"font-size:14px;\">\u8fd0\u884c\uff0c\u6bd4\u5982\u6587\u4ef6\u5173\u95ed\uff0c\u91ca\u653e\u9501\uff0c\u628a\u6570\u636e\u5e93\u8fde\u63a5\u8fd4\u8fd8\u7ed9\u8fde\u63a5\u6c60\u7b49\u3002<\/span><span style=\"font-size:14px;\">&nbsp;<\/span>\n<\/p>\n<p class=\"p0\" style=\"line-height:25.2px;margin:10px auto;white-space:normal;\">\n\t<span style=\"font-size:14px;\">\u521b\u5efa\u6587\u4ef6poem.txt<\/span>\n<\/p>\n<p class=\"p0\" style=\"line-height:25.2px;margin:10px auto;white-space:normal;\">\n\t<img decoding=\"async\" src=\"http:\/\/images.cnitblog.com\/blog\/311516\/201401\/132247343927.png\" alt=\"\" style=\"max-width:900px;\" \/>\n<\/p>\n<p class=\"p0\" style=\"line-height:25.2px;margin:10px auto;white-space:normal;\">\n\t<span style=\"font-size:14px;\">tryf.py<\/span>\n<\/p>\n<div class=\"cnblogs_code\" style=\"border:1px solid #CCCCCC;padding:5px;overflow:auto;margin:5px 0px;white-space:normal;\">\n<pre style=\"margin-top:0px;margin-bottom:0px;white-space:pre-wrap;word-wrap:break-word;max-width:600px;\">import time try:\n    f = file('poem.txt') while True: # our usual file-reading idiom line = f.readline() if len(line) == 0: break time.sleep(2) print line, finally:\n    f.close() print 'Cleaning up...closed the file'<\/pre>\n<\/div>\n<p class=\"p0\" style=\"line-height:25.2px;margin:10px auto;white-space:normal;\">\n\t<span style=\"font-size:14px;\">\u8fd0\u884c\u7a0b\u5e8f<\/span><span style=\"font-size:14px;\">(<\/span><span style=\"font-size:14px;\">\u5728<\/span><span style=\"font-size:14px;\">windows<\/span><span style=\"font-size:14px;\">\u547d\u4ee4\u63d0\u793a\u7b26\u6216<\/span><span style=\"font-size:14px;\">linux<\/span><span style=\"font-size:14px;\">\u7ec8\u7aef\u4e0b\u8fd0\u884c<\/span><span style=\"font-size:14px;\">)<\/span><span style=\"font-size:14px;\">\uff1a<\/span>\n<\/p>\n<div class=\"cnblogs_code\" style=\"border:1px solid #CCCCCC;padding:5px;overflow:auto;margin:5px 0px;white-space:normal;\">\n<pre style=\"margin-top:0px;margin-bottom:0px;white-space:pre-wrap;word-wrap:break-word;max-width:600px;\">...$ python tryf.py \nabc\nefg ^CCleaning up...closed the file\nTraceback (most recent call last):\n  File \"tryy.py\", line 18, in &lt;module&gt; time.sleep(2)\nKeyboardInterrupt<\/pre>\n<\/div>\n<p class=\"p0\" style=\"line-height:25.2px;margin:10px auto;white-space:normal;\">\n\t<span style=\"font-size:14px;\"> \u7a0b\u5e8f\u8bfbpoem.txt\u6587\u4ef6\u4e2d\u6bcf\u4e00\u884c\u6570\u636e\u6253\u5370\uff0c\u4f46\u662f\u6211\u6709\u610f\u5728\u6bcf\u6253\u5370\u4e00\u884c\u4e4b\u524d\u7528<\/span><span style=\"font-size:14px;\">time.sleep<\/span><span style=\"font-size:14px;\">\u65b9\u6cd5\u6682\u505c<\/span><span style=\"font-size:14px;\">2<\/span><span style=\"font-size:14px;\">\u79d2\u949f\u3002\u8fd9\u6837\u505a\u7684\u539f\u56e0\u662f\u8ba9\u7a0b\u5e8f\u8fd0\u884c\u5f97\u6162\u4e00\u4e9b\u3002\u5728\u7a0b\u5e8f\u8fd0\u884c\u7684\u65f6\u5019\uff0c\u6309<\/span><span style=\"font-size:14px;\">Ctrl-c<\/span><span style=\"font-size:14px;\">\u4e2d\u65ad<\/span><span style=\"font-size:14px;\">\/<\/span><span style=\"font-size:14px;\">\u53d6\u6d88\u7a0b\u5e8f\u3002<\/span>\n<\/p>\n<p class=\"p0\" style=\"line-height:25.2px;margin:10px auto;white-space:normal;\">\n\t<span style=\"font-size:14px;\"> \u6211\u4eec\u53ef\u4ee5\u89c2\u5bdf\u5230<\/span><span style=\"font-size:14px;\">KeyboardInterrupt<\/span><span style=\"font-size:14px;\">\u5f02\u5e38\u88ab\u89e6\u53d1\uff0c\u7a0b\u5e8f\u9000\u51fa\u3002\u4f46\u662f\u5728\u7a0b\u5e8f\u9000\u51fa\u4e4b\u524d\uff0c<\/span><span style=\"font-size:14px;\">finally<\/span><span style=\"font-size:14px;\">\u4ece\u53e5\u4ecd\u7136\u88ab\u6267\u884c\uff0c\u628a\u6587\u4ef6\u5173\u95ed<\/span><span style=\"font-size:14px;\">\u3002<\/span>\n<\/p>\n<p class=\"p0\" style=\"line-height:25.2px;margin:10px auto;white-space:normal;\">\n\t<span style=\"font-size:14px;\">\u5230\u76ee\u524d\u4e3a\u6b62\uff0c\u6211\u4eec\u53ea\u8ba8\u8bba\u4e86\u5982\u4f55\u6355\u6349\u5f02\u5e38\uff0c\u90a3\u4e48\u5982\u4f55\u629b\u51fa\u5f02\u5e38\u5462\uff1f<\/span>\n<\/p>\n<p class=\"p0\" style=\"line-height:25.2px;margin:10px auto;white-space:normal;\">\n\t<span style=\"font-size:14px;\"><strong>Raise<\/strong><\/span><span style=\"font-size:14px;\"><strong>\u629b\u51fa\u5f02\u5e38<\/strong><\/span>\n<\/p>\n<p class=\"p0\" style=\"line-height:25.2px;margin:10px auto;white-space:normal;\">\n\t<span style=\"font-size:14px;\">\u4f7f\u7528<\/span><span style=\"font-size:14px;\">raise<\/span><span style=\"font-size:14px;\">\u6765\u629b\u51fa\u4e00\u4e2a\u5f02\u5e38\uff1a<\/span>\n<\/p>\n<p class=\"p0\" style=\"line-height:25.2px;margin:10px auto;white-space:normal;\">\n\t<span style=\"font-size:14px;\">tryr.py<\/span>\n<\/p>\n<div class=\"cnblogs_code\" style=\"border:1px solid #CCCCCC;padding:5px;overflow:auto;margin:5px 0px;white-space:normal;\">\n<pre style=\"margin-top:0px;margin-bottom:0px;white-space:pre-wrap;word-wrap:break-word;max-width:600px;\">#coding=utf-8 filename = raw_input('please input file name:') if filename=='hello': raise NameError('input file name error !')<\/pre>\n<\/div>\n<p class=\"p0\" style=\"line-height:25.2px;margin:10px auto;white-space:normal;\">\n\t<span style=\"font-size:14px;\">\u7a0b\u5e8f\u8981\u6c42\u7528\u6237\u8f93\u5165\u4e00\u4e2a\u6587\u4ef6\u540d\uff0c\u5982\u679c\u7528\u6237\u8f93\u5165\u7684\u6587\u4ef6\u540d\u662f<\/span><span style=\"font-size:14px;\">hello&nbsp;<\/span><span style=\"font-size:14px;\">\uff0c\u90a3\u4e48\u629b\u51fa\u4e00\u4e2a<\/span><span style=\"font-size:14px;\">NameError<\/span><span style=\"font-size:14px;\">\u7684\u5f02\u5e38\uff0c\u7528\u6237\u8f93\u5165<\/span><span style=\"font-size:14px;\">hello&nbsp;\u548cNameError\u5f02\u5e38\u4e4b\u95f4\u6ca1\u6709\u4efb\u4f55\u5fc5\u7136\u8054\u7cfb\uff0c\u6211\u53ea\u662f\u4eba\u4e3a\u7684\u901a\u8fc7<\/span><span style=\"font-size:14px;\">raise<\/span><span style=\"font-size:14px;\">\u6765\u8fd9\u6837\u5b9a\u4e49\uff0c\u6211\u5f53\u7136\u4e5f\u53ef\u4ee5\u5b9a\u4e49\u79f0<\/span><span style=\"font-size:14px;\">TypeError&nbsp;<\/span><span style=\"font-size:14px;\">\uff0c\u4f46\u6211\u5b9a\u4e49\u7684\u5f02\u5e38\u7c7b\u578b\u5fc5\u987b\u662f<\/span><span style=\"font-size:14px;\">python<\/span><span style=\"font-size:14px;\">\u63d0\u4f9b\u7684\u3002<\/span>\n<\/p>\n<p class=\"p0\" style=\"line-height:25.2px;margin:10px auto;white-space:normal;\">\n\t<span style=\"font-size:14px;\"><strong>\u9644\u5f55\uff1a<\/strong><\/span>\n<\/p>\n<p class=\"p0\" style=\"line-height:25.2px;margin:10px auto;white-space:normal;\">\n\t<span style=\"font-size:14px;\">\u5e38\u89c1\u7684python\u5f02\u5e38\u7c7b\u578b<\/span>\n<\/p>\n<p class=\"p0\" style=\"line-height:25.2px;margin:10px auto;white-space:normal;\">\n\t<img decoding=\"async\" src=\"http:\/\/images.cnitblog.com\/blog\/311516\/201401\/132257132203.png\" alt=\"\" style=\"max-width:900px;\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p class=\"p0\" style=\"line-height:25.2px;margin:10px auto;white-space:normal;\">\n\t<span style=\"font-size:14px;\"><strong>python\u5f02\u5e38<\/strong><\/span>\n<\/p>\n<p class=\"p0\" style=\"line-height:25.2px;margin:10px auto;white-space:normal;\">\n\t<span style=\"font-size:14px;\">python\u7528\u5f02\u5e38\u5bf9\u8c61\uff08exception&nbsp;object\uff09\u6765\u8868\u793a\u5f02\u5e38\u60c5\u51b5\u3002\u9047\u5230\u9519\u8bef\u540e\uff0c\u4f1a\u5f15\u53d1\u5f02\u5e38\u3002\u5982\u679c\u5f02\u5e38\u5bf9\u8c61\u5e76\u672a\u88ab\u5904\u7406\u6216\u6355\u6349\uff0c\u7a0b\u5e8f\u5c31\u4f1a\u7528\u6240\u8c13\u7684&nbsp;\u56de\u6eaf\uff08Traceback\uff0c&nbsp;\u4e00\u79cd\u9519\u8bef\u4fe1\u606f\uff09\u7ec8\u6b62\u6267\u884c\uff1a<\/span>\n<\/p>\n<div class=\"cnblogs_code\" style=\"border:1px solid #CCCCCC;padding:5px;overflow:auto;margin:5px 0px;white-space:normal;\">\n<pre style=\"margin-top:0px;margin-bottom:0px;white-space:pre-wrap;word-wrap:break-word;max-width:600px;\">&gt;&gt;&gt; 1\/0\n\nTraceback (most recent call last):\n  File \"&lt;pyshell#0...<\/pre>\n<\/div>\n","protected":false},"author":1,"featured_media":557,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[334],"tags":[],"class_list":["post-194","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v16.9 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>[\u8f6c]Python\u5f02\u5e38\u5904\u7406 - Wayne&#039;s Blog<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"http:\/\/weizn.net\/?p=194\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"[\u8f6c]Python\u5f02\u5e38\u5904\u7406 - Wayne&#039;s Blog\" \/>\n<meta property=\"og:description\" content=\"python\u5f02\u5e38   python\u7528\u5f02\u5e38\u5bf9\u8c61\uff08exception&nbsp;object\uff09\u6765\u8868\u793a\u5f02\u5e38\u60c5\u51b5\u3002\u9047\u5230\u9519\u8bef\u540e\uff0c\u4f1a\u5f15\u53d1\u5f02\u5e38\u3002\u5982\u679c\u5f02\u5e38\u5bf9\u8c61\u5e76\u672a\u88ab\u5904\u7406\u6216\u6355\u6349\uff0c\u7a0b\u5e8f\u5c31\u4f1a\u7528\u6240\u8c13\u7684&nbsp;\u56de\u6eaf\uff08Traceback\uff0c&nbsp;\u4e00\u79cd\u9519\u8bef\u4fe1\u606f\uff09\u7ec8\u6b62\u6267\u884c\uff1a  &gt;&gt;&gt; 1\/0 Traceback (most recent call last):  File &quot;&lt;pyshell#0...\" \/>\n<meta property=\"og:url\" content=\"http:\/\/weizn.net\/?p=194\" \/>\n<meta property=\"og:site_name\" content=\"Wayne&#039;s Blog\" \/>\n<meta property=\"article:published_time\" content=\"2016-07-31T08:06:07+00:00\" \/>\n<meta property=\"og:image\" content=\"http:\/\/images.cnitblog.com\/blog\/311516\/201401\/132247343927.png\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"zinan\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebSite\",\"@id\":\"http:\/\/weizn.net\/#website\",\"url\":\"http:\/\/weizn.net\/\",\"name\":\"Wayne&#039;s Blog\",\"description\":\"\",\"publisher\":{\"@id\":\"http:\/\/weizn.net\/#\/schema\/person\/e88bc12c590502d8b6249326f960b264\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"http:\/\/weizn.net\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"ImageObject\",\"@id\":\"http:\/\/weizn.net\/?p=194#primaryimage\",\"inLanguage\":\"zh-Hans\",\"url\":\"http:\/\/weizn.net\/wp-content\/uploads\/2019\/01\/timg-1.jpg\",\"contentUrl\":\"http:\/\/weizn.net\/wp-content\/uploads\/2019\/01\/timg-1.jpg\",\"width\":1004,\"height\":582},{\"@type\":\"WebPage\",\"@id\":\"http:\/\/weizn.net\/?p=194#webpage\",\"url\":\"http:\/\/weizn.net\/?p=194\",\"name\":\"[\\u8f6c]Python\\u5f02\\u5e38\\u5904\\u7406 - Wayne&#039;s Blog\",\"isPartOf\":{\"@id\":\"http:\/\/weizn.net\/#website\"},\"primaryImageOfPage\":{\"@id\":\"http:\/\/weizn.net\/?p=194#primaryimage\"},\"datePublished\":\"2016-07-31T08:06:07+00:00\",\"dateModified\":\"2016-07-31T08:06:07+00:00\",\"breadcrumb\":{\"@id\":\"http:\/\/weizn.net\/?p=194#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"http:\/\/weizn.net\/?p=194\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"http:\/\/weizn.net\/?p=194#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\\u9996\\u9875\",\"item\":\"http:\/\/weizn.net\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"[\\u8f6c]Python\\u5f02\\u5e38\\u5904\\u7406\"}]},{\"@type\":\"Article\",\"@id\":\"http:\/\/weizn.net\/?p=194#article\",\"isPartOf\":{\"@id\":\"http:\/\/weizn.net\/?p=194#webpage\"},\"author\":{\"@id\":\"http:\/\/weizn.net\/#\/schema\/person\/e88bc12c590502d8b6249326f960b264\"},\"headline\":\"[\\u8f6c]Python\\u5f02\\u5e38\\u5904\\u7406\",\"datePublished\":\"2016-07-31T08:06:07+00:00\",\"dateModified\":\"2016-07-31T08:06:07+00:00\",\"mainEntityOfPage\":{\"@id\":\"http:\/\/weizn.net\/?p=194#webpage\"},\"wordCount\":735,\"commentCount\":0,\"publisher\":{\"@id\":\"http:\/\/weizn.net\/#\/schema\/person\/e88bc12c590502d8b6249326f960b264\"},\"image\":{\"@id\":\"http:\/\/weizn.net\/?p=194#primaryimage\"},\"thumbnailUrl\":\"http:\/\/weizn.net\/wp-content\/uploads\/2019\/01\/timg-1.jpg\",\"articleSection\":[\"Python\"],\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"http:\/\/weizn.net\/?p=194#respond\"]}]},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"http:\/\/weizn.net\/#\/schema\/person\/e88bc12c590502d8b6249326f960b264\",\"name\":\"zinan\",\"logo\":{\"@id\":\"http:\/\/weizn.net\/#personlogo\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"[\u8f6c]Python\u5f02\u5e38\u5904\u7406 - Wayne&#039;s Blog","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"http:\/\/weizn.net\/?p=194","og_locale":"zh_CN","og_type":"article","og_title":"[\u8f6c]Python\u5f02\u5e38\u5904\u7406 - Wayne&#039;s Blog","og_description":"python\u5f02\u5e38   python\u7528\u5f02\u5e38\u5bf9\u8c61\uff08exception&nbsp;object\uff09\u6765\u8868\u793a\u5f02\u5e38\u60c5\u51b5\u3002\u9047\u5230\u9519\u8bef\u540e\uff0c\u4f1a\u5f15\u53d1\u5f02\u5e38\u3002\u5982\u679c\u5f02\u5e38\u5bf9\u8c61\u5e76\u672a\u88ab\u5904\u7406\u6216\u6355\u6349\uff0c\u7a0b\u5e8f\u5c31\u4f1a\u7528\u6240\u8c13\u7684&nbsp;\u56de\u6eaf\uff08Traceback\uff0c&nbsp;\u4e00\u79cd\u9519\u8bef\u4fe1\u606f\uff09\u7ec8\u6b62\u6267\u884c\uff1a  &gt;&gt;&gt; 1\/0 Traceback (most recent call last):  File \"&lt;pyshell#0...","og_url":"http:\/\/weizn.net\/?p=194","og_site_name":"Wayne&#039;s Blog","article_published_time":"2016-07-31T08:06:07+00:00","og_image":[{"url":"http:\/\/images.cnitblog.com\/blog\/311516\/201401\/132247343927.png"}],"twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"zinan","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"4 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebSite","@id":"http:\/\/weizn.net\/#website","url":"http:\/\/weizn.net\/","name":"Wayne&#039;s Blog","description":"","publisher":{"@id":"http:\/\/weizn.net\/#\/schema\/person\/e88bc12c590502d8b6249326f960b264"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"http:\/\/weizn.net\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"ImageObject","@id":"http:\/\/weizn.net\/?p=194#primaryimage","inLanguage":"zh-Hans","url":"http:\/\/weizn.net\/wp-content\/uploads\/2019\/01\/timg-1.jpg","contentUrl":"http:\/\/weizn.net\/wp-content\/uploads\/2019\/01\/timg-1.jpg","width":1004,"height":582},{"@type":"WebPage","@id":"http:\/\/weizn.net\/?p=194#webpage","url":"http:\/\/weizn.net\/?p=194","name":"[\u8f6c]Python\u5f02\u5e38\u5904\u7406 - Wayne&#039;s Blog","isPartOf":{"@id":"http:\/\/weizn.net\/#website"},"primaryImageOfPage":{"@id":"http:\/\/weizn.net\/?p=194#primaryimage"},"datePublished":"2016-07-31T08:06:07+00:00","dateModified":"2016-07-31T08:06:07+00:00","breadcrumb":{"@id":"http:\/\/weizn.net\/?p=194#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["http:\/\/weizn.net\/?p=194"]}]},{"@type":"BreadcrumbList","@id":"http:\/\/weizn.net\/?p=194#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"http:\/\/weizn.net\/"},{"@type":"ListItem","position":2,"name":"[\u8f6c]Python\u5f02\u5e38\u5904\u7406"}]},{"@type":"Article","@id":"http:\/\/weizn.net\/?p=194#article","isPartOf":{"@id":"http:\/\/weizn.net\/?p=194#webpage"},"author":{"@id":"http:\/\/weizn.net\/#\/schema\/person\/e88bc12c590502d8b6249326f960b264"},"headline":"[\u8f6c]Python\u5f02\u5e38\u5904\u7406","datePublished":"2016-07-31T08:06:07+00:00","dateModified":"2016-07-31T08:06:07+00:00","mainEntityOfPage":{"@id":"http:\/\/weizn.net\/?p=194#webpage"},"wordCount":735,"commentCount":0,"publisher":{"@id":"http:\/\/weizn.net\/#\/schema\/person\/e88bc12c590502d8b6249326f960b264"},"image":{"@id":"http:\/\/weizn.net\/?p=194#primaryimage"},"thumbnailUrl":"http:\/\/weizn.net\/wp-content\/uploads\/2019\/01\/timg-1.jpg","articleSection":["Python"],"inLanguage":"zh-Hans","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["http:\/\/weizn.net\/?p=194#respond"]}]},{"@type":["Person","Organization"],"@id":"http:\/\/weizn.net\/#\/schema\/person\/e88bc12c590502d8b6249326f960b264","name":"zinan","logo":{"@id":"http:\/\/weizn.net\/#personlogo"}}]}},"_links":{"self":[{"href":"http:\/\/weizn.net\/index.php?rest_route=\/wp\/v2\/posts\/194","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/weizn.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/weizn.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/weizn.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/weizn.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=194"}],"version-history":[{"count":0,"href":"http:\/\/weizn.net\/index.php?rest_route=\/wp\/v2\/posts\/194\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/weizn.net\/index.php?rest_route=\/wp\/v2\/media\/557"}],"wp:attachment":[{"href":"http:\/\/weizn.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=194"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/weizn.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=194"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/weizn.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=194"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}