{"id":224808,"date":"2021-09-01T09:44:51","date_gmt":"2021-09-01T01:44:51","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=224808"},"modified":"2021-08-23T08:47:21","modified_gmt":"2021-08-23T00:47:21","slug":"jsp-exception-handling","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/jsp-exception-handling.html","title":{"rendered":"JSP \u5f02\u5e38\u5904\u7406\u5982\u4f55\u5904\u7406\uff1f"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u5f53\u7f16\u5199JSP\u7a0b\u5e8f\u7684\u65f6\u5019\uff0c\u7a0b\u5e8f\u5458\u53ef\u80fd\u4f1a\u9057\u6f0f\u4e00\u4e9bBUG\uff0c\u8fd9\u4e9bBUG\u53ef\u80fd\u4f1a\u51fa\u73b0\u5728\u7a0b\u5e8f\u7684\u4efb\u4f55\u5730\u65b9\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\"\"<\/p>\n

JSP\u4ee3\u7801\u4e2d\u901a\u5e38\u6709\u4ee5\u4e0b\u51e0\u7c7b\u5f02\u5e38:<\/p>\n

\u68c0\u67e5\u578b\u5f02\u5e38:\u68c0\u67e5\u578b\u5f02\u5e38\u5c31\u662f\u4e00\u4e2a\u5178\u578b\u7684\u7528\u6237\u9519\u8bef\u6216\u8005\u4e00\u4e2a\u7a0b\u5e8f\u5458\u65e0\u6cd5\u9884\u89c1\u7684\u9519\u8bef\u3002\u4e3e\u4f8b\u6765\u8bf4\uff0c\u5982\u679c\u4e00\u4e2a\u6587\u4ef6\u5c06\u8981\u88ab\u6253\u5f00\uff0c\u4f46\u662f\u65e0\u6cd5\u627e\u5230\u8fd9\u4e2a\u6587\u4ef6\uff0c\u5219\u4e00\u4e2a\u5f02\u5e38\u88ab\u629b\u51fa\u3002\u8fd9\u4e9b\u5f02\u5e38\u4e0d\u80fd\u5728\u7f16\u8bd1\u671f\u88ab\u7b80\u5355\u5730\u5ffd\u7565\u3002
\n\u8fd0\u884c\u65f6\u5f02\u5e38:\u4e00\u4e2a\u8fd0\u884c\u65f6\u5f02\u5e38\u53ef\u80fd\u5df2\u7ecf\u88ab\u7a0b\u5e8f\u5458\u907f\u514d\uff0c\u8fd9\u79cd\u5f02\u5e38\u5728\u7f16\u8bd1\u671f\u5c06\u4f1a\u88ab\u5ffd\u7565\u3002
\n\u9519\u8bef:\u9519\u8bef\u4e0d\u662f\u5f02\u5e38\uff0c\u4f46\u95ee\u9898\u662f\u5b83\u8d85\u51fa\u4e86\u7528\u6237\u6216\u8005\u7a0b\u5e8f\u5458\u7684\u63a7\u5236\u8303\u56f4\u3002\u9519\u8bef\u901a\u5e38\u4f1a\u5728\u4ee3\u7801\u4e2d\u88ab\u5ffd\u7565\uff0c\u60a8\u51e0\u4e4e\u4e0d\u80fd\u62ff\u5b83\u600e\u4e48\u6837\u3002\u4e3e\u4f8b\u6765\u8bf4\uff0c\u6808\u6ea2\u51fa\u9519\u8bef\u3002\u8fd9\u4e9b\u9519\u8bef\u90fd\u4f1a\u5728\u7f16\u8bd1\u671f\u88ab\u5ffd\u7565\u3002
\n\u672c\u8282\u5c06\u4f1a\u7ed9\u51fa\u51e0\u4e2a\u7b80\u5355\u800c\u4f18\u96c5\u7684\u65b9\u5f0f\u6765\u5904\u7406\u8fd0\u884c\u65f6\u5f02\u5e38\u548c\u9519\u8bef\u3002<\/p>\n

\u4f7f\u7528Exception\u5bf9\u8c61<\/strong><\/div>\n

exception\u5bf9\u8c61\u662fThrowable\u5b50\u7c7b\u7684\u4e00\u4e2a\u5b9e\u4f8b\uff0c\u53ea\u5728\u9519\u8bef\u9875\u9762\u4e2d\u53ef\u7528\u3002\u4e0b\u8868\u5217\u51fa\u4e86Throwable\u7c7b\u4e2d\u4e00\u4e9b\u91cd\u8981\u7684\u65b9\u6cd5:<\/p>\n\n\n\n\n\n\n\n\n\n
\n\u5e8f\u53f7<\/strong><\/th>\n\n\u65b9\u6cd5<\/strong>&<\/strong>\u63cf\u8ff0<\/strong><\/th>\n<\/tr>\n
\n1<\/td>\n\npublic String getMessage()<\/strong><\/p>\n

\u8fd4\u56de\u5f02\u5e38\u7684\u4fe1\u606f\u3002\u8fd9\u4e2a\u4fe1\u606f\u5728Throwable\u6784\u9020\u51fd\u6570\u4e2d\u88ab\u521d\u59cb\u5316<\/td>\n<\/tr>\n

\n2<\/td>\n\npublic ThrowablegetCause()<\/strong><\/p>\n

\u8fd4\u56de\u5f15\u8d77\u5f02\u5e38\u7684\u539f\u56e0\uff0c\u7c7b\u578b\u4e3aThrowable\u5bf9\u8c61<\/td>\n<\/tr>\n

\n3<\/td>\n\npublic String toString()<\/strong><\/p>\n

\u8fd4\u56de\u7c7b\u540d<\/td>\n<\/tr>\n

\n4<\/td>\n\npublic void printStackTrace()<\/strong><\/p>\n

\u5c06\u5f02\u5e38\u6808\u8f68\u8ff9\u8f93\u51fa\u81f3System.err<\/td>\n<\/tr>\n

\n5<\/td>\n\npublic StackTraceElement [] getStackTrace()<\/strong><\/p>\n

\u4ee5\u6808\u8f68\u8ff9\u5143\u7d20\u6570\u7ec4\u7684\u5f62\u5f0f\u8fd4\u56de\u5f02\u5e38\u6808\u8f68\u8ff9<\/td>\n<\/tr>\n

\n6<\/td>\n\npublic ThrowablefillInStackTrace()<\/strong><\/p>\n

\u4f7f\u7528\u5f53\u524d\u6808\u8f68\u8ff9\u586b\u5145Throwable\u5bf9\u8c61<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

JSP\u63d0\u4f9b\u4e86\u53ef\u9009\u9879\u6765\u4e3a\u6bcf\u4e2aJSP\u9875\u9762\u6307\u5b9a\u9519\u8bef\u9875\u9762\u3002\u65e0\u8bba\u4f55\u65f6\u9875\u9762\u629b\u51fa\u4e86\u5f02\u5e38\uff0cJSP\u5bb9\u5668\u90fd\u4f1a\u81ea\u52a8\u5730\u8c03\u7528\u9519\u8bef\u9875\u9762\u3002<\/p>\n

\u63a5\u4e0b\u6765\u7684\u4f8b\u5b50\u4e3amain.jsp\u6307\u5b9a\u4e86\u4e00\u4e2a\u9519\u8bef\u9875\u9762\u3002\u4f7f\u7528<%@page errorPage=\"XXXXX\"%>\u6307\u4ee4\u6307\u5b9a\u4e00\u4e2a\u9519\u8bef\u9875\u9762\u3002<\/p>\n

<%@<\/span> page errorPage<\/span>=<\/span>\"ShowError.jsp\"<\/span> %>\r\n\r\n<\/span><html><\/span>\r\n<\/span><head><\/span>\r\n   <\/span><title><\/span>Error Handling Example<\/span><\/title><\/span>\r\n<\/span><\/head><\/span>\r\n<\/span><body><\/span>\r\n<\/span><%<\/span>\r\n   <\/span>\/\/ Throw an exception to invoke the error page<\/span>\r\n   <\/span>int<\/span> x <\/span>=<\/span> <\/span>1<\/span>;<\/span>\r\n   <\/span>if<\/span> <\/span>(<\/span>x <\/span>==<\/span> <\/span>1<\/span>)<\/span>\r\n   <\/span>{<\/span>\r\n      <\/span>throw<\/span> <\/span>new<\/span> <\/span>RuntimeException<\/span>(<\/span>\"Error condition!!!\"<\/span>);<\/span>\r\n   <\/span>}<\/span>\r\n%>\r\n<\/span><\/body><\/span>\r\n<\/span><\/html><\/span><\/pre>\n

\u73b0\u5728\uff0c\u7f16\u5199ShowError.jsp\u6587\u4ef6\u5982\u4e0b:<\/p>\n

<%@<\/span> page isErrorPage<\/span>=<\/span>\"true\"<\/span> %>\r\n<\/span><html><\/span>\r\n<\/span><head><\/span>\r\n<\/span><title><\/span>Show Error Page<\/span><\/title><\/span>\r\n<\/span><\/head><\/span>\r\n<\/span><body><\/span>\r\n<\/span><h1><\/span>Opps...<\/span><\/h1><\/span>\r\n<\/span><p><\/span>Sorry, an error occurred.<\/span><\/p><\/span>\r\n<\/span><p><\/span>Here is the exception stack trace: <\/span><\/p><\/span>\r\n<\/span><pre><\/span>\r\n<\/span><%<\/span> exception<\/span>.<\/span>printStackTrace<\/span>(<\/span>response<\/span>.<\/span>getWriter<\/span>());<\/span> %><\/span><\/pre>\n

\u6ce8\u610f\u5230\uff0cShowError.jsp\u6587\u4ef6\u4f7f\u7528\u4e86<%@page isErrorPage=\"true\"%>\u6307\u4ee4\uff0c\u8fd9\u4e2a\u6307\u4ee4\u544a\u8bc9JSP\u7f16\u8bd1\u5668\u9700\u8981\u4ea7\u751f\u4e00\u4e2a\u5f02\u5e38\u5b9e\u4f8b\u53d8\u91cf\u3002<\/p>\n

\u73b0\u5728\u8bd5\u7740\u8bbf\u95eemain.jsp\u9875\u9762\uff0c\u5b83\u5c06\u4f1a\u4ea7\u751f\u5982\u4e0b\u7ed3\u679c:<\/p>\n

java.lang.RuntimeException: Error condition!!!\r\n......\r\n\r\nOpps...\r\nSorry, an error occurred.\r\n\r\nHere is the exception stack trace:<\/pre>\n
\u5728\u9519\u8bef\u9875\u9762\u4e2d\u4f7f\u7528JSTL\u6807\u7b7e<\/strong><\/div>\n

\u53ef\u4ee5\u5229\u7528JSTL\u6807\u7b7e\u6765\u7f16\u5199\u9519\u8bef\u9875\u9762ShowError.jsp\u3002\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\u7684\u4ee3\u7801\u4e0e\u4e0a\u4f8b\u4ee3\u7801\u7684\u903b\u8f91\u51e0\u4e4e\u4e00\u6837\uff0c\u4f46\u662f\u672c\u4f8b\u7684\u4ee3\u7801\u6709\u66f4\u597d\u7684\u7ed3\u6784\uff0c\u5e76\u4e14\u80fd\u591f\u63d0\u4f9b\u66f4\u591a\u4fe1\u606f:<\/p>\n

<%@<\/span> taglib prefix<\/span>=<\/span>\"c\"<\/span> uri<\/span>=<\/span>\"http:\/\/java.sun.com\/jsp\/jstl\/core\"<\/span> %>\r\n<\/span><%<\/span>@page<\/span> isErrorPage<\/span>=<\/span>\"true\"<\/span> %>\r\n<\/span><html><\/span>\r\n<\/span><head><\/span>\r\n<\/span><title><\/span>Show Error Page<\/span><\/title><\/span>\r\n<\/span><\/head><\/span>\r\n<\/span><body><\/span>\r\n<\/span><h1><\/span>Opps...<\/span><\/h1><\/span>\r\n<\/span><table<\/span> <\/span>width<\/span>=<\/span>\"100%\"<\/span> <\/span>border<\/span>=<\/span>\"1\"<\/span>><\/span>\r\n<\/span><tr<\/span> <\/span>valign<\/span>=<\/span>\"top\"<\/span>><\/span>\r\n<\/span><td<\/span> <\/span>width<\/span>=<\/span>\"40%\"<\/span>><b><\/span>Error:<\/span><\/b><\/td><\/span>\r\n<\/span><td><\/span>${pageContext.exception}<\/span><\/td><\/span>\r\n<\/span><\/tr><\/span>\r\n<\/span><tr<\/span> <\/span>valign<\/span>=<\/span>\"top\"<\/span>><\/span>\r\n<\/span><td><b><\/span>URI:<\/span><\/b><\/td><\/span>\r\n<\/span><td><\/span>${pageContext.errorData.requestURI}<\/span><\/td><\/span>\r\n<\/span><\/tr><\/span>\r\n<\/span><tr<\/span> <\/span>valign<\/span>=<\/span>\"top\"<\/span>><\/span>\r\n<\/span><td><b><\/span>Status code:<\/span><\/b><\/td><\/span>\r\n<\/span><td><\/span>${pageContext.errorData.statusCode}<\/span><\/td><\/span>\r\n<\/span><\/tr><\/span>\r\n<\/span><tr<\/span> <\/span>valign<\/span>=<\/span>\"top\"<\/span>><\/span>\r\n<\/span><td><b><\/span>Stack trace:<\/span><\/b><\/td><\/span>\r\n<\/span><td><\/span>\r\n<\/span><c:forEach<\/span> <\/span>var<\/span>=<\/span>\"trace\"<\/span> \r\n         <\/span>items<\/span>=<\/span>\"${pageContext.exception.stackTrace}\"<\/span>><\/span>\r\n<\/span><p><\/span>${trace}<\/span><\/p><\/span>\r\n<\/span><\/c:forEach><\/span>\r\n<\/span><\/td><\/span>\r\n<\/span><\/tr><\/span>\r\n<\/span><\/table><\/span>\r\n<\/span><\/body><\/span>\r\n<\/span><\/html><\/span><\/pre>\n

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

jsp-exeception-1<\/pre>\n
\u4f7f\u7528 try\u2026catch\u5757<\/strong><\/div>\n

\u5982\u679c\u60a8\u60f3\u8981\u5c06\u5f02\u5e38\u5904\u7406\u653e\u5728\u4e00\u4e2a\u9875\u9762\u4e2d\uff0c\u5e76\u4e14\u5bf9\u4e0d\u540c\u7684\u5f02\u5e38\u8fdb\u884c\u4e0d\u540c\u7684\u5904\u7406\uff0c\u90a3\u4e48\u60a8\u5c31\u9700\u8981\u4f7f\u7528try\u2026catch\u5757\u4e86\u3002<\/p>\n

\u63a5\u4e0b\u6765\u7684\u8fd9\u4e2a\u4f8b\u5b50\u663e\u793a\u4e86\u5982\u4f55\u4f7f\u7528try\u2026catch\u5757\uff0c\u5c06\u8fd9\u4e9b\u4ee3\u7801\u653e\u5728main.jsp\u4e2d:<\/p>\n

<html><\/span>\r\n<\/span><head><\/span>\r\n   <\/span><title><\/span>Try...Catch Example<\/span><\/title><\/span>\r\n<\/span><\/head><\/span>\r\n<\/span><body><\/span>\r\n<\/span><%<\/span>\r\n   <\/span>try<\/span>{<\/span>\r\n      <\/span>int<\/span> i <\/span>=<\/span> <\/span>1<\/span>;<\/span>\r\n      i <\/span>=<\/span> i <\/span>\/<\/span> <\/span>0<\/span>;<\/span>\r\n      <\/span>out<\/span>.<\/span>println<\/span>(<\/span>\"The answer is \"<\/span> <\/span>+<\/span> i<\/span>);<\/span>\r\n   <\/span>}<\/span>\r\n   <\/span>catch<\/span> <\/span>(<\/span>Exception<\/span> e<\/span>){<\/span>\r\n      <\/span>out<\/span>.<\/span>println<\/span>(<\/span>\"An exception occurred: \"<\/span> <\/span>+<\/span> e<\/span>.<\/span>getMessage<\/span>());<\/span>\r\n   <\/span>}<\/span>\r\n%>\r\n<\/span><\/body><\/span>\r\n<\/span><\/html><\/span><\/pre>\n

\u8bd5\u7740\u8bbf\u95eemain.jsp\uff0c\u5b83\u5c06\u4f1a\u4ea7\u751f\u5982\u4e0b\u7ed3\u679c:<\/p>\n

An exception occurred: \/ by zero<\/pre>\n","protected":false},"excerpt":{"rendered":"

JSP\u4ee3\u7801\u4e2d\u901a\u5e38\u6709\u4ee5\u4e0b\u51e0\u7c7b\u5f02\u5e38: \u68c0\u67e5\u578b\u5f02\u5e38:\u68c0\u67e5\u578b\u5f02\u5e38\u5c31\u662f\u4e00\u4e2a\u5178\u578b\u7684\u7528\u6237\u9519\u8bef\u6216\u8005\u4e00\u4e2a\u7a0b\u5e8f\u5458\u65e0\u6cd5\u9884\u89c1\u7684\u9519\u8bef\u3002\u4e3e […]<\/p>\n","protected":false},"author":1481,"featured_media":224810,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-224808","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\/224808","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\/1481"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=224808"}],"version-history":[{"count":6,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/224808\/revisions"}],"predecessor-version":[{"id":224818,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/224808\/revisions\/224818"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/224810"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=224808"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=224808"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=224808"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}