{"id":236792,"date":"2022-03-11T09:42:29","date_gmt":"2022-03-11T01:42:29","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=236792"},"modified":"2022-02-28T10:43:25","modified_gmt":"2022-02-28T02:43:25","slug":"go-error-spring","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/go-error-spring.html","title":{"rendered":"\u7b80\u5355\u4ecb\u7ecdGo\u8bed\u8a00\u9519\u8bef\u5904\u7406\u5f02\u5e38\u6355\u83b7+\u5f02\u5e38\u629b\u51fa"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u8fd9\u7bc7\u6587\u7ae0\u4e3b\u8981\u4ecb\u7ecd\u4e86Go\u8bed\u8a00\u9519\u8bef\u5904\u7406\u5f02\u5e38\u6355\u83b7\u548c\u5f02\u5e38\u629b\u51fa\uff0cGo\u8bed\u8a00\u7684\u4f5c\u8005\u8ba4\u4e3ajava\u7b49\u8bed\u8a00\u7684\u9519\u8bef\u5904\u7406\u5e95\u5c42\u5b9e\u73b0\u8f83\u4e3a\u590d\u6742,\u5c31\u5b9e\u73b0\u4e86\u51fd\u6570\u53ef\u4ee5\u8fd4\u56de\u9519\u8bef\u7c7b\u578b\u4ee5\u53ca\u7b80\u5355\u7684\u5f02\u5e38\u6355\u83b7,\u867d\u7136\u7b80\u5355\u4f46\u662f\u4e5f\u975e\u5e38\u7cbe\u5999,\u5927\u5927\u7684\u63d0\u9ad8\u4e86\u8fd0\u884c\u6548\u7387,\u4e0b\u6587\u9700\u8981\u7684\u670b\u53cb\u53ef\u4ee5\u53c2\u8003\u4e00\u4e0b<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

Go \u8bed\u8a00\u8ffd\u6c42\u7b80\u6d01\u4f18\u96c5\uff0c\u6240\u4ee5\uff0cGo \u8bed\u8a00\u4e0d\u652f\u6301\u4f20\u7edf\u7684 try\u2026catch\u2026finally \u8fd9\u79cd\u5904\u7406\u3002<\/p>\n

Go \u4e2d\u5f15\u5165\u7684\u9519\u8bef\u5904\u7406\u65b9\u5f0f\u4e3a\uff1adefer, panic, recover,\u4e5f\u4ec5\u4ec5\u662f\u9519\u5904\u5904\u7406\u7684\u6a21\u62df<\/p>\n

Go\u8bed\u8a00\u7684\u4f5c\u8005\u8ba4\u4e3ajava\u7b49\u8bed\u8a00\u7684\u9519\u8bef\u5904\u7406\u5e95\u5c42\u5b9e\u73b0\u8f83\u4e3a\u590d\u6742,\u5c31\u5b9e\u73b0\u4e86\u51fd\u6570\u53ef\u4ee5\u8fd4\u56de\u9519\u8bef\u7c7b\u578b\u4ee5\u53ca<\/p>\n

\u7b80\u5355\u7684\u5f02\u5e38\u6355\u83b7,\u867d\u7136\u7b80\u5355\u4f46\u662f\u4e5f\u975e\u5e38\u7cbe\u5999,\u5927\u5927\u7684\u63d0\u9ad8\u4e86\u8fd0\u884c\u6548\u7387\u3002<\/p>\n

\u4e00\u3001error\u53d8\u91cf\u53ef\u4ee5\u505a\u4ec0\u4e48<\/strong><\/div>\n
1.\u5b9a\u4e49\u4e00\u4e2aerror\u53d8\u91cf<\/strong><\/span><\/div>\n

\u2460\u76f4\u63a5new\u4e00\u4e2a<\/p>\n

err := errors.New(\"\u6211\u662f\u9519\u8bef\u5904\u7406\u8bed\u53e5\")<\/p>\n

\u2461\u5b9e\u73b0Error\u63a5\u53e3<\/p>\n

\u53ea\u8981\u5b9e\u73b0\u4e86Error\u63a5\u53e3\u5c31\u53ef\u4ee5\u88aberror\u7c7b\u578b\u6240\u63a5\u6536,\u6253\u5370\u7684\u65f6\u5019\u4f1a\u76f4\u63a5\u6253\u5370\u5b9e\u73b0\u63a5\u53e3\u65f6\uff0c\u65b9\u6cd5\u8fd4\u56de\u7684\u5b57\u7b26\u4e32\u3002<\/p>\n

\u5b98\u65b9\u63a5\u53e3\u5982\u4e0b\uff1a<\/p>\n

type error interface {\r\n    Error() string\r\n}<\/pre>\n

\u4ee3\u7801\u5982\u4e0b:<\/p>\n

\r\n\/\/\u5b9a\u4e49\u4e00\u4e2a\u7ed3\u6784\u4f53\r\ntype man struct {\r\n    age int\r\n}\r\n\/\/\u5b9e\u73b0\u63a5\u53e3\r\nfunc (a man) Error() string {\r\n    str := \"\u8fd9\u662f\u4e00\u4e2a\u9519\u8bef\u63a5\u53e3\"\r\n    return str\r\n}\r\n\/\/\u521b\u5efa\u51fd\u6570\u8fd4\u56de\u9519\u8bef\u7c7b\u578b\r\nfunc judgeAge() (int, error) {\r\n    var a man\r\n    return 0, a\r\n}\r\n\/\/\u4e3b\u51fd\u6570\u8c03\u7528\r\nfunc main() {\r\n    _, e := judgeAge()\r\n    if e!=nil{\r\n        fmt.Println(e)\r\n    }\r\n}\r\n<\/pre>\n
2.\u9519\u8bef\u7684\u5904\u7406<\/strong><\/span><\/div>\n

\u5c06\u5982\u679c\u9519\u8bef\u53d8\u91cf\u4e0d\u4e3a\u7a7a,\u5219\u5c06\u9519\u8bef\u8fdb\u884c\u6355\u83b7\u6253\u5370,\u8fd9\u91cc\u76f4\u63a5\u6253\u5370\u5230\u4e86\u63a7\u5236\u53f0,\u53ef\u4ee5\u5c06\u9519\u8bef\u6253\u5370\u5230\u65e5\u5fd7\u6587\u4ef6\u5185,\u4e3a\u4ee5\u540e\u7cfb\u7edf\u7684\u7ef4\u62a4\u505a\u4fdd\u969c\u3002<\/p>\n

\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n

\r\npackage main\r\n \r\nimport (\r\n    \"errors\"\r\n    \"fmt\"\r\n)\r\n \r\nfunc main() {\r\n    err := errors.New(\"\u6211\u662f\u9519\u8bef\u5904\u7406\u8bed\u53e5\")\r\n    if err != nil {\r\n        fmt.Println(err)\r\n    }else {\r\n        fmt.Println(\"\u6ca1\u6709\u9519\u8bef\")\r\n    }\r\n    fmt.Println(123)\r\n}<\/pre>\n
3.\u505a\u51fd\u6570\u8fd4\u56de\u503c<\/strong><\/span><\/div>\n

\u5982\u679c\u51fd\u6570\u8fd4\u56de\u7684\u9519\u8bef\u7c7b\u578b\u503c\u4e0d\u4e3a\u7a7a,\u5219\u5c06\u5176\u8fdb\u884c\u6253\u5370<\/p>\n

\u4ee3\u7801\u5982\u4e0b:<\/p>\n

\r\npackage main\r\n \r\nimport (\r\n    \"errors\"\r\n    \"fmt\"\r\n)\r\n \r\nfunc test(num1,num2 int) error {\r\n    if num2 == 0 {\r\n        return errors.New(\"\u9664\u6570\u4e3a\u96f6\")\r\n    }\r\n    res := num1 \/ num2\r\n    fmt.Println(\"\u8ba1\u7b97\u7ed3\u679c\u4e3a=\", res)\r\n    return nil\r\n \r\n}\r\nfunc main() {\r\n    err := test(11,0)\r\n    if err!=nil{\r\n        fmt.Println(err)\r\n    }\r\n}<\/pre>\n
4.\u505a\u51fd\u6570\u53c2\u6570<\/strong><\/span><\/div>\n

\u4e00\u822c\u7528\u4f5c\u5904\u7406\u9519\u8bef\u53d8\u91cf,\u5728\u9700\u8981\u8fdb\u884c\u9519\u8bef\u68c0\u9a8c\u7684\u65f6\u5019\u8c03\u7528\u51fd\u6570,\u4e0d\u7528\u518d\u8fdb\u884c\u5224\u65ad\u4e86<\/p>\n

\u4ee3\u7801\u5982\u4e0b:<\/p>\n

\r\nfunc PrintError(err error){\r\n    if err!=nil{\r\n        fmt.Println(err)\r\n    }\r\n}<\/pre>\n
\u4e8c\u3001\u6a21\u62df\u5f02\u5e38\u7684\u6355\u83b7\u4e0e\u629b\u51fa<\/strong><\/div>\n
1.defer\u7b80\u4ecb<\/strong><\/span><\/div>\n

defer\u540e\u9762\u5b9a\u4e49\u7684\u4e1c\u897f\u662f\u5728\u8c03\u7528\u51fd\u6570\u6267\u884c\u5b8c\u7684\u65f6\u5019\u6267\u884c\u7684\u4ee3\u7801\uff0c\u4e00\u822c\u7528\u4e8e\u6587\u4ef6\u63cf\u8ff0\u7b26\u3001\u6570\u636e\u5e93\u5bf9\u8c61\u7684\u5173\u95ed\uff08\u5728\u4e4b\u524d\u8bb2\u7ba1\u9053\u7684\u65f6\u5019\u6709\u4ecb\u7ecd\u8fc7\uff09<\/p>\n

defer\u4ec0\u4e48\u4ec0\u4e48\u5ef6\u65f6\u6267\u884c<\/p>\n

\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n

\r\nfunc pDefer() {\r\n    defer fmt.Println(\"hahaha\")\r\n    fmt.Println(\"hello\")\r\n}\r\nfunc main() {\r\n    pDefer()\r\n}\r\n\/*\r\n\u6253\u5370\u7ed3\u679c\r\n    hello\r\n    hahaha\r\n*\/<\/pre>\n
2.\u4f7f\u7528recover\u6a21\u62df\u5f02\u5e38\u7684\u6355\u83b7<\/strong><\/span><\/div>\n

\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n

\r\npackage main\r\n \r\nimport (\r\n    \"fmt\"\r\n)\r\n \r\nfunc test() {\r\n    \/\/\u4f7f\u7528defer + recover \u6765\u6355\u83b7\u548c\u5904\u7406\u5f02\u5e38\uff08\u8fd4\u56de\u9519\u8bef\u53d8\u91cf\uff09\r\n    \/\/\u51fd\u6570\u6267\u884c\u672b\u5c3e\u6267\u884c\u8fd9\u4e2a\u533f\u540d\u51fd\u6570\r\n     defer func() {\r\n         err := recover() \/\/ recover()\u5185\u7f6e\u51fd\u6570\uff0c\u53ef\u4ee5\u6355\u83b7\u5230\u5f02\u5e38\r\n         if err != nil {  \/\/\u8bf4\u660e\u6355\u83b7\u5230\u9519\u8bef\r\n             fmt.Println(err)\r\n         }\r\n     }()\r\n    num1 := 10\r\n    num2 := 0\r\n    res := num1 \/ num2\r\n    fmt.Println(\"\u8ba1\u7b97\u7ed3\u679c\u4e3a=\", res)\r\n \r\n}\r\nfunc main() {\r\n    test()\r\n}<\/pre>\n
3.\u4f7f\u7528panic\u4e3b\u52a8\u629b\u51fa\u9519\u8bef<\/strong><\/span><\/div>\n

\u5f02\u5e38\u7684\u6355\u83b7,\u4f7f\u7a0b\u5e8f\u8fd8\u80fd\u591f\u6b63\u5e38\u7684\u8fd0\u884c,\u5982\u679c\u6709\u81f4\u547d\u9519\u8bef\u5c31\u9700\u8981\u8fdb\u884c\u9519\u8bef\u7684\u629b\u51fa\u4e86,\u4e5f\u5c31\u662f\u5b95\u673a\uff0c\u4e0d\u5230\u8feb\u4e0d\u5f97\u5df2\u5343\u4e07\u4e0d\u8981\u4f7f\u7528\u8fd9\u4e2a\u51fd\u6570,\u6709\u53ef\u80fd\u4f1a\u9020\u6210\u5927\u91cf\u6570\u636e\u7684\u4e22\u5931<\/p>\n

\u4ee3\u7801\u5982\u4e0b:<\/p>\n

\r\npackage main\r\n \r\nimport (\r\n    \"errors\"\r\n    \"fmt\"\r\n)\r\n \r\nfunc test() error {\r\n    num1 := 10\r\n    num2 := 0\r\n    if num2 == 0 {\r\n        return errors.New(\"\u9664\u6570\u4e3a\u96f6\")\r\n    }\r\n    res := num1 \/ num2\r\n    fmt.Println(\"\u8ba1\u7b97\u7ed3\u679c\u4e3a=\", res)\r\n    return nil\r\n \r\n}\r\n \r\n \r\nfunc main() {\r\n    \/\/\u65e0\u8bbaerr\u662f\u5426\u4e3a\u7a7a,\u90fd\u8fdb\u884c\u7a0b\u5e8f\u7684\u7ec8\u6b62\r\n     panic(test())\/\/\u5185\u7f6e\u51fd\u65702,\u53c2\u6570\u4f7f\u4e00\u4e2ainterface\u63a5\u53e3\r\n}<\/pre>\n

\u8fd0\u884c\u7ed3\u679c:<\/p>\n

\"\"<\/p>\n

\u603b\u7ed3\uff1a<\/strong><\/div>\n

Go\u8bed\u8a00\u867d\u7136\u6ca1\u6709\u4e00\u4e2a\u8f83\u4e3a\u6210\u719f\u7684\u5f02\u5e38\u6355\u83b7\u673a\u5236,\u4f46\u662f\u5176\u7cbe\u7b80\u7684\u9519\u8bef\u5904\u7406\u4ecd\u65e7\u80fd\u591f\u6ee1\u8db3\u5176\u9700\u6c42,\u51cf\u5c11\u9519\u8bef\u7684\u5904\u7406\u5927\u5927\u7684\u52a0\u5feb\u4e86\u6211\u4eec\u7684\u5f00\u53d1\u6548\u7387,\u4ee3\u7801\u7684\u8fd0\u884c\u6548\u7387\u3002<\/p>\n

\u5230\u6b64\u8fd9\u7bc7\u5173\u4e8eGo\u8bed\u8a00\u9519\u8bef\u5904\u7406\u5f02\u5e38\u6355\u83b7+\u5f02\u5e38\u629b\u51fa\u7684\u6587\u7ae0\u5c31\u4ecb\u7ecd\u5230\u8fd9\u4e86<\/p>\n","protected":false},"excerpt":{"rendered":"

Go \u8bed\u8a00\u8ffd\u6c42\u7b80\u6d01\u4f18\u96c5\uff0c\u6240\u4ee5\uff0cGo \u8bed\u8a00\u4e0d\u652f\u6301\u4f20\u7edf\u7684 try\u2026catch\u2026finally \u8fd9\u79cd\u5904\u7406\u3002 Go \u4e2d […]<\/p>\n","protected":false},"author":668,"featured_media":208739,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-236792","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\/236792","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\/668"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=236792"}],"version-history":[{"count":4,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/236792\/revisions"}],"predecessor-version":[{"id":236797,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/236792\/revisions\/236797"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/208739"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=236792"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=236792"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=236792"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}