{"id":167449,"date":"2020-01-05T13:36:45","date_gmt":"2020-01-05T05:36:45","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=167449"},"modified":"2019-12-24T13:37:32","modified_gmt":"2019-12-24T05:37:32","slug":"python3-errors-and","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/python3-errors-and.html","title":{"rendered":"Python3 \u9519\u8bef\u548c\u5f02\u5e38\u4ecb\u7ecd"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\nPython \u6709\u4e24\u79cd\u9519\u8bef\u5f88\u5bb9\u6613\u8fa8\u8ba4\uff1a\u8bed\u6cd5\u9519\u8bef\u548c\u5f02\u5e38\u3002
\nPython assert\uff08\u65ad\u8a00\uff09\u7528\u4e8e\u5224\u65ad\u4e00\u4e2a\u8868\u8fbe\u5f0f\uff0c\u5728\u8868\u8fbe\u5f0f\u6761\u4ef6\u4e3a false \u7684\u65f6\u5019\u89e6\u53d1\u5f02\u5e38\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\"\"<\/p>\n

\u8bed\u6cd5\u9519\u8bef<\/strong><\/div>\n

Python \u7684\u8bed\u6cd5\u9519\u8bef\u6216\u8005\u79f0\u4e4b\u4e3a\u89e3\u6790\u9519\uff0c\u662f\u521d\u5b66\u8005\u7ecf\u5e38\u78b0\u5230\u7684\uff0c\u5982\u4e0b\u5b9e\u4f8b<\/p>\n

\r\n
>>><\/span>while<\/span> <\/span>True<\/span> <\/span>print<\/span>(<\/span>'<\/span>Hello world<\/span>'<\/span>)<\/span>\r\n <\/span>File<\/span> <\/span>\"<\/span><stdin><\/span>\"<\/span>, <\/span>line<\/span> <\/span>1<\/span>, <\/span>in<\/span> ?\r\n <\/span>while<\/span> <\/span>True<\/span> <\/span>print<\/span>(<\/span>'<\/span>Hello world<\/span>'<\/span>)<\/span>\r\n ^\r\n<\/span>SyntaxError<\/span>: <\/span>invalid<\/span> <\/span>syntax<\/span><\/div><\/pre>\n

\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u51fd\u6570 print() \u88ab\u68c0\u67e5\u5230\u6709\u9519\u8bef\uff0c\u662f\u5b83\u524d\u9762\u7f3a\u5c11\u4e86\u4e00\u4e2a\u5192\u53f7 : \u3002<\/p>\n

\u8bed\u6cd5\u5206\u6790\u5668\u6307\u51fa\u4e86\u51fa\u9519\u7684\u4e00\u884c\uff0c\u5e76\u4e14\u5728\u6700\u5148\u627e\u5230\u7684\u9519\u8bef\u7684\u4f4d\u7f6e\u6807\u8bb0\u4e86\u4e00\u4e2a\u5c0f\u5c0f\u7684\u7bad\u5934\u3002<\/p>\n

\u5f02\u5e38<\/strong><\/div>\n

\u5373\u4fbf Python \u7a0b\u5e8f\u7684\u8bed\u6cd5\u662f\u6b63\u786e\u7684\uff0c\u5728\u8fd0\u884c\u5b83\u7684\u65f6\u5019\uff0c\u4e5f\u6709\u53ef\u80fd\u53d1\u751f\u9519\u8bef\u3002\u8fd0\u884c\u671f\u68c0\u6d4b\u5230\u7684\u9519\u8bef\u88ab\u79f0\u4e3a\u5f02\u5e38\u3002<\/p>\n

\u5927\u591a\u6570\u7684\u5f02\u5e38\u90fd\u4e0d\u4f1a\u88ab\u7a0b\u5e8f\u5904\u7406\uff0c\u90fd\u4ee5\u9519\u8bef\u4fe1\u606f\u7684\u5f62\u5f0f\u5c55\u73b0\u5728\u8fd9\u91cc:<\/p>\n

\r\n
>>><\/span>10<\/span> * <\/span>(<\/span>1<\/span>\/<\/span>0<\/span>)<\/span> <\/span># 0 \u4e0d\u80fd\u4f5c\u4e3a\u9664\u6570\uff0c\u89e6\u53d1\u5f02\u5e38<\/span>\r\n<\/span>Traceback<\/span> <\/span>(<\/span>most<\/span> <\/span>recent<\/span> <\/span>call<\/span> <\/span>last<\/span>)<\/span>:\r\n <\/span>File<\/span> <\/span>\"<\/span><stdin><\/span>\"<\/span>, <\/span>line<\/span> <\/span>1<\/span>, <\/span>in<\/span> ?\r\n<\/span>ZeroDivisionError<\/span>: <\/span>division<\/span> <\/span>by<\/span> <\/span>zero<\/span>\r\n>>> <\/span>4<\/span> + <\/span>spam<\/span>*<\/span>3<\/span> <\/span># spam \u672a\u5b9a\u4e49\uff0c\u89e6\u53d1\u5f02\u5e38<\/span>\r\n<\/span>Traceback<\/span> <\/span>(<\/span>most<\/span> <\/span>recent<\/span> <\/span>call<\/span> <\/span>last<\/span>)<\/span>:\r\n <\/span>File<\/span> <\/span>\"<\/span><stdin><\/span>\"<\/span>, <\/span>line<\/span> <\/span>1<\/span>, <\/span>in<\/span> ?\r\n<\/span>NameError<\/span>: <\/span>name<\/span> <\/span>'<\/span>spam<\/span>'<\/span> <\/span>is<\/span> <\/span>not<\/span> <\/span>defined<\/span>\r\n>>> <\/span>'<\/span>2<\/span>'<\/span> + <\/span>2<\/span> <\/span># int \u4e0d\u80fd\u4e0e str \u76f8\u52a0\uff0c\u89e6\u53d1\u5f02\u5e38<\/span>\r\n<\/span>Traceback<\/span> <\/span>(<\/span>most<\/span> <\/span>recent<\/span> <\/span>call<\/span> <\/span>last<\/span>)<\/span>:\r\n <\/span>File<\/span> <\/span>\"<\/span><stdin><\/span>\"<\/span>, <\/span>line<\/span> <\/span>1<\/span>, <\/span>in<\/span> ?\r\n<\/span>TypeError<\/span>: <\/span>Can<\/span>'<\/span>t convert <\/span>'<\/span>int<\/span>'<\/span> object to str implicitly<\/span><\/div><\/pre>\n

\u5f02\u5e38\u4ee5\u4e0d\u540c\u7684\u7c7b\u578b\u51fa\u73b0\uff0c\u8fd9\u4e9b\u7c7b\u578b\u90fd\u4f5c\u4e3a\u4fe1\u606f\u7684\u4e00\u90e8\u5206\u6253\u5370\u51fa\u6765: \u4f8b\u5b50\u4e2d\u7684\u7c7b\u578b\u6709 ZeroDivisionError\uff0cNameError \u548c TypeError\u3002<\/p>\n

\u9519\u8bef\u4fe1\u606f\u7684\u524d\u9762\u90e8\u5206\u663e\u793a\u4e86\u5f02\u5e38\u53d1\u751f\u7684\u4e0a\u4e0b\u6587\uff0c\u5e76\u4ee5\u8c03\u7528\u6808\u7684\u5f62\u5f0f\u663e\u793a\u5177\u4f53\u4fe1\u606f\u3002<\/p>\n

\u5f02\u5e38\u5904\u7406<\/strong><\/div>\n
try\/except<\/strong><\/span><\/div>\n

\u5f02\u5e38\u6355\u6349\u53ef\u4ee5\u4f7f\u7528 try\/except \u8bed\u53e5\u3002
\n\"\"
\n\u4ee5\u4e0b\u4f8b\u5b50\u4e2d\uff0c\u8ba9\u7528\u6237\u8f93\u5165\u4e00\u4e2a\u5408\u6cd5\u7684\u6574\u6570\uff0c\u4f46\u662f\u5141\u8bb8\u7528\u6237\u4e2d\u65ad\u8fd9\u4e2a\u7a0b\u5e8f\uff08\u4f7f\u7528 Control-C \u6216\u8005\u64cd\u4f5c\u7cfb\u7edf\u63d0\u4f9b\u7684\u65b9\u6cd5\uff09\u3002\u7528\u6237\u4e2d\u65ad\u7684\u4fe1\u606f\u4f1a\u5f15\u53d1\u4e00\u4e2a KeyboardInterrupt \u5f02\u5e38\u3002<\/p>\n

\r\n
while<\/span> <\/span>True<\/span>:\r\n <\/span>try<\/span>:\r\n <\/span>x<\/span> = <\/span>int<\/span>(<\/span>input<\/span>(<\/span>\"<\/span>\u8bf7\u8f93\u5165\u4e00\u4e2a\u6570\u5b57: <\/span>\"<\/span>)<\/span>)<\/span>\r\n <\/span>break<\/span>\r\n <\/span>except<\/span> <\/span>ValueError<\/span>:\r\n <\/span>print<\/span>(<\/span>\"<\/span>\u60a8\u8f93\u5165\u7684\u4e0d\u662f\u6570\u5b57\uff0c\u8bf7\u518d\u6b21\u5c1d\u8bd5\u8f93\u5165\uff01<\/span>\"<\/span>)<\/span><\/div><\/pre>\n

try \u8bed\u53e5\u6309\u7167\u5982\u4e0b\u65b9\u5f0f\u5de5\u4f5c\uff1b<\/p>\n

\u9996\u5148\uff0c\u6267\u884c try \u5b50\u53e5\uff08\u5728\u5173\u952e\u5b57 try \u548c\u5173\u952e\u5b57 except \u4e4b\u95f4\u7684\u8bed\u53e5\uff09\u3002<\/p>\n

\u5982\u679c\u6ca1\u6709\u5f02\u5e38\u53d1\u751f\uff0c\u5ffd\u7565 except \u5b50\u53e5\uff0ctry \u5b50\u53e5\u6267\u884c\u540e\u7ed3\u675f\u3002<\/p>\n

\u5982\u679c\u5728\u6267\u884c try \u5b50\u53e5\u7684\u8fc7\u7a0b\u4e2d\u53d1\u751f\u4e86\u5f02\u5e38\uff0c\u90a3\u4e48 try \u5b50\u53e5\u4f59\u4e0b\u7684\u90e8\u5206\u5c06\u88ab\u5ffd\u7565\u3002\u5982\u679c\u5f02\u5e38\u7684\u7c7b\u578b\u548c except \u4e4b\u540e\u7684\u540d\u79f0\u76f8\u7b26\uff0c\u90a3\u4e48\u5bf9\u5e94\u7684 except \u5b50\u53e5\u5c06\u88ab\u6267\u884c\u3002<\/p>\n

\u5982\u679c\u4e00\u4e2a\u5f02\u5e38\u6ca1\u6709\u4e0e\u4efb\u4f55\u7684 excep \u5339\u914d\uff0c\u90a3\u4e48\u8fd9\u4e2a\u5f02\u5e38\u5c06\u4f1a\u4f20\u9012\u7ed9\u4e0a\u5c42\u7684 try \u4e2d\u3002<\/p>\n

\u4e00\u4e2a try \u8bed\u53e5\u53ef\u80fd\u5305\u542b\u591a\u4e2aexcept\u5b50\u53e5\uff0c\u5206\u522b\u6765\u5904\u7406\u4e0d\u540c\u7684\u7279\u5b9a\u7684\u5f02\u5e38\u3002\u6700\u591a\u53ea\u6709\u4e00\u4e2a\u5206\u652f\u4f1a\u88ab\u6267\u884c\u3002<\/p>\n

\u5904\u7406\u7a0b\u5e8f\u5c06\u53ea\u9488\u5bf9\u5bf9\u5e94\u7684 try \u5b50\u53e5\u4e2d\u7684\u5f02\u5e38\u8fdb\u884c\u5904\u7406\uff0c\u800c\u4e0d\u662f\u5176\u4ed6\u7684 try \u7684\u5904\u7406\u7a0b\u5e8f\u4e2d\u7684\u5f02\u5e38\u3002<\/p>\n

\u4e00\u4e2aexcept\u5b50\u53e5\u53ef\u4ee5\u540c\u65f6\u5904\u7406\u591a\u4e2a\u5f02\u5e38\uff0c\u8fd9\u4e9b\u5f02\u5e38\u5c06\u88ab\u653e\u5728\u4e00\u4e2a\u62ec\u53f7\u91cc\u6210\u4e3a\u4e00\u4e2a\u5143\u7ec4\uff0c\u4f8b\u5982:<\/p>\n

except (RuntimeError, TypeError, NameError):\r\npass<\/pre>\n

\u6700\u540e\u4e00\u4e2aexcept\u5b50\u53e5\u53ef\u4ee5\u5ffd\u7565\u5f02\u5e38\u7684\u540d\u79f0\uff0c\u5b83\u5c06\u88ab\u5f53\u4f5c\u901a\u914d\u7b26\u4f7f\u7528\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528\u8fd9\u79cd\u65b9\u6cd5\u6253\u5370\u4e00\u4e2a\u9519\u8bef\u4fe1\u606f\uff0c\u7136\u540e\u518d\u6b21\u628a\u5f02\u5e38\u629b\u51fa\u3002<\/p>\n

import<\/span> <\/span>sys<\/span> \r\n<\/span>try<\/span>:\r\n <\/span>f<\/span> = <\/span>open<\/span>(<\/span>'<\/span>myfile.txt<\/span>'<\/span>)<\/span>\r\n <\/span>s<\/span> = <\/span>f<\/span>.<\/span>readline<\/span>(<\/span>)<\/span>\r\n <\/span>i<\/span> = <\/span>int<\/span>(<\/span>s<\/span>.<\/span>strip<\/span>(<\/span>)<\/span>)<\/span>\r\n<\/span>except<\/span> <\/span>OSError<\/span> <\/span>as<\/span> <\/span>err<\/span>:\r\n <\/span>print<\/span>(<\/span>\"<\/span>OS error: {0}<\/span>\"<\/span>.<\/span>format<\/span>(<\/span>err<\/span>)<\/span>)<\/span>\r\n<\/span>except<\/span> <\/span>ValueError<\/span>:\r\n <\/span>print<\/span>(<\/span>\"<\/span>Could not convert data to an integer.<\/span>\"<\/span>)<\/span>\r\n<\/span>except<\/span>:\r\n <\/span>print<\/span>(<\/span>\"<\/span>Unexpected error:<\/span>\"<\/span>, <\/span>sys<\/span>.<\/span>exc_info<\/span>(<\/span>)<\/span>[<\/span>0<\/span>]<\/span>)<\/span>\r\n <\/span>raise<\/span><\/div><\/pre>\n
try\/except...else<\/strong><\/span><\/div>\n

try\/except \u8bed\u53e5\u8fd8\u6709\u4e00\u4e2a\u53ef\u9009\u7684 else \u5b50\u53e5\uff0c\u5982\u679c\u4f7f\u7528\u8fd9\u4e2a\u5b50\u53e5\uff0c\u90a3\u4e48\u5fc5\u987b\u653e\u5728\u6240\u6709\u7684 except \u5b50\u53e5\u4e4b\u540e\u3002<\/p>\n

else \u5b50\u53e5\u5c06\u5728 try \u5b50\u53e5\u6ca1\u6709\u53d1\u751f\u4efb\u4f55\u5f02\u5e38\u7684\u65f6\u5019\u6267\u884c\u3002
\n\"\"<\/p>\n

\u4ee5\u4e0b\u5b9e\u4f8b\u5728 try \u8bed\u53e5\u4e2d\u5224\u65ad\u6587\u4ef6\u662f\u5426\u53ef\u4ee5\u6253\u5f00\uff0c\u5982\u679c\u6253\u5f00\u6587\u4ef6\u65f6\u6b63\u5e38\u7684\u6ca1\u6709\u53d1\u751f\u5f02\u5e38\u5219\u6267\u884c else \u90e8\u5206\u7684\u8bed\u53e5\uff0c\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\uff1a<\/p>\n

\r\n
for<\/span> <\/span>arg<\/span> <\/span>in<\/span> <\/span>sys<\/span>.<\/span>argv<\/span>[<\/span>1<\/span>:<\/span>]<\/span>:\r\n <\/span>try<\/span>:\r\n <\/span>f<\/span> = <\/span>open<\/span>(<\/span>arg<\/span>, <\/span>'<\/span>r<\/span>'<\/span>)<\/span>\r\n <\/span>except<\/span> <\/span>IOError<\/span>:\r\n <\/span>print<\/span>(<\/span>'<\/span>cannot open<\/span>'<\/span>, <\/span>arg<\/span>)<\/span>\r\n <\/span>else<\/span>:\r\n <\/span>print<\/span>(<\/span>arg<\/span>, <\/span>'<\/span>has<\/span>'<\/span>, <\/span>len<\/span>(<\/span>f<\/span>.<\/span>readlines<\/span>(<\/span>)<\/span>)<\/span>, <\/span>'<\/span>lines<\/span>'<\/span>)<\/span>\r\n <\/span>f<\/span>.<\/span>close<\/span>(<\/span>)<\/span><\/div><\/pre>\n

\u4f7f\u7528 else \u5b50\u53e5\u6bd4\u628a\u6240\u6709\u7684\u8bed\u53e5\u90fd\u653e\u5728 try \u5b50\u53e5\u91cc\u9762\u8981\u597d\uff0c\u8fd9\u6837\u53ef\u4ee5\u907f\u514d\u4e00\u4e9b\u610f\u60f3\u4e0d\u5230\uff0c\u800c except \u53c8\u65e0\u6cd5\u6355\u83b7\u7684\u5f02\u5e38\u3002<\/p>\n

\u5f02\u5e38\u5904\u7406\u5e76\u4e0d\u4ec5\u4ec5\u5904\u7406\u90a3\u4e9b\u76f4\u63a5\u53d1\u751f\u5728 try \u5b50\u53e5\u4e2d\u7684\u5f02\u5e38\uff0c\u800c\u4e14\u8fd8\u80fd\u5904\u7406\u5b50\u53e5\u4e2d\u8c03\u7528\u7684\u51fd\u6570\uff08\u751a\u81f3\u95f4\u63a5\u8c03\u7528\u7684\u51fd\u6570\uff09\u91cc\u629b\u51fa\u7684\u5f02\u5e38\u3002\u4f8b\u5982:<\/p>\n

\r\n
>>><\/span>def<\/span> <\/span>this_fails<\/span>(<\/span>)<\/span>:\r\n <\/span>x<\/span> = <\/span>1<\/span>\/<\/span>0<\/span>\r\n \r\n>>> <\/span>try<\/span>:\r\n <\/span>this_fails<\/span>(<\/span>)<\/span>\r\n <\/span>except<\/span> <\/span>ZeroDivisionError<\/span> <\/span>as<\/span> <\/span>err<\/span>:\r\n <\/span>print<\/span>(<\/span>'<\/span>Handling run-time error:<\/span>'<\/span>, <\/span>err<\/span>)<\/span>\r\n \r\n<\/span>Handling<\/span> <\/span>run<\/span>-<\/span>time<\/span> <\/span>error<\/span>: <\/span>int<\/span> <\/span>division<\/span> <\/span>or<\/span> <\/span>modulo<\/span> <\/span>by<\/span> <\/span>zero<\/span><\/div><\/pre>\n
try-finally \u8bed\u53e5<\/strong><\/span><\/div>\n

try-finally \u8bed\u53e5\u65e0\u8bba\u662f\u5426\u53d1\u751f\u5f02\u5e38\u90fd\u5c06\u6267\u884c\u6700\u540e\u7684\u4ee3\u7801\u3002
\n\"\"
\n\u4ee5\u4e0b\u5b9e\u4f8b\u4e2d finally \u8bed\u53e5\u65e0\u8bba\u5f02\u5e38\u662f\u5426\u53d1\u751f\u90fd\u4f1a\u6267\u884c\uff1a
\n\u5b9e\u4f8b<\/strong><\/p>\n

\r\ntry<\/span>:\r\n    runoob(<\/span>)<\/span>\r\nexcept<\/span> AssertionError<\/span> as<\/span> error:\r\n    print<\/span>(<\/span>error)<\/span>\r\nelse<\/span>:\r\n    try<\/span>:\r\n        with<\/span> open<\/span>(<\/span>'file.log'<\/span>)<\/span> as<\/span> file<\/span>:\r\n            read_data =<\/span> file<\/span>.read<\/span>(<\/span>)<\/span>\r\n    except<\/span> FileNotFoundError as<\/span> fnf_error:\r\n        print<\/span>(<\/span>fnf_error)<\/span>\r\nfinally<\/span>:\r\n    print<\/span>(<\/span>'\u8fd9\u53e5\u8bdd\uff0c\u65e0\u8bba\u5f02\u5e38\u662f\u5426\u53d1\u751f\u90fd\u4f1a\u6267\u884c\u3002'<\/span>)<\/span>\r\n<\/pre>\n
\u629b\u51fa\u5f02\u5e38<\/strong><\/div>\n

Python \u4f7f\u7528 raise \u8bed\u53e5\u629b\u51fa\u4e00\u4e2a\u6307\u5b9a\u7684\u5f02\u5e38\u3002
\nraise\u8bed\u6cd5\u683c\u5f0f\u5982\u4e0b\uff1a<\/p>\n

raise<\/span> <\/span>[<\/span>Exception<\/span> <\/span>[,<\/span> args <\/span>[,<\/span> traceback<\/span>]]]<\/span><\/pre>\n

\"\"
\n\u4ee5\u4e0b\u5b9e\u4f8b\u5982\u679c x \u5927\u4e8e 5 \u5c31\u89e6\u53d1\u5f02\u5e38:<\/p>\n

\r\n
x<\/span> = <\/span>10<\/span>\r\n<\/span>if<\/span> <\/span>x<\/span> > <\/span>5<\/span>:\r\n <\/span>raise<\/span> <\/span>Exception<\/span>(<\/span>'<\/span>x \u4e0d\u80fd\u5927\u4e8e 5\u3002x \u7684\u503c\u4e3a: {}<\/span>'<\/span>.<\/span>format<\/span>(<\/span>x<\/span>)<\/span>)<\/span><\/div><\/pre>\n

\u6267\u884c\u4ee5\u4e0a\u4ee3\u7801\u4f1a\u89e6\u53d1\u5f02\u5e38\uff1a<\/p>\n

\r\n
x<\/span> = <\/span>10<\/span>\r\n<\/span>if<\/span> <\/span>x<\/span> > <\/span>5<\/span>:\r\n <\/span>raise<\/span> <\/span>Exception<\/span>(<\/span>'<\/span>x \u4e0d\u80fd\u5927\u4e8e 5\u3002x \u7684\u503c\u4e3a: {}<\/span>'<\/span>.<\/span>format<\/span>(<\/span>x<\/span>)<\/span>)<\/span>\r\n<\/div><\/pre>\n

raise \u552f\u4e00\u7684\u4e00\u4e2a\u53c2\u6570\u6307\u5b9a\u4e86\u8981\u88ab\u629b\u51fa\u7684\u5f02\u5e38\u3002\u5b83\u5fc5\u987b\u662f\u4e00\u4e2a\u5f02\u5e38\u7684\u5b9e\u4f8b\u6216\u8005\u662f\u5f02\u5e38\u7684\u7c7b\uff08\u4e5f\u5c31\u662f Exception \u7684\u5b50\u7c7b\uff09\u3002<\/p>\n

\u5982\u679c\u4f60\u53ea\u60f3\u77e5\u9053\u8fd9\u662f\u5426\u629b\u51fa\u4e86\u4e00\u4e2a\u5f02\u5e38\uff0c\u5e76\u4e0d\u60f3\u53bb\u5904\u7406\u5b83\uff0c\u90a3\u4e48\u4e00\u4e2a\u7b80\u5355\u7684 raise \u8bed\u53e5\u5c31\u53ef\u4ee5\u518d\u6b21\u628a\u5b83\u629b\u51fa\u3002<\/p>\n

\r\n
>>><\/span>try<\/span>:\r\n <\/span>raise<\/span> <\/span>NameError<\/span>(<\/span>'<\/span>HiThere<\/span>'<\/span>)<\/span>\r\n <\/span>except<\/span> <\/span>NameError<\/span>:\r\n <\/span>print<\/span>(<\/span>'<\/span>An exception flew by!<\/span>'<\/span>)<\/span>\r\n <\/span>raise<\/span>\r\n \r\n<\/span>An<\/span> <\/span>exception<\/span> <\/span>flew<\/span> <\/span>by<\/span>!\r\n<\/span>Traceback<\/span> <\/span>(<\/span>most<\/span> <\/span>recent<\/span> <\/span>call<\/span> <\/span>last<\/span>)<\/span>:\r\n <\/span>File<\/span> <\/span>\"<\/span><stdin><\/span>\"<\/span>, <\/span>line<\/span> <\/span>2<\/span>, <\/span>in<\/span> ?\r\n<\/span>NameError<\/span>: <\/span>HiThere<\/span><\/div><\/pre>\n
\u7528\u6237\u81ea\u5b9a\u4e49\u5f02\u5e38<\/strong><\/div>\n

\u4f60\u53ef\u4ee5\u901a\u8fc7\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5f02\u5e38\u7c7b\u6765\u62e5\u6709\u81ea\u5df1\u7684\u5f02\u5e38\u3002\u5f02\u5e38\u7c7b\u7ee7\u627f\u81ea Exception \u7c7b\uff0c\u53ef\u4ee5\u76f4\u63a5\u7ee7\u627f\uff0c\u6216\u8005\u95f4\u63a5\u7ee7\u627f\uff0c\u4f8b\u5982:<\/p>\n

\r\n
>>><\/span>class<\/span> <\/span>MyError<\/span>(<\/span>Exception<\/span>)<\/span>:\r\n <\/span>def<\/span> <\/span>__init__<\/span>(<\/span>self<\/span>, <\/span>value<\/span>)<\/span>:\r\n <\/span>self<\/span>.<\/span>value<\/span> = <\/span>value<\/span>\r\n <\/span>def<\/span> <\/span>__str__<\/span>(<\/span>self<\/span>)<\/span>:\r\n <\/span>return<\/span> <\/span>repr<\/span>(<\/span>self<\/span>.<\/span>value<\/span>)<\/span>\r\n \r\n>>> <\/span>try<\/span>:\r\n <\/span>raise<\/span> <\/span>MyError<\/span>(<\/span>2<\/span>*<\/span>2<\/span>)<\/span>\r\n <\/span>except<\/span> <\/span>MyError<\/span> <\/span>as<\/span> <\/span>e<\/span>:\r\n <\/span>print<\/span>(<\/span>'<\/span>My exception occurred, value:<\/span>'<\/span>, <\/span>e<\/span>.<\/span>value<\/span>)<\/span>\r\n \r\n<\/span>My<\/span> <\/span>exception<\/span> <\/span>occurred<\/span>, <\/span>value<\/span>: <\/span>4<\/span>\r\n>>> <\/span>raise<\/span> <\/span>MyError<\/span>(<\/span>'<\/span>oops!<\/span>'<\/span>)<\/span>\r\n<\/span>Traceback<\/span> <\/span>(<\/span>most<\/span> <\/span>recent<\/span> <\/span>call<\/span> <\/span>last<\/span>)<\/span>:\r\n <\/span>File<\/span> <\/span>\"<\/span><stdin><\/span>\"<\/span>, <\/span>line<\/span> <\/span>1<\/span>, <\/span>in<\/span> ?\r\n<\/span>__main__<\/span>.<\/span>MyError<\/span>: <\/span>'<\/span>oops!<\/span>'<\/span><\/div><\/pre>\n

\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u7c7b Exception \u9ed8\u8ba4\u7684 __init__() \u88ab\u8986\u76d6\u3002<\/p>\n

\u5f53\u521b\u5efa\u4e00\u4e2a\u6a21\u5757\u6709\u53ef\u80fd\u629b\u51fa\u591a\u79cd\u4e0d\u540c\u7684\u5f02\u5e38\u65f6\uff0c\u4e00\u79cd\u901a\u5e38\u7684\u505a\u6cd5\u662f\u4e3a\u8fd9\u4e2a\u5305\u5efa\u7acb\u4e00\u4e2a\u57fa\u7840\u5f02\u5e38\u7c7b\uff0c\u7136\u540e\u57fa\u4e8e\u8fd9\u4e2a\u57fa\u7840\u7c7b\u4e3a\u4e0d\u540c\u7684\u9519\u8bef\u60c5\u51b5\u521b\u5efa\u4e0d\u540c\u7684\u5b50\u7c7b:<\/p>\n

\r\n
class<\/span> <\/span>Error<\/span>(<\/span>Exception<\/span>)<\/span>:\r\n <\/span>\"\"\"<\/span>Base class for exceptions in this module.<\/span>\"\"\"<\/span>\r\n <\/span>pass<\/span>\r\n \r\n<\/span>class<\/span> <\/span>InputError<\/span>(<\/span>Error<\/span>)<\/span>:\r\n <\/span>\"\"\"<\/span>Exception raised for errors in the input.\r\n \r\n Attributes:\r\n expression -- input expression in which the error occurred\r\n message -- explanation of the error\r\n <\/span>\"\"\"<\/span>\r\n \r\n <\/span>def<\/span> <\/span>__init__<\/span>(<\/span>self<\/span>, <\/span>expression<\/span>, <\/span>message<\/span>)<\/span>:\r\n <\/span>self<\/span>.<\/span>expression<\/span> = <\/span>expression<\/span>\r\n <\/span>self<\/span>.<\/span>message<\/span> = <\/span>message<\/span>\r\n \r\n<\/span>class<\/span> <\/span>TransitionError<\/span>(<\/span>Error<\/span>)<\/span>:\r\n <\/span>\"\"\"<\/span>Raised when an operation attempts a state transition that's not\r\n allowed.\r\n \r\n Attributes:\r\n previous -- state at beginning of transition\r\n next -- attempted new state\r\n message -- explanation of why the specific transition is not allowed\r\n <\/span>\"\"\"<\/span>\r\n \r\n <\/span>def<\/span> <\/span>__init__<\/span>(<\/span>self<\/span>, <\/span>previous<\/span>, <\/span>next<\/span>, <\/span>message<\/span>)<\/span>:\r\n <\/span>self<\/span>.<\/span>previous<\/span> = <\/span>previous<\/span>\r\n <\/span>self<\/span>.<\/span>next<\/span> = <\/span>next<\/span>\r\n <\/span>self<\/span>.<\/span>message<\/span> = <\/span>message<\/span><\/div><\/pre>\n

\u5927\u591a\u6570\u7684\u5f02\u5e38\u7684\u540d\u5b57\u90fd\u4ee5\"Error\"\u7ed3\u5c3e\uff0c\u5c31\u8ddf\u6807\u51c6\u7684\u5f02\u5e38\u547d\u540d\u4e00\u6837\u3002<\/p>\n

\u5b9a\u4e49\u6e05\u7406\u884c\u4e3a<\/strong><\/div>\n

try \u8bed\u53e5\u8fd8\u6709\u53e6\u5916\u4e00\u4e2a\u53ef\u9009\u7684\u5b50\u53e5\uff0c\u5b83\u5b9a\u4e49\u4e86\u65e0\u8bba\u5728\u4efb\u4f55\u60c5\u51b5\u4e0b\u90fd\u4f1a\u6267\u884c\u7684\u6e05\u7406\u884c\u4e3a\u3002 \u4f8b\u5982:<\/p>\n

\r\n
>>><\/span>try<\/span>:\r\n... <\/span>raise<\/span> <\/span>KeyboardInterrupt<\/span>\r\n... <\/span>finally<\/span>:\r\n... <\/span>print<\/span>(<\/span>'<\/span>Goodbye, world!<\/span>'<\/span>)<\/span>\r\n... \r\n<\/span>Goodbye<\/span>, <\/span>world<\/span>!\r\n<\/span>Traceback<\/span> <\/span>(<\/span>most<\/span> <\/span>recent<\/span> <\/span>call<\/span> <\/span>last<\/span>)<\/span>:\r\n <\/span>File<\/span> <\/span>\"<\/span><stdin><\/span>\"<\/span>, <\/span>line<\/span> <\/span>2<\/span>, <\/span>in<\/span> <<\/span>module<\/span>>\r\n<\/span>KeyboardInterrupt<\/span><\/div><\/pre>\n

\u4ee5\u4e0a\u4f8b\u5b50\u4e0d\u7ba1 try \u5b50\u53e5\u91cc\u9762\u6709\u6ca1\u6709\u53d1\u751f\u5f02\u5e38\uff0cfinally \u5b50\u53e5\u90fd\u4f1a\u6267\u884c\u3002<\/p>\n

\u5982\u679c\u4e00\u4e2a\u5f02\u5e38\u5728 try \u5b50\u53e5\u91cc\uff08\u6216\u8005\u5728 except \u548c else \u5b50\u53e5\u91cc\uff09\u88ab\u629b\u51fa\uff0c\u800c\u53c8\u6ca1\u6709\u4efb\u4f55\u7684 except \u628a\u5b83\u622a\u4f4f\uff0c\u90a3\u4e48\u8fd9\u4e2a\u5f02\u5e38\u4f1a\u5728 finally \u5b50\u53e5\u6267\u884c\u540e\u88ab\u629b\u51fa\u3002<\/p>\n

\u4e0b\u9762\u662f\u4e00\u4e2a\u66f4\u52a0\u590d\u6742\u7684\u4f8b\u5b50\uff08\u5728\u540c\u4e00\u4e2a try \u8bed\u53e5\u91cc\u5305\u542b except \u548c finally \u5b50\u53e5\uff09:<\/p>\n

\r\n
>>><\/span>def<\/span> <\/span>divide<\/span>(<\/span>x<\/span>, <\/span>y<\/span>)<\/span>:\r\n <\/span>try<\/span>:\r\n <\/span>result<\/span> = <\/span>x<\/span> \/ <\/span>y<\/span>\r\n <\/span>except<\/span> <\/span>ZeroDivisionError<\/span>:\r\n <\/span>print<\/span>(<\/span>\"<\/span>division by zero!<\/span>\"<\/span>)<\/span>\r\n <\/span>else<\/span>:\r\n <\/span>print<\/span>(<\/span>\"<\/span>result is<\/span>\"<\/span>, <\/span>result<\/span>)<\/span>\r\n <\/span>finally<\/span>:\r\n <\/span>print<\/span>(<\/span>\"<\/span>executing finally clause<\/span>\"<\/span>)<\/span>\r\n \r\n>>> <\/span>divide<\/span>(<\/span>2<\/span>, <\/span>1<\/span>)<\/span>\r\n<\/span>result<\/span> <\/span>is<\/span> <\/span>2.0<\/span>\r\n<\/span>executing<\/span> <\/span>finally<\/span> <\/span>clause<\/span>\r\n>>> <\/span>divide<\/span>(<\/span>2<\/span>, <\/span>0<\/span>)<\/span>\r\n<\/span>division<\/span> <\/span>by<\/span> <\/span>zero<\/span>!\r\n<\/span>executing<\/span> <\/span>finally<\/span> <\/span>clause<\/span>\r\n>>> <\/span>divide<\/span>(<\/span>\"<\/span>2<\/span>\"<\/span>, <\/span>\"<\/span>1<\/span>\"<\/span>)<\/span>\r\n<\/span>executing<\/span> <\/span>finally<\/span> <\/span>clause<\/span>\r\n<\/span>Traceback<\/span> <\/span>(<\/span>most<\/span> <\/span>recent<\/span> <\/span>call<\/span> <\/span>last<\/span>)<\/span>:\r\n <\/span>File<\/span> <\/span>\"<\/span><stdin><\/span>\"<\/span>, <\/span>line<\/span> <\/span>1<\/span>, <\/span>in<\/span> ?\r\n <\/span>File<\/span> <\/span>\"<\/span><stdin><\/span>\"<\/span>, <\/span>line<\/span> <\/span>3<\/span>, <\/span>in<\/span> <\/span>divide<\/span>\r\n<\/span>TypeError<\/span>: <\/span>unsupported<\/span> <\/span>operand<\/span> <\/span>type<\/span>(<\/span>s<\/span>)<\/span> <\/span>for<\/span> \/: <\/span>'<\/span>str<\/span>'<\/span> <\/span>and<\/span> <\/span>'<\/span>str<\/span>'<\/span><\/div><\/pre>\n
\u9884\u5b9a\u4e49\u7684\u6e05\u7406\u884c\u4e3a<\/strong><\/div>\n

\u4e00\u4e9b\u5bf9\u8c61\u5b9a\u4e49\u4e86\u6807\u51c6\u7684\u6e05\u7406\u884c\u4e3a\uff0c\u65e0\u8bba\u7cfb\u7edf\u662f\u5426\u6210\u529f\u7684\u4f7f\u7528\u4e86\u5b83\uff0c\u4e00\u65e6\u4e0d\u9700\u8981\u5b83\u4e86\uff0c\u90a3\u4e48\u8fd9\u4e2a\u6807\u51c6\u7684\u6e05\u7406\u884c\u4e3a\u5c31\u4f1a\u6267\u884c\u3002
\n\u8fd9\u9762\u8fd9\u4e2a\u4f8b\u5b50\u5c55\u793a\u4e86\u5c1d\u8bd5\u6253\u5f00\u4e00\u4e2a\u6587\u4ef6\uff0c\u7136\u540e\u628a\u5185\u5bb9\u6253\u5370\u5230\u5c4f\u5e55\u4e0a:<\/p>\n

\r\n
for<\/span> <\/span>line<\/span> <\/span>in<\/span> <\/span>open<\/span>(<\/span>\"<\/span>myfile.txt<\/span>\"<\/span>)<\/span>:\r\n <\/span>print<\/span>(<\/span>line<\/span>, <\/span>end<\/span>=<\/span>\"<\/span>\"<\/span>)<\/span><\/div><\/pre>\n

\u4ee5\u4e0a\u8fd9\u6bb5\u4ee3\u7801\u7684\u95ee\u9898\u662f\uff0c\u5f53\u6267\u884c\u5b8c\u6bd5\u540e\uff0c\u6587\u4ef6\u4f1a\u4fdd\u6301\u6253\u5f00\u72b6\u6001\uff0c\u5e76\u6ca1\u6709\u88ab\u5173\u95ed\u3002<\/p>\n

\u5173\u952e\u8bcd with \u8bed\u53e5\u5c31\u53ef\u4ee5\u4fdd\u8bc1\u8bf8\u5982\u6587\u4ef6\u4e4b\u7c7b\u7684\u5bf9\u8c61\u5728\u4f7f\u7528\u5b8c\u4e4b\u540e\u4e00\u5b9a\u4f1a\u6b63\u786e\u7684\u6267\u884c\u4ed6\u7684\u6e05\u7406\u65b9\u6cd5:<\/p>\n

\r\n
with<\/span> <\/span>open<\/span>(<\/span>\"<\/span>myfile.txt<\/span>\"<\/span>)<\/span> <\/span>as<\/span> <\/span>f<\/span>:\r\n <\/span>for<\/span> <\/span>line<\/span> <\/span>in<\/span> <\/span>f<\/span>:\r\n <\/span>print<\/span>(<\/span>line<\/span>, <\/span>end<\/span>=<\/span>\"<\/span>\"<\/span>)<\/span><\/div><\/pre>\n

\u4ee5\u4e0a\u8fd9\u6bb5\u4ee3\u7801\u6267\u884c\u5b8c\u6bd5\u540e\uff0c\u5c31\u7b97\u5728\u5904\u7406\u8fc7\u7a0b\u4e2d\u51fa\u95ee\u9898\u4e86\uff0c\u6587\u4ef6 f \u603b\u662f\u4f1a\u5173\u95ed\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"

Python \u7684\u8bed\u6cd5\u9519\u8bef\u6216\u8005\u79f0\u4e4b\u4e3a\u89e3\u6790\u9519\uff0c\u662f\u521d\u5b66\u8005\u7ecf\u5e38\u78b0\u5230\u7684\uff0c\u5982\u4e0b\u5b9e\u4f8b >>>while T […]<\/p>\n","protected":false},"author":321,"featured_media":167452,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-167449","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/167449","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/321"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=167449"}],"version-history":[{"count":5,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/167449\/revisions"}],"predecessor-version":[{"id":167911,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/167449\/revisions\/167911"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/167452"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=167449"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=167449"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=167449"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}