{"id":3775,"date":"2023-12-29T21:53:58","date_gmt":"2023-12-29T13:53:58","guid":{"rendered":"http:\/\/www.ponybai.com\/?p=3775"},"modified":"2023-11-28T22:01:33","modified_gmt":"2023-11-28T14:01:33","slug":"apex-%e4%b8%ad%e7%9a%84%e5%bc%82%e5%b8%b8","status":"publish","type":"post","link":"http:\/\/www.ponybai.com\/?p=3775","title":{"rendered":"Apex \u4e2d\u7684\u5f02\u5e38"},"content":{"rendered":"\n<p><dfn>\u5f02\u5e38<\/dfn>\u4f1a\u8bb0\u5f55\u4e2d\u65ad\u6b63\u5e38\u4ee3\u7801\u6d41\u7684\u9519\u8bef\u548c\u5176\u4ed6\u4e8b\u4ef6 \u6267\u884c\u3002 \u8bed\u53e5\u7528\u4e8e \u751f\u6210\u5f02\u5e38\uff0c\u800c \u3001 \u548c \u8bed\u53e5\u7528\u4e8e\u4ece\u5f02\u5e38\u4e2d\u6b63\u5e38\u6062\u590d\u3002<samp>throw<\/samp><samp>try<\/samp><samp>catch<\/samp><samp>finally<\/samp><\/p>\n\n\n\n<p>\u6709\u8bb8\u591a\u65b9\u6cd5\u53ef\u4ee5\u5904\u7406\u4ee3\u7801\u4e2d\u7684\u9519\u8bef\uff0c\u5305\u62ec\u4f7f\u7528\u65ad\u8a00\uff08\u5982\u8c03\u7528\uff09\u6216\u8fd4\u56de\u9519\u8bef \u4ee3\u7801\u6216\u5e03\u5c14\u503c\uff0c\u90a3\u4e48\u4e3a\u4ec0\u4e48\u8981\u4f7f\u7528\u5f02\u5e38\u5462\uff1f\u4f7f\u7528\u5f02\u5e38\u7684\u4f18\u70b9\u662f \u5b83\u4eec\u7b80\u5316\u4e86\u9519\u8bef\u5904\u7406\u3002\u5f02\u5e38\u4ece\u88ab\u8c03\u7528\u7684\u65b9\u6cd5\u5192\u6ce1\u5230 caller\uff0c\u6839\u636e\u9700\u8981\u8bbe\u7f6e\u591a\u4e2a\u7ea7\u522b\uff0c\u76f4\u5230\u627e\u5230\u5904\u7406\u9519\u8bef\u7684\u8bed\u53e5\u3002\u8fd9\u79cd\u5192\u6ce1\u7f13\u89e3\u4e86 \u60a8\u4e0d\u5fc5\u5728\u6bcf\u4e2a\u65b9\u6cd5\u4e2d\u7f16\u5199\u9519\u8bef\u5904\u7406\u4ee3\u7801\u3002\u6b64\u5916\uff0c\u901a\u8fc7\u4f7f\u7528\u8bed\u53e5\uff0c\u60a8\u53ef\u4ee5\u5728\u4e00\u4e2a\u4f4d\u7f6e\u8fdb\u884c\u6062\u590d \u4ece\u5f02\u5e38\uff0c\u4f8b\u5982\u91cd\u7f6e\u53d8\u91cf\u548c\u5220\u9664\u6570\u636e\u3002<samp>System.assert<\/samp><samp>catch<\/samp><samp>finally<\/samp><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u53d1\u751f\u5f02\u5e38\u65f6\u4f1a\u53d1\u751f\u4ec0\u4e48\u60c5\u51b5\uff1f<\/h2>\n\n\n\n<p><a><\/a>\u53d1\u751f\u5f02\u5e38\u65f6\uff0c\u4ee3\u7801\u6267\u884c\u5c06\u505c\u6b62\u3002\u4efb\u4f55 \u5728\u5f02\u5e38\u4e4b\u524d\u5904\u7406\u7684 DML \u64cd\u4f5c\u5c06\u56de\u6eda\uff0c\u5e76\u4e14 \u672a\u63d0\u4ea4\u5230\u6570\u636e\u5e93\u3002\u5f02\u5e38\u8bb0\u5f55\u5728\u8c03\u8bd5\u65e5\u5fd7\u4e2d\u3002\u4e3a Salesforce \u53d1\u9001\u7684\u672a\u5904\u7406\u5f02\u5e38\uff08\u4ee3\u7801\u672a\u6355\u83b7\u7684\u5f02\u5e38\uff09 \u5305\u542b\u5f02\u5e38\u4fe1\u606f\u7684\u7535\u5b50\u90ae\u4ef6\u3002\u6700\u7ec8\u7528\u6237\u770b\u5230\u9519\u8bef Salesforce \u7528\u6237\u754c\u9762\u4e2d\u7684\u6d88\u606f\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u672a\u7ecf\u5904\u7406\u7684\u5f02\u5e38\u7535\u5b50\u90ae\u4ef6<\/h2>\n\n\n\n<p><a><\/a>\u5f53\u53d1\u751f\u672a\u7ecf\u5904\u7406\u7684 Apex \u5f02\u5e38\u65f6\uff0c\u5c06\u53d1\u9001\u7535\u5b50\u90ae\u4ef6 \u5305\u62ec Apex \u5806\u6808\u8ddf\u8e2a\u3001\u5f02\u5e38\u6d88\u606f\u4ee5\u53ca\u5ba2\u6237\u7684\u7ec4\u7ec7\u548c\u7528\u6237 ID\u3002\u62a5\u8868\u4e0d\u4f1a\u8fd4\u56de\u5176\u4ed6\u6570\u636e\u3002<a><\/a>\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u672a\u5904\u7406\u7684\u5f02\u5e38\u7535\u5b50\u90ae\u4ef6\u4f1a\u53d1\u9001\u7ed9\u6307\u5b9a\u7684\u5f00\u53d1\u4eba\u5458 \u5728\u5931\u8d25\u7c7b\u7684&nbsp;LastModifiedBy&nbsp;\u5b57\u6bb5\u4e2d\uff0c\u6216\u8005 \u89e6\u53d1\u3002\u6b64\u5916\uff0c\u60a8\u8fd8\u53ef\u4ee5\u5c06\u7535\u5b50\u90ae\u4ef6\u53d1\u9001\u7ed9 Salesforce \u7684\u7528\u6237 \u7ec4\u7ec7\u548c\u4efb\u610f\u7535\u5b50\u90ae\u4ef6\u5730\u5740\u3002\u8fd9\u4e9b\u7535\u5b50\u90ae\u4ef6\u6536\u4ef6\u4eba\u8fd8\u53ef\u4ee5 \u63a5\u6536\u6d41\u7a0b\u6216\u6d41\u7a0b\u9519\u8bef\u7535\u5b50\u90ae\u4ef6\u3002\u8981\u8bbe\u7f6e\u8fd9\u4e9b\u7535\u5b50\u90ae\u4ef6\u901a\u77e5\uff0c \u4ece\u201c\u8bbe\u7f6e\u201d\u4e2d\uff0c\u8f93\u5165\u201c\u5feb\u901f\u67e5\u627e\u201d\u6846\uff0c\u7136\u540e\u9009\u62e9<strong>\u201cApex\u201d \u4f8b\u5916\u7535\u5b50\u90ae\u4ef6<\/strong>\u3002\u7136\u540e\uff0c\u8f93\u5165\u7684\u7535\u5b50\u90ae\u4ef6\u5730\u5740\u5c06\u5e94\u7528\u4e8e \u5ba2\u6237\u7ec4\u7ec7\u4e2d\u7684\u6240\u6709\u6258\u7ba1\u8f6f\u4ef6\u5305\u3002\u60a8\u8fd8\u53ef\u4ee5\u914d\u7f6e Apex \u4f7f\u7528 Tooling API \u5bf9\u8c61\u7684\u5f02\u5e38\u7535\u5b50\u90ae\u4ef6 Apex\u7535\u5b50\u90ae\u4ef6\u901a\u77e5\u3002<kbd>Apex Exception Email<\/kbd><\/p>\n\n\n\n<p>\u6ce8\u610f<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5982\u679c\u540c\u6b65\u8fd0\u884c\u7684 Apex \u4ee3\u7801\u4e2d\u51fa\u73b0\u91cd\u590d\u5f02\u5e38\uff0c\u6216\u8005 \u5f02\u6b65\u5730\uff0c\u540e\u7eed\u5f02\u5e38\u7535\u5b50\u90ae\u4ef6\u5c06\u88ab\u7981\u6b62\uff0c\u5e76\u4e14\u53ea\u6709 \u53d1\u9001\u7b2c\u4e00\u5c01\u7535\u5b50\u90ae\u4ef6\u3002\u6b64\u7535\u5b50\u90ae\u4ef6\u6291\u5236\u53ef\u9632\u6b62\u6cdb\u6ee5 \u5f00\u53d1\u4eba\u5458\u7684\u6536\u4ef6\u7bb1\u4e2d\u5305\u542b\u6709\u5173\u76f8\u540c\u9519\u8bef\u7684\u7535\u5b50\u90ae\u4ef6\u3002<\/li>\n\n\n\n<li>\u5bf9\u4e8e\u533f\u540d Apex \u9047\u5230\u7684\u5f02\u5e38\uff0c\u4e0d\u4f1a\u53d1\u9001\u7535\u5b50\u90ae\u4ef6 \u6267\u884c\u3002<\/li>\n\n\n\n<li>\u6bcf\u4e2a\u5e94\u7528\u7a0b\u5e8f\u6bcf\u5c0f\u65f6\u9650\u5236\u4e3a 10 \u5c01\u7535\u5b50\u90ae\u4ef6 Apex \u4f8b\u5916\u7535\u5b50\u90ae\u4ef6 \u670d\u52a1\u5668\u3002\u7531\u4e8e\u6b64\u9650\u5236\u4e0d\u662f\u57fa\u4e8e\u6bcf\u4e2a\u7ec4\u7ec7\uff0c\u56e0\u6b64\u7535\u5b50\u90ae\u4ef6\u4f20\u9012\u5230 \u7279\u5b9a\u7684\u7ec4\u7ec7\u53ef\u80fd\u4e0d\u53ef\u9760\u3002<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">\u7528\u6237\u754c\u9762\u4e2d\u672a\u5904\u7406\u7684\u5f02\u5e38<\/h2>\n\n\n\n<p>\u5982\u679c\u6700\u7ec8\u7528\u6237\u5728\u4f7f\u7528 \u6807\u51c6\u7528\u6237\u754c\u9762\uff0c\u5219\u663e\u793a\u9519\u8bef\u6d88\u606f\u3002\u9519\u8bef\u6d88\u606f\u5305\u542b\u6587\u672c \u4e0e\u6b64\u5904\u663e\u793a\u7684\u901a\u77e5\u7c7b\u4f3c\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/developer.salesforce.com\/docs\/resources\/img\/en-us\/246.0?doc_id=dev_guides%2Fapex%2Fimages%2Fapex_exceptions_unhandled_ui.png&amp;folder=apexcode\" alt=\"\u201c\u65b0\u5546\u54c1\u201d\u9875\u9762\u4e2d\u672a\u5904\u7406\u7684\u5f02\u5e38\"\/><\/figure>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u5f02\u5e38\u8bed\u53e5<\/strong><\/li>\n\n\n\n<li><strong>\u5f02\u5e38\u5904\u7406\u793a\u4f8b<\/strong><\/li>\n\n\n\n<li><strong>\u5185\u7f6e\u5f02\u5e38\u548c\u5e38\u7528\u65b9\u6cd5<\/strong><\/li>\n\n\n\n<li><strong>\u6355\u83b7\u4e0d\u540c\u7684\u5f02\u5e38\u7c7b\u578b<\/strong><\/li>\n\n\n\n<li><strong>\u521b\u5efa\u81ea\u5b9a\u4e49\u4f8b\u5916<\/strong><\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">\u5f02\u5e38\u8bed\u53e5<\/h2>\n\n\n\n<p><a><\/a>Apex \u4f7f\u7528<dfn>\u5f02\u5e38<\/dfn>\u6765\u8bb0\u5f55\u9519\u8bef\u548c\u5176\u4ed6\u4e8b\u4ef6 \u6270\u4e71\u4ee3\u7801\u6267\u884c\u7684\u6b63\u5e38\u6d41\u7a0b\u3002 \u8bed\u53e5\u53ef\u7528\u4e8e\u751f\u6210\u5f02\u5e38\uff0c\u800c \u3001 \u548c \u53ef\u4ee5 \u7528\u4e8e\u4ece\u5f02\u5e38\u4e2d\u6b63\u5e38\u6062\u590d\u3002<samp>throw<\/samp><samp>try<\/samp><samp>catch<\/samp><samp>finally<\/samp><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u629b\u51fa\u8bed\u53e5<\/h2>\n\n\n\n<p>\u8bed\u53e5\u5141\u8bb8\u60a8\u53d1\u51fa\u4fe1\u53f7 \u53d1\u751f\u9519\u8bef\u3002\u82e5\u8981\u5f15\u53d1\u5f02\u5e38\uff0c\u8bf7\u4f7f\u7528\u8be5\u8bed\u53e5\u5e76\u4e3a\u5176\u63d0\u4f9b \u4e00\u4e2a Exception \u5bf9\u8c61\uff0c\u7528\u4e8e\u63d0\u4f9b\u6709\u5173\u7279\u5b9a\u9519\u8bef\u7684\u4fe1\u606f\u3002 \u4f8b\u5982\uff1a<\/p>\n\n\n\n<p><samp>throw<\/samp><samp>throw<\/samp><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>throw<em><\/em> exceptionObject;<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u5c1d\u8bd5-\u6355\u6349-\u6700\u540e \u8bed\u53e5<\/h2>\n\n\n\n<p>\u3001 \u548c \u8bed\u53e5\u53ef\u7528\u4e8e\u4ece\u5f15\u53d1\u7684\u5f02\u5e38\u4e2d\u6b63\u5e38\u6062\u590d\uff1a<\/p>\n\n\n\n<p><samp>try<\/samp><samp>catch<\/samp><samp>finally<\/samp><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u8be5\u8bed\u53e5\u6807\u8bc6\u4e00\u4e2a \u53ef\u80fd\u53d1\u751f\u5f02\u5e38\u7684\u4ee3\u7801\u5757\u3002<samp>try<\/samp><\/li>\n\n\n\n<li>\u8be5\u8bed\u53e5\u6807\u8bc6\u4e00\u4e2a \u53ef\u4ee5\u5904\u7406\u7279\u5b9a\u7c7b\u578b\u5f02\u5e38\u7684\u4ee3\u7801\u5757\u3002\u5355\u4e2a\u8bed\u53e5\u53ef\u4ee5\u6709\u96f6\u6216 \u66f4\u591a\u76f8\u5173\u8bed\u53e5\u3002 \u6bcf\u4e2a\u8bed\u53e5\u5fc5\u987b\u5177\u6709 \u552f\u4e00\u7684\u5f02\u5e38\u7c7b\u578b\u3002\u6b64\u5916\uff0c\u4e00\u65e6\u6355\u83b7\u5230\u7279\u5b9a\u7684\u5f02\u5e38\u7c7b\u578b \u4e00\u4e2a\u533a\u5757\uff0c\u5176\u4f59\u533a\u5757\uff08\u5982\u679c\u6709\u7684\u8bdd\uff09\uff0c \u4e0d\u6267\u884c\u3002<samp>catch<\/samp><samp>try<\/samp><samp>catch<\/samp><samp>catch<\/samp><samp>catch<\/samp><samp>catch<\/samp><\/li>\n\n\n\n<li>\u8be5\u8bed\u53e5\u6807\u8bc6\u4e00\u4e2a \u4fdd\u8bc1\u6267\u884c\u7684\u4ee3\u7801\u5757\uff0c\u5e76\u5141\u8bb8\u60a8\u6e05\u7406\u60a8\u7684 \u6cd5\u5178\u3002\u5355\u4e2a\u8bed\u53e5\u53ef\u4ee5 \u6700\u591a\u6709\u4e00\u4e2a\u5173\u8054\u7684\u8bed\u53e5\u3002\u5757\u4e2d\u7684\u4ee3\u7801 \u65e0\u8bba\u662f\u5426\u5f15\u53d1\u5f02\u5e38\u6216\u7c7b\u578b\u5982\u4f55\uff0c\u59cb\u7ec8\u6267\u884c \u5f15\u53d1\u7684\u5f02\u5e38\u3002\u7531\u4e8e\u8be5\u5757\u59cb\u7ec8\u6267\u884c\uff0c\u56e0\u6b64\u8bf7\u5c06\u5176\u7528\u4e8e\u6e05\u7406\u4ee3\u7801\uff0c\u4f8b\u5982 \u81f3\u4e8e\u91ca\u653e\u8d44\u6e90\u3002<samp>finally<\/samp><samp>try<\/samp><samp>finally<\/samp><samp>finally<\/samp><samp>finally<\/samp><\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">\u8bed\u6cd5<\/h2>\n\n\n\n<p>\u3001 \u548c \u8bed\u53e5\u7684\u8bed\u6cd5 \u5982\u4e0b\u6240\u793a\u3002<samp>try<\/samp><samp>catch<\/samp><samp>finally<\/samp><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>try {\n  \/\/ Try block\n <em>code_block<\/em>\n} catch (<em>exceptionType<\/em> variableName) {\n  \/\/ Initial catch block.\n  \/\/ At least the catch block or the finally block must be present. \n <em>code_block<\/em>\n} catch (Exception e) {\n  \/\/ Optional additional catch statement for other exception types.\n  \/\/ Note that the general exception type, 'Exception',\n  \/\/ must be the last catch block when it is used.\n <em>code_block<\/em>\n} finally {\n  \/\/ Finally block.\n  \/\/ At least the catch block or the finally block must be present.\n <em>code_block<\/em>\n}<\/code><\/pre>\n\n\n\n<p>\u81f3\u5c11\u4e00\u4e2a\u5757\u6216\u4e00\u4e2a\u5757\u5fc5\u987b\u4e0e\u5757\u4e00\u8d77\u5b58\u5728\u3002\u4ee5\u4e0b\u662f try-catch \u5757\u3002<samp>catch<\/samp><samp>finally<\/samp><samp>try<\/samp><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>try {\n <em>code_block<\/em>\n} catch (<em>exceptionType<\/em> variableName) {\n <em>code_block<\/em>\n}\n\/\/ Optional additional catch blocks<\/code><\/pre>\n\n\n\n<p>\u4ee5\u4e0b\u662f try-finally \u5757\u7684\u8bed\u6cd5\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>try {\n <em>code_block<\/em>\n} finally {\n <em>code_block<\/em>\n}<\/code><\/pre>\n\n\n\n<p>\u8fd9\u662f try-catch-finally \u7684\u9aa8\u67b6\u793a\u4f8b \u5757\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>try {\n    \/\/ Perform some operation that \n    \/\/   might cause an exception.\n} catch(Exception e) {\n    \/\/ Generic exception handling code here.\n} finally {\n    \/\/ Perform some clean up.\n}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u65e0\u6cd5\u6355\u83b7\u7684\u5f02\u5e38<\/h2>\n\n\n\n<p>\u67d0\u4e9b\u7279\u6b8a\u7c7b\u578b\u7684\u5185\u7f6e\u5f02\u5e38\u65e0\u6cd5\u6355\u83b7\u3002\u8fd9\u4e9b\u4f8b\u5916\u4e0e \u95ea\u7535\u5e73\u53f0\u4e2d\u7684\u5371\u6025\u60c5\u51b5\u3002\u8fd9\u4e9b\u60c5\u51b5\u9700\u8981\u5815\u80ce \u7684\u4ee3\u7801\u6267\u884c\uff0c\u5e76\u4e14\u4e0d\u5141\u8bb8\u901a\u8fc7\u5f02\u5e38\u6062\u590d\u6267\u884c \u5904\u7406\u3002\u5176\u4e2d\u4e00\u4e2a\u5f02\u5e38\u662f\u9650\u5236\u5f02\u5e38 \uff08\uff09\uff0c\u5982\u679c\u8c03\u63a7\u5668\u9650\u5236\u5177\u6709 \u5df2\u8d85\u8fc7\uff0c\u4f8b\u5982\uff0c\u5f53\u53d1\u51fa\u7684\u6700\u5927 SOQL \u67e5\u8be2\u6570 \u8d85\u8fc7\u3002\u5176\u4ed6\u793a\u4f8b\u662f\u65ad\u8a00\u8bed\u53e5\u5931\u8d25\u65f6\u5f15\u53d1\u7684\u5f02\u5e38 \uff08\u901a\u8fc7\u65b9\u6cd5\uff09\u6216\u8bb8\u53ef\u8bc1 \u5f02\u5e38\u3002<samp>System.LimitException<\/samp><samp>System.assert<\/samp><\/p>\n\n\n\n<p>\u5f53\u5f02\u5e38\u65e0\u6cd5\u6355\u83b7\u65f6\uff0c\u5757\u4ee5\u53ca\u5757\uff08\u5982\u679c\u6709\uff09\u4e0d\u4f1a\u6355\u83b7 \u6267\u884c\u3002<samp>catch<\/samp><samp>finally<\/samp><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u7248\u672c\u5316\u884c\u4e3a\u66f4\u6539<\/h2>\n\n\n\n<p>\u5728 API \u7248\u672c 41.0 \u53ca\u66f4\u9ad8\u7248\u672c\u4e2d\uff0c\u4ee3\u7801\u4e2d\u7684 unreachable \u8bed\u53e5\u5c06\u5bfc\u81f4 \u7f16\u8bd1\u9519\u8bef\u3002\u4f8b\u5982\uff0c\u4ee5\u4e0b\u4ee3\u7801\u5757\u751f\u6210\u7f16\u8bd1\u65f6 API \u7248\u672c 41.0 \u53ca\u66f4\u9ad8\u7248\u672c\u4e2d\u7684\u9519\u8bef\u3002\u6c38\u8fdc\u65e0\u6cd5\u8fbe\u5230\u7b2c\u4e09\u79cd\u8bf4\u6cd5 \u56e0\u4e3a\u524d\u9762\u7684\u8bed\u53e5\u629b\u51fa\u4e86\u4e00\u4e2a\u65e0\u6761\u4ef6\u7684 \u4f8b\u5916\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Boolean x = true; \nthrow new NullPointerException();\nx = false;<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u5f02\u5e38\u5904\u7406\u793a\u4f8b<\/h2>\n\n\n\n<p>\u82e5\u8981\u67e5\u770b\u64cd\u4f5c\u4e2d\u7684\u5f02\u5e38\uff0c\u8bf7\u6267\u884c\u4e00\u4e9b\u5bfc\u81f4\u5f15\u53d1 DML \u5f02\u5e38\u7684\u4ee3\u7801\u3002 \u5728 Developer \u4e2d\u6267\u884c\u4ee5\u4e0b\u547d\u4ee4 \u5b89\u6170\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Merchandise__c m = new Merchandise__c();\ninsert m;<\/code><\/pre>\n\n\n\n<p>DML \u8bed\u53e5 \u5728\u793a\u4f8b\u4e2d\u5bfc\u81f4 DmlException\uff0c\u56e0\u4e3a\u6211\u4eec\u63d2\u5165 \u672a\u8bbe\u7f6e\u4efb\u4f55\u5fc5\u586b\u5b57\u6bb5\u7684\u5546\u54c1\u9879\u3002\u8fd9 \u662f\u60a8\u5728\u8c03\u8bd5\u65e5\u5fd7\u4e2d\u770b\u5230\u7684\u5f02\u5e38\u9519\u8bef\u3002<samp>insert<\/samp><\/p>\n\n\n\n<p><samp>System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [Description, Price, Total Inventory]: [Description, Price, Total Inventory]<\/samp>\u63a5\u4e0b\u6765\uff0c\u5728\u5f00\u53d1\u8005\u63a7\u5236\u53f0\u4e2d\u6267\u884c\u6b64\u4ee3\u7801\u6bb5\u3002\u5b83\u57fa\u4e8e\u524d\u9762\u7684\u793a\u4f8b\uff0c\u4f46 \u5305\u62ec\u4e00\u4e2a try-catch \u5757\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>try {\n    Merchandise__c m = new Merchandise__c();\n    insert m;\n} catch(DmlException e) {\n    System.debug('The following exception has occurred: ' + e.getMessage());\n}<\/code><\/pre>\n\n\n\n<p>\u8bf7\u6ce8\u610f\uff0c\u5f00\u53d1\u8005\u63a7\u5236\u53f0\u4e2d\u7684\u8bf7\u6c42\u72b6\u6001\u73b0\u5728\u62a5\u544a\u6210\u529f\u3002\u8fd9\u662f\u56e0\u4e3a \u4ee3\u7801\u5904\u7406\u5f02\u5e38\u3002<\/p>\n\n\n\n<p>\u5728\u5f02\u5e38\u4e4b\u540e\u53d1\u751f\u7684 try \u5757\u4e2d\u7684\u4efb\u4f55\u8bed\u53e5\u90fd\u662f \u8df3\u8fc7\uff0c\u4e0d\u6267\u884c\u3002\u4f8b\u5982\uff0c\u5982\u679c\u6dfb\u52a0\u8bed\u53e5 \u4e4b\u540e\uff0c\u6b64\u8bed\u53e5 \u4e0d\u4f1a\u88ab\u6267\u884c\u3002\u6267\u884c\u4ee5\u4e0b\u547d\u4ee4\uff1a<samp>insert&nbsp;m;<\/samp><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>try {\n    Merchandise__c m = new Merchandise__c();\n    insert m;\n    \/\/ This doesn't execute since insert causes an exception\n    System.debug('Statement after insert.');\n} catch(DmlException e) {\n    System.debug('The following exception has occurred: ' + e.getMessage());\n}<\/code><\/pre>\n\n\n\n<p>\u5728\u65b0\u7684\u8c03\u8bd5\u65e5\u5fd7\u6761\u76ee\u4e2d\uff0c\u8bf7\u6ce8\u610f\uff0c\u60a8\u6ca1\u6709\u770b\u5230 \u7684\u8c03\u8bd5\u6d88\u606f\u3002\u8fd9\u662f\u56e0\u4e3a\u6b64\u8c03\u8bd5\u8bed\u53e5\u53d1\u751f\u5728\u5f02\u5e38\u4e4b\u540e \u7531\u63d2\u5165\u5f15\u8d77\uff0c\u5e76\u4e14\u6c38\u8fdc\u4e0d\u4f1a\u88ab\u6267\u884c\u3002\u7ee7\u7eed\u6267\u884c \u7684\u4ee3\u7801\u8bed\u53e5\uff0c\u5c06\u8bed\u53e5\u653e\u5728 \u5728 try-catch \u5757\u4e4b\u540e\u3002\u6267\u884c\u8fd9\u4e2a\u4fee\u6539\u540e\u7684\u4ee3\u7801\u7247\u6bb5\uff0c\u7136\u540e \u8bf7\u6ce8\u610f\uff0c\u8c03\u8bd5\u65e5\u5fd7\u73b0\u5728\u5177\u6709 \u7684\u8c03\u8bd5\u6d88\u606f\u3002<samp>Statement after insert<\/samp><samp>Statement after insert<\/samp><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>try {\n    Merchandise__c m = new Merchandise__c();\n    insert m;\n} catch(DmlException e) {\n    System.debug('The following exception has occurred: ' + e.getMessage());\n}\n\/\/ This will get executed\nSystem.debug('Statement after insert.');<\/code><\/pre>\n\n\n\n<p>\u6216\u8005\uff0c\u60a8\u53ef\u4ee5\u5305\u542b\u5176\u4ed6 try-catch \u5757\u3002\u8fd9 \u4ee3\u7801\u7247\u6bb5\u5728\u7b2c\u4e8c\u4e2a try-catch \u5757\u4e2d\u5177\u6709\u8be5\u8bed\u53e5\u3002\u6267\u884c\u5b83\u4ee5\u67e5\u770b \u4f60\u5f97\u5230\u548c\u4ee5\u524d\u4e00\u6837\u7684\u7ed3\u679c\u3002<samp>System.debug<\/samp><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>try {\n    Merchandise__c m = new Merchandise__c();\n    insert m;\n} catch(DmlException e) {\n    System.debug('The following exception has occurred: ' + e.getMessage());\n}\n\ntry {\n    System.debug('Statement after insert.');\n    \/\/ Insert other records\n}\ncatch (Exception e) {\n    \/\/ Handle this exception here\n}<\/code><\/pre>\n\n\n\n<p>\u65e0\u8bba\u51fa\u73b0\u4ec0\u4e48\u5f02\u5e38\uff0cfinally \u5757\u59cb\u7ec8\u6267\u884c \u88ab\u629b\u51fa\uff0c\u5373\u4f7f\u6ca1\u6709\u629b\u51fa\u5f02\u5e38\u3002\u8ba9\u6211\u4eec\u6765\u770b\u770b\u5427 \u5728\u884c\u52a8\u4e2d\u4f7f\u7528\u3002\u6267\u884c\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ Declare the variable outside the try-catch block\n\/\/ so that it will be in scope for all blocks.\nXmlStreamWriter w = null;\ntry {\n    w = new XmlStreamWriter();\n    w.writeStartDocument(null, '1.0');\n    w.writeStartElement(null, 'book', null);\n    w.writeCharacters('This is my book');\n    w.writeEndElement(); \n    w.writeEndDocument();\n\n    \/\/ Perform some other operations\n    String s;\n    \/\/ This causes an exception because\n    \/\/ the string hasn't been assigned a value.\n    Integer i = s.length();\n} catch(Exception e) {\n    System.debug('An exception occurred: ' + e.getMessage());\n} finally {\n    \/\/ This gets executed after the exception is handled\n    System.debug('Closing the stream writer in the finally block.');\n    \/\/ Close the stream writer\n    w.close();\n}<\/code><\/pre>\n\n\n\n<p>\u524d\u9762\u7684\u4ee3\u7801\u7247\u6bb5\u521b\u5efa\u4e00\u4e2a XML \u6d41\u7f16\u5199\u5668\uff0c\u5e76\u5c06 \u4e00\u4e9b XML \u5143\u7d20\u3002\u63a5\u4e0b\u6765\uff0c\u7531\u4e8e\u8bbf\u95ee null \u5b57\u7b26\u4e32\u53d8\u91cf\u3002\u8fd9 catch \u5757\u5904\u7406\u6b64\u5f02\u5e38\u3002\u7136\u540e\u6267\u884c finally \u5757\u3002 \u5b83\u5199\u5165\u8c03\u8bd5\u6d88\u606f\u5e76\u5173\u95ed\u6d41\u7f16\u5199\u5668\uff0c\u4ece\u800c\u91ca\u653e \u4efb\u4f55\u5173\u8054\u7684\u8d44\u6e90\u3002\u68c0\u67e5\u8c03\u8bd5\u65e5\u5fd7\u4e2d\u7684\u8c03\u8bd5\u8f93\u51fa\u3002 \u5f02\u5e38\u540e\uff0c\u4f60\u5c06\u770b\u5230\u8c03\u8bd5\u6d88\u606f \u9519\u8bef\u3002\u8fd9\u544a\u8bc9\u60a8\uff0c\u5728\u5f02\u5e38\u4e4b\u540e\u6267\u884c\u7684 finally \u5757 \u88ab\u6293\u4f4f\u4e86\u3002<samp>s<\/samp><samp>Closing the stream writer in the finally block.<\/samp><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u5185\u7f6e\u5f02\u5e38\u548c\u5e38\u7528\u65b9\u6cd5<\/h2>\n\n\n\n<p>Apex \u63d0\u4f9b\u4e86\u8bb8\u591a\u5185\u7f6e\u7684\u5f02\u5e38\u7c7b\u578b\uff0c\u8fd0\u884c\u65f6\u5f15\u64ce\u4f1a\u5728\u4ee5\u4e0b\u60c5\u51b5\u4e0b\u629b\u51fa\u8fd9\u4e9b\u5f02\u5e38\u7c7b\u578b \u5728\u6267\u884c\u8fc7\u7a0b\u4e2d\u9047\u5230\u9519\u8bef\u3002\u4f60\u5df2\u5728\u524d\u9762\u7684 \u4f8b\u3002\u4e0b\u9762\u662f\u4e00\u4e9b\u5176\u4ed6\u5185\u7f6e\u5f02\u5e38\u7684\u793a\u4f8b\u3002\u6709\u5173\u5b8c\u6574\u5217\u8868 \u5185\u7f6e\u5f02\u5e38\u7c7b\u578b\uff0c\u8bf7\u53c2\u9605\u00a0Exception \u7c7b\u548c\u5185\u7f6e\u5f02\u5e38\u3002DmlExceptionDML \u8bed\u53e5\u7684\u4efb\u4f55\u95ee\u9898\uff0c\u4f8b\u5982\u8bed\u53e5\u5728\u8bb0\u5f55\u4e2d\u7f3a\u5c11\u5fc5\u586b\u5b57\u6bb5\u3002<samp>insert<\/samp>\u6b64\u793a\u4f8b\u4f7f\u7528 DmlException\u3002\u6b64\u793a\u4f8b\u4e2d\u7684 DML \u8bed\u53e5\u4f1a\u5bfc\u81f4 DmlException\uff0c\u56e0\u4e3a \u5b83\u63d2\u5165\u5546\u54c1\u9879\u76ee\u800c\u4e0d\u8bbe\u7f6e\u4efb\u4f55\u6240\u9700\u7684\u5185\u5bb9 \u9886\u57df\u3002\u6b64\u5f02\u5e38\u5728\u5757\u4e2d\u6355\u83b7\uff0c\u5f02\u5e38\u6d88\u606f\u5c06\u5199\u5165\u8c03\u8bd5\u65e5\u5fd7 \u4f7f\u7528\u8bed\u53e5\u3002<samp>insertcatchSystem.debug<\/samp><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>try {\n    Merchandise__c m = new Merchandise__c();\n    insert m;\n} catch(DmlException e) {\n    System.debug('The following exception has occurred: ' + e.getMessage());\n}<\/code><\/pre>\n\n\n\n<p>\u5217\u8868\u5f02\u5e38\u5217\u8868\u7684\u4efb\u4f55\u95ee\u9898\uff0c\u4f8b\u5982\u5c1d\u8bd5\u8bbf\u95ee\u8d85\u51fa \u8fb9\u754c\u3002\u6b64\u793a\u4f8b\u521b\u5efa\u4e00\u4e2a\u5217\u8868\u5e76\u5411\u5176\u6dfb\u52a0\u4e00\u4e2a\u5143\u7d20\u3002\u7136\u540e\uff0c\u8fdb\u884c\u5c1d\u8bd5 \u8bbf\u95ee\u4e24\u4e2a\u5143\u7d20\uff0c\u4e00\u4e2a\u4f4d\u4e8e\u7d22\u5f15 0\uff08\u5b58\u5728\uff09\uff0c\u53e6\u4e00\u4e2a\u4f4d\u4e8e\u7d22\u5f15 1\uff08\u5176\u4e2d \u5bfc\u81f4\u5f15\u53d1 ListException\uff0c\u56e0\u4e3a\u6b64\u7d22\u5f15\u5904\u4e0d\u5b58\u5728\u4efb\u4f55\u5143\u7d20\u3002 \u6b64\u5f02\u5e38\u5728 catch \u5757\u4e2d\u88ab\u6355\u83b7\u3002catch \u5757\u4e2d\u7684\u8bed\u53e5\u5c06\u4ee5\u4e0b\u5185\u5bb9\u5199\u5165 \u8c03\u8bd5\u65e5\u5fd7\uff1a\u3002<samp>System.debug<\/samp><samp>The following exception has occurred: List index out of bounds: 1<\/samp><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>try {\n    List&lt;Integer&gt; li = new List&lt;Integer&gt;();\n    li.add(15);\n    \/\/ This list contains only one element,\n    \/\/ but we're attempting to access the second element\n    \/\/ from this zero-based list.\n    Integer i1 = li&#91;0]; \n    Integer i2 = li&#91;1]; \/\/ Causes a ListException\n} catch(ListException le) {\n    System.debug('The following exception has occurred: ' + le.getMessage());\n}<\/code><\/pre>\n\n\n\n<p>NullPointerException\u53d6\u6d88\u5f15\u7528\u53d8\u91cf\u7684\u4efb\u4f55\u95ee\u9898\u3002<samp>null<\/samp>\u6b64\u793a\u4f8b\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a String \u7684\u53d8\u91cf\uff0c\u4f46\u6211\u4eec\u4e0d\u5c06\u5176\u521d\u59cb\u5316\u4e3a\u503c\uff0c\u56e0\u6b64\u5b83\u662f null\u3002 \u5728\u6211\u4eec\u7684 null \u4e0a\u8c03\u7528\u8be5\u65b9\u6cd5 \u53d8\u91cf\u5bfc\u81f4 NullPointerException\u3002\u4f8b\u5916\u60c5\u51b5\u5728\u6211\u4eec\u7684\u6355\u83b7\u4e2d\u88ab\u53d1\u73b0 \u5757\uff0c\u8fd9\u662f\u5199\u5165\u8c03\u8bd5\u65e5\u5fd7\u7684\u5185\u5bb9\uff1a\u3002<samp>s<\/samp><samp>contains<\/samp><samp>The following exception has occurred: Attempt to de-reference a null object<\/samp><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>try {\n    String s;\n    Boolean b = s.contains('abc'); \/\/ Causes a NullPointerException\n} catch(NullPointerException npe) {\n    System.debug('The following exception has occurred: ' + npe.getMessage());\n}<\/code><\/pre>\n\n\n\n<p>\u67e5\u8be2\u5f02\u5e38SOQL \u67e5\u8be2\u7684\u4efb\u4f55\u95ee\u9898\uff0c\u4f8b\u5982\u5206\u914d\u4e0d\u8fd4\u56de\u4efb\u4f55\u8bb0\u5f55\u7684\u67e5\u8be2 \u6216\u591a\u6761\u8bb0\u5f55\u5230\u4e00\u4e2a\u5355\u4e00\u5b9e\u4f8b sObject \u53d8\u91cf\u3002\u6b64\u793a\u4f8b\u4e2d\u7684\u7b2c\u4e8c\u4e2a SOQL \u67e5\u8be2\u4f1a\u5bfc\u81f4 QueryException\u3002\u793a\u4f8b \u5c06 Merchandise \u5bf9\u8c61\u5206\u914d\u7ed9\u4ece\u67e5\u8be2\u8fd4\u56de\u7684\u5185\u5bb9\u3002\u8bf7\u6ce8\u610f\u5728\u67e5\u8be2\u4e2d\u4f7f\u7528\u3002\u8fd9\u786e\u4fdd\u4e86 \u4ece\u6570\u636e\u5e93\u4e2d\u6700\u591a\u8fd4\u56de\u4e00\u4e2a\u5bf9\u8c61\uff0c\u56e0\u6b64\u6211\u4eec\u53ef\u4ee5\u5c06\u5176\u5206\u914d\u7ed9 \u5355\u4e2a\u5bf9\u8c61\uff0c\u800c\u4e0d\u662f\u5217\u8868\u3002\u4f46\u662f\uff0c\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u6ca1\u6709 \u5546\u54c1\u540d\u4e3a XYZ\uff0c\u56e0\u6b64\u4e0d\u4f1a\u8fd4\u56de\u4efb\u4f55\u5185\u5bb9\uff0c\u5e76\u4e14\u5c1d\u8bd5\u5c06 \u5355\u4e2a\u5bf9\u8c61\u7684\u8fd4\u56de\u503c\u4f1a\u5bfc\u81f4 QueryException\u3002\u4f8b\u5916\u60c5\u51b5\u662f \u6355\u83b7\u5230\u6211\u4eec\u7684 catch \u5757\u4e2d\uff0c\u8fd9\u5c31\u662f\u60a8\u5c06\u5728\u8c03\u8bd5\u65e5\u5fd7\u4e2d\u770b\u5230\u7684\u5185\u5bb9\uff1a.<samp>LIMIT 1<\/samp><samp>The following exception has occurred: List has no rows for assignment to SObject<\/samp><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>try {\n    \/\/ This statement doesn't cause an exception, even though \n    \/\/ we don't have a merchandise with name='XYZ'.\n    \/\/ The list will just be empty.\n    List&lt;Merchandise__c&gt; lm = &#91;SELECT Name FROM Merchandise__c WHERE Name = 'XYZ'];\n    \/\/ lm.size() is 0 \n    System.debug(lm.size());\n    \n    \/\/ However, this statement causes a QueryException because \n    \/\/ we're assiging the return value to a Merchandise__c object\n    \/\/ but no Merchandise is returned.\n    Merchandise__c m = &#91;SELECT Name FROM Merchandise__c WHERE Name = 'XYZ' LIMIT 1];\n} catch(QueryException qe) {\n    System.debug('The following exception has occurred: ' + qe.getMessage());    \n}<\/code><\/pre>\n\n\n\n<p>SObjectExceptionsObject \u8bb0\u5f55\u7684\u4efb\u4f55\u95ee\u9898\uff0c\u4f8b\u5982\u5c1d\u8bd5\u66f4\u6539\u8bed\u53e5\u4e2d\u7684\u5b57\u6bb5\uff0c\u8be5\u5b57\u6bb5\u53ea\u80fd\u662f \u671f\u95f4\u66f4\u6539\u3002<samp>update<\/samp><samp>insert<\/samp>\u6b64\u793a\u4f8b\u5728 try \u5757\u4e2d\u751f\u6210 SObjectException\uff0c\u8be5\u5757\u5728 catch \u5757\u3002\u8be5\u793a\u4f8b\u67e5\u8be2\u53d1\u7968\u5bf9\u5e10\u5355\uff0c\u5e76\u4ec5\u9009\u62e9\u5176 \u540d\u79f0\u5b57\u6bb5\u3002\u7136\u540e\uff0c\u5b83\u5c1d\u8bd5\u83b7\u53d6\u67e5\u8be2\u7684 Description__c \u5b57\u6bb5 sObject\uff0c\u5b83\u4e0d\u53ef\u7528\uff0c\u56e0\u4e3a\u5b83\u4e0d\u5728 \u5728 SELECT \u8bed\u53e5\u4e2d\u67e5\u8be2\u7684\u5b57\u6bb5\u3002\u8fd9\u4f1a\u5bfc\u81f4 SObjectException\u3002 \u8fd9\u4e2a\u5f02\u5e38\u5728\u6211\u4eec\u7684 catch \u5757\u4e2d\u88ab\u6355\u83b7\uff0c\u8fd9\u5c31\u662f\u4f60\u5c06\u770b\u5230\u7684 \u5728\u8c03\u8bd5\u65e5\u5fd7\u4e2d\uff1a\u3002<samp>The following exception has occurred: SObject row was retrieved via SOQL without querying the requested field: Invoice_Statement__c.Description__c<\/samp><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>try {\n    Invoice_Statement__c inv = new Invoice_Statement__c(\n        Description__c='New Invoice');\n    insert inv;\n\n    \/\/ Query the invoice we just inserted\n    Invoice_Statement__c v = &#91;SELECT Name FROM Invoice_Statement__c WHERE Id = :inv.Id];\n    \/\/ Causes an SObjectException because we didn't retrieve\n    \/\/ the Description__c field.\n    String s = v.Description__c;\n} catch(SObjectException se) {\n    System.debug('The following exception has occurred: ' + se.getMessage());\n}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u5e38\u89c1\u5f02\u5e38\u65b9\u6cd5<\/h2>\n\n\n\n<p>\u60a8\u53ef\u4ee5\u4f7f\u7528\u5e38\u89c1\u7684\u5f02\u5e38\u65b9\u6cd5\u6765\u83b7\u53d6\u6709\u5173\u5f02\u5e38\u7684\u8be6\u7ec6\u4fe1\u606f\uff0c\u4f8b\u5982 \u4f5c\u4e3a\u5f02\u5e38\u9519\u8bef\u6d88\u606f\u6216\u5806\u6808\u8ddf\u8e2a\u3002\u524d\u9762\u7684\u793a\u4f8b\u8c03\u7528\u8be5\u65b9\u6cd5\uff0c\u8be5\u65b9\u6cd5\u8fd4\u56de \u4e0e\u5f02\u5e38\u5173\u8054\u7684\u9519\u8bef\u6d88\u606f\u3002\u8fd8\u6709\u5176\u4ed6\u5f02\u5e38\u65b9\u6cd5 \u4e5f\u53ef\u7528\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u6709\u7528\u65b9\u6cd5\u7684\u8bf4\u660e\uff1a<\/p>\n\n\n\n<p><samp>getMessage<\/samp><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><samp>getCause<\/samp>\uff1a\u8fd4\u56de exception \u4f5c\u4e3a\u5f02\u5e38\u5bf9\u8c61\u3002<\/li>\n\n\n\n<li><samp>getLineNumber<\/samp>\uff1a\u8fd4\u56de\u8be5\u884c \u5f15\u53d1\u5f02\u5e38\u7684\u7f16\u53f7\u3002<\/li>\n\n\n\n<li><samp>getMessage<\/samp>\uff1a\u8fd4\u56de\u9519\u8bef \u4e3a\u7528\u6237\u663e\u793a\u7684\u6d88\u606f\u3002<\/li>\n\n\n\n<li><samp>getStackTraceString<\/samp>\uff1a\u8fd4\u56de \u4ee5\u5b57\u7b26\u4e32\u5f62\u5f0f\u5f15\u53d1\u5f02\u5e38\u7684\u5806\u6808\u8ddf\u8e2a\u3002<\/li>\n\n\n\n<li><samp>getTypeName<\/samp>\uff1a\u8fd4\u56de \u5f02\u5e38\uff0c\u4f8b\u5982 DmlException\u3001ListException\u3001MathException \u7b49 \u4e0a\u3002<\/li>\n<\/ul>\n\n\n\n<p><strong>\u4f8b<\/strong><\/p>\n\n\n\n<p>\u82e5\u8981\u4e86\u89e3\u4e00\u4e9b\u5e38\u89c1\u65b9\u6cd5\u8fd4\u56de\u7684\u5185\u5bb9\uff0c\u8bf7\u5c1d\u8bd5\u8fd0\u884c\u6b64\u793a\u4f8b\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>try {\n    Merchandise__c m = &#91;SELECT Name FROM Merchandise__c LIMIT 1];\n    \/\/ Causes an SObjectException because we didn't retrieve\n    \/\/ the Total_Inventory__c field.\n    Double inventory = m.Total_Inventory__c;\n} catch(Exception e) {\n    System.debug('Exception type caught: ' + e.getTypeName());    \n    System.debug('Message: ' + e.getMessage());    \n    System.debug('Cause: ' + e.getCause());    \/\/ returns null\n    System.debug('Line number: ' + e.getLineNumber());    \n    System.debug('Stack trace: ' + e.getStackTraceString());    \n}<\/code><\/pre>\n\n\n\n<p>\u6240\u6709\u8bed\u53e5\u7684\u8f93\u51fa \u5982\u4e0b\u6240\u793a\uff1a<samp>System.debug<\/samp><\/p>\n\n\n\n<p><samp>17:38:04:149 USER_DEBUG [7]|DEBUG|Exception type caught: System.SObjectException<\/samp><\/p>\n\n\n\n<p><samp>17:38:04:149 USER_DEBUG [8]|DEBUG|Message: SObject row was retrieved via SOQL without querying the requested field: Merchandise__c.Total_Inventory__c<\/samp><\/p>\n\n\n\n<p><samp>17:38:04:150 USER_DEBUG [9]|DEBUG|Cause: null<\/samp><\/p>\n\n\n\n<p><samp>17:38:04:150 USER_DEBUG [10]|DEBUG|Line number: 5<\/samp><\/p>\n\n\n\n<p><samp>17:38:04:150 USER_DEBUG [11]|DEBUG|Stack trace: AnonymousBlock: line 5, column 1<\/samp><\/p>\n\n\n\n<p>catch \u8bed\u53e5\u53c2\u6570\u7c7b\u578b\u662f\u6cdb\u578b Exception \u7c7b\u578b\u3002\u5b83\u6293\u4f4f\u4e86\u66f4\u591a \u7279\u5b9a\u7684 SObjectException\u3002\u60a8\u53ef\u4ee5\u901a\u8fc7\u68c0\u67e5\u9000\u8d27\u6765\u9a8c\u8bc1\u662f\u5426\u5982\u6b64 \u7684\u503c \u8f93\u51fa\u3002\u8f93\u51fa\u8fd8\u5305\u542b SObjectException \u7684\u5176\u4ed6\u5c5e\u6027\uff0c\u5982 \u9519\u8bef\u6d88\u606f\u3001\u53d1\u751f\u5f02\u5e38\u7684\u884c\u53f7\u548c\u5806\u6808\u8ddf\u8e2a\u3002 \u60a8\u53ef\u80fd\u60f3\u77e5\u9053\u4e3a\u4ec0\u4e48\u8fd4\u56de null\u3002\u8fd9\u662f\u56e0\u4e3a\u5728\u6211\u4eec\u7684\u6837\u672c\u4e2d\uff0c\u4e4b\u524d\u6ca1\u6709\u5f02\u5e38\uff08\u5185\u90e8 exception\uff09\uff0c\u5bfc\u81f4\u6b64\u5f02\u5e38\u3002\u5728\u00a0Create Custom Exceptions\u00a0\u4e2d\uff0c\u60a8\u5c06\u770b\u5230\u4e00\u4e2a\u793a\u4f8b\uff0c\u5176\u4e2d \u7684\u8fd4\u56de\u503c\u662f\u5b9e\u9645\u503c \u4f8b\u5916\u3002<samp>e.getTypeName()getCausegetCause<\/samp><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u66f4\u591a\u5f02\u5e38\u65b9\u6cd5<\/h2>\n\n\n\n<p>\u67d0\u4e9b\u5f02\u5e38\u7c7b\u578b\uff08\u5982 DmlException\uff09\u5177\u6709\u7279\u5b9a\u7684\u5f02\u5e38\u65b9\u6cd5\uff0c\u8fd9\u4e9b\u65b9\u6cd5 \u4ec5\u9002\u7528\u4e8e\u5b83\u4eec\uff0c\u4e0d\u9002\u7528\u4e8e\u5176\u4ed6\u5f02\u5e38\u7c7b\u578b\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><samp>getDmlFieldNames(Index of the failed record)<\/samp>\uff1a\u8fd4\u56de\u5bfc\u81f4\u9519\u8bef\u7684\u5b57\u6bb5\u7684\u540d\u79f0 \u6307\u5b9a\u7684\u5931\u8d25\u8bb0\u5f55\u3002<\/li>\n\n\n\n<li><samp>getDmlId(Index of the failed record)<\/samp>: \u8fd4\u56de\u5bfc\u81f4\u6307\u5b9a\u9519\u8bef\u7684\u5931\u8d25\u8bb0\u5f55\u7684 ID \u5931\u8d25\u7684\u8bb0\u5f55\u3002<\/li>\n\n\n\n<li><samp>getDmlMessage(Index of the failed record)<\/samp>\uff1a\u8fd4\u56de\u6307\u5b9a\u5931\u8d25\u7684\u9519\u8bef\u6d88\u606f \u8bb0\u5f55\u3002<\/li>\n\n\n\n<li><samp>getNumDml<\/samp>\uff1a\u8fd4\u56de\u5931\u8d25\u7684\u6b21\u6570 \u8bb0\u5f55\u3002<\/li>\n<\/ul>\n\n\n\n<p><strong>\u4f8b<\/strong>\u6b64\u4ee3\u7801\u7247\u6bb5\u5229\u7528 DmlException \u65b9\u6cd5\u83b7\u53d6\u6709\u5173 \u63d2\u5165 Merchandise \u5bf9\u8c61\u5217\u8868\u65f6\u8fd4\u56de\u7684\u5f02\u5e38\u3002\u9879\u76ee\u6e05\u5355 \u8981\u63d2\u5165\u5305\u542b\u4e09\u4e2a\u9879\u76ee\uff0c\u5176\u4e2d\u6700\u540e\u4e24\u4e2a\u4e0d\u662f\u5fc5\u9700\u7684 \u5b57\u6bb5\u548c\u539f\u56e0 \u5f02\u5e38\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Merchandise__c m1 = new Merchandise__c(\n    Name='Coffeemaker',\n    Description__c='Kitchenware',\n    Price__c=25,\n    Total_Inventory__c=1000);\n\/\/ Missing the Price and Total_Inventory fields\nMerchandise__c m2 = new Merchandise__c(\n    Name='Coffeemaker B',\n    Description__c='Kitchenware');\n\/\/ Missing all required fields\nMerchandise__c m3 = new Merchandise__c();\nMerchandise__c&#91;] mList = new List&lt;Merchandise__c&gt;();\nmList.add(m1);\nmList.add(m2);\nmList.add(m3);\n\ntry {\n    insert mList;\n} catch (DmlException de) {\n    Integer numErrors = de.getNumDml();\n    System.debug('getNumDml=' + numErrors);\n    for(Integer i=0;i&lt;numErrors;i++) {\n        System.debug('getDmlFieldNames=' + de.getDmlFieldNames(i));\n        System.debug('getDmlMessage=' + de.getDmlMessage(i));  \n    }\n}<\/code><\/pre>\n\n\n\n<p>\u8bf7\u6ce8\u610f\uff0c\u4e0a\u9762\u7684\u793a\u4f8b\u5728\u5c1d\u8bd5\u4e2d\u6ca1\u6709\u5305\u542b\u6240\u6709\u521d\u59cb\u4ee3\u7801 \u5757\u3002\u53ea\u6709\u53ef\u80fd\u751f\u6210\u5f02\u5e38\u7684\u4ee3\u7801\u90e8\u5206\u624d\u4f1a\u88ab\u5305\u88c5 \u5728\u5757\u5185\uff0c\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u8be5\u8bed\u53e5\u53ef\u80fd\u4f1a\u5728 \u5982\u679c\u8f93\u5165\u6570\u636e\u65e0\u6548\u3002\u64cd\u4f5c\u5bfc\u81f4\u7684\u5f02\u5e38\u7531\u5176\u540e\u9762\u7684\u5757\u6355\u83b7\u3002\u6267\u884c\u540e \u6b64\u793a\u4f8b\u4e2d\uff0c\u4f60\u5c06\u770b\u5230\u7c7b\u4f3c\u4e8e\u4ee5\u4e0b\u5185\u5bb9\u7684\u8bed\u53e5\u8f93\u51fa\uff1a<samp>try<\/samp><samp>insert<\/samp><samp>insert<\/samp><samp>catch<\/samp><samp>System.debug<\/samp><\/p>\n\n\n\n<p><samp>14:01:24:939 USER_DEBUG [20]|DEBUG|getNumDml=2<\/samp><\/p>\n\n\n\n<p><samp>14:01:24:941 USER_DEBUG [23]|DEBUG|getDmlFieldNames=(Price, Total Inventory)<\/samp><\/p>\n\n\n\n<p><samp>14:01:24:941 USER_DEBUG [24]|DEBUG|getDmlMessage=Required fields are missing: [Price, Total Inventory]<\/samp><\/p>\n\n\n\n<p><samp>14:01:24:942 USER_DEBUG [23]|DEBUG|getDmlFieldNames=(Description, Price, Total Inventory)<\/samp><\/p>\n\n\n\n<p><samp>14:01:24:942 USER_DEBUG [24]|DEBUG|getDmlMessage=Required fields are missing: [Description, Price, Total Inventory]<\/samp><\/p>\n\n\n\n<p>DML \u5931\u8d25\u6570\u6b63\u786e\u62a5\u544a\u4e3a 2\uff0c\u56e0\u4e3a\u6211\u4eec\u5217\u8868\u4e2d\u6709\u4e24\u4e2a\u9879\u76ee \u63d2\u5165\u5931\u8d25\u3002\u6b64\u5916\uff0c\u5bfc\u81f4\u5931\u8d25\u7684\u5b57\u6bb5\u540d\u79f0\u548c\u9519\u8bef\u6d88\u606f \u5bf9\u4e8e\u6bcf\u4e2a\u5931\u8d25\u7684\u8bb0\u5f55\uff0c\u5c06\u5199\u5165\u8f93\u51fa\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u6355\u83b7\u4e0d\u540c\u7684\u5f02\u5e38\u7c7b\u578b<\/h2>\n\n\n\n<p>\u5728\u524d\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86 catch \u5757\u3002\u6211\u4eec\u4e5f\u53ef\u4ee5\u6355\u83b7\u901a\u7528\u5f02\u5e38 \u952e\u5165\u6240\u6709\u793a\u4f8b\uff0c\u8fd9\u5c06\u6355\u83b7\u6240\u6709\u5f02\u5e38\u7c7b\u578b\u3002\u4f8b\u5982 \u5c1d\u8bd5\u8fd0\u884c\u6b64\u793a\u4f8b\uff0c\u8be5\u793a\u4f8b\u4f1a\u5f15\u53d1 SObjectException \u5e76\u5177\u6709 catch \u8bed\u53e5\uff0c\u53c2\u6570\u7c7b\u578b\u4e3a Exception\u3002The SObjectException \u88ab catch \u5757\u5361\u4f4f\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>try {\n    Merchandise__c m = &#91;SELECT Name FROM Merchandise__c LIMIT 1];\n    \/\/ Causes an SObjectException because we didn't retrieve\n    \/\/ the Total_Inventory__c field.\n    Double inventory = m.Total_Inventory__c;\n} catch(Exception e) {\n    System.debug('The following exception has occurred: ' + e.getMessage());    \n}<\/code><\/pre>\n\n\n\n<p>\u6216\u8005\uff0c\u60a8\u53ef\u4ee5\u6709\u591a\u4e2a catch \u5757 &#8211; \u4e00\u4e2a catch \u5757\uff0c\u4ee5\u53ca\u6355\u83b7\u7684\u6700\u7ec8 catch \u5757 \u6cdb\u578b Exception \u7c7b\u578b\u3002\u8bf7\u770b\u8fd9\u4e2a\u4f8b\u5b50\u3002\u8bf7\u6ce8\u610f\uff0c\u5b83\u6709 \u4e09\u4e2a\u6355\u83b7\u5757\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>try {\n    Merchandise__c m = &#91;SELECT Name FROM Merchandise__c LIMIT 1];\n    \/\/ Causes an SObjectException because we didn't retrieve\n    \/\/ the Total_Inventory__c field.\n    Double inventory = m.Total_Inventory__c;\n} catch(DmlException e) {\n    System.debug('DmlException caught: ' + e.getMessage());    \n} catch(SObjectException e) {\n    System.debug('SObjectException caught: ' + e.getMessage());    \n} catch(Exception e) {\n    System.debug('Exception caught: ' + e.getMessage());    \n}<\/code><\/pre>\n\n\n\n<p>\u8bf7\u8bb0\u4f4f\uff0c\u53ea\u6267\u884c\u4e00\u4e2a catch \u5757\uff0c\u5176\u4f59\u7684 catch \u5757 \u90a3\u4e9b\u88ab\u7ed5\u8fc7\u3002\u6b64\u793a\u4f8b\u4e0e\u4e0a\u4e00\u4e2a\u793a\u4f8b\u7c7b\u4f3c\uff0c\u4f46 \u5b83\u6709\u66f4\u591a\u7684\u6355\u83b7\u5757\u3002\u8fd0\u884c\u6b64\u4ee3\u7801\u6bb5\u65f6\uff0c\u4e00\u4e2a SObjectException \u5728\u4ee5\u4e0b\u884c\u4e0a\u629b\u51fa\uff1a\u3002\u6bcf\u4e2a\u6355\u83b7\u5757 \u6309\u6307\u5b9a\u7684\u987a\u5e8f\u8fdb\u884c\u68c0\u67e5\uff0c\u4ee5\u67e5\u627e\u629b\u51fa\u7684\u5339\u914d\u9879 exception \u548c catch \u5757\u53c2\u6570\u4e2d\u6307\u5b9a\u7684\u5f02\u5e38\u7c7b\u578b\uff1a<\/p>\n\n\n\n<p><samp>Double&nbsp;inventory = m.Total_Inventory__c;<\/samp><\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u7b2c\u4e00\u4e2a catch \u5757\u53c2\u6570\u7684\u7c7b\u578b\u4e3a DmlException\uff0c\u5176\u7c7b\u578b\u4e3a \u4e0e\u5f15\u53d1\u7684\u5f02\u5e38 \uff08SObjectException\uff09 \u4e0d\u5339\u914d\u3002<\/li>\n\n\n\n<li>\u7b2c\u4e8c\u4e2a catch \u5757\u53c2\u6570\u7684\u7c7b\u578b\u4e3a SObjectException\uff0c\u5176 \u4e0e\u6211\u4eec\u7684\u5f02\u5e38\u5339\u914d\uff0c\u56e0\u6b64\u6b64\u5757\u88ab\u6267\u884c\u5e76\u6267\u884c\u4ee5\u4e0b \u6d88\u606f\u5c06\u5199\u5165\u8c03\u8bd5\u65e5\u5fd7\uff1a\u3002<samp>SObjectException caught: SObject row was retrieved via SOQL without querying the requested field: Merchandise__c.Total_Inventory__c<\/samp><\/li>\n\n\n\n<li>\u6700\u540e\u4e00\u4e2a catch \u5757\u88ab\u5ffd\u7565\uff0c\u56e0\u4e3a\u4e00\u4e2a catch \u5757\u5df2\u7ecf \u6267\u884c\u3002<\/li>\n<\/ol>\n\n\n\n<p>\u6700\u540e\u4e00\u4e2a catch \u5757\u5f88\u65b9\u4fbf\uff0c\u56e0\u4e3a\u5b83\u53ef\u4ee5\u6355\u83b7\u4efb\u4f55\u5f02\u5e38 \u7c7b\u578b\uff0c\u4ece\u800c\u6355\u83b7\u4e0a\u4e00\u4e2a\u672a\u6355\u83b7\u7684\u4efb\u4f55\u5f02\u5e38 catch \u5757\u3002\u5047\u8bbe\u6211\u4eec\u4fee\u6539\u4e86\u4e0a\u9762\u7684\u4ee3\u7801\u4ee5\u5bfc\u81f4 NullPointerException \u8981\u629b\u51fa\uff0c\u6b64\u5f02\u5e38\u5c06\u5728\u6700\u540e\u4e00\u4e2a catch \u5757\u4e2d\u6355\u83b7\u3002 \u6267\u884c\u6b64\u4fee\u6539\u540e\u7684\u793a\u4f8b\u3002\u4f60\u5c06\u770b\u5230\u4ee5\u4e0b\u8c03\u8bd5You&#8217;ll see the following debug \u6d88\u606f\uff1a\u3002<samp>Exception caught: Attempt to de-reference a null object<\/samp><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>try {\n    String s;\n    Boolean b = s.contains('abc'); \/\/ Causes a NullPointerException\n} catch(DmlException e) {\n    System.debug('DmlException caught: ' + e.getMessage());    \n} catch(SObjectException e) {\n    System.debug('SObjectException caught: ' + e.getMessage());    \n} catch(Exception e) {\n    System.debug('Exception caught: ' + e.getMessage());    \n}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u521b\u5efa\u81ea\u5b9a\u4e49\u4f8b\u5916<\/h2>\n\n\n\n<p>\u60a8\u4e0d\u80fd\u629b\u51fa\u5185\u7f6e\u7684 Apex \u5f02\u5e38\u3002\u4f60\u53ea\u80fd\u6293\u4f4f\u4ed6\u4eec\u3002\u4f46\u662f\u6709\u5b9a\u5236 \u5f02\u5e38\uff0c\u60a8\u53ef\u4ee5\u5728\u65b9\u6cd5\u4e2d\u629b\u51fa\u548c\u6355\u83b7\u5b83\u4eec\u3002\u81ea\u5b9a\u4e49\u5f02\u5e38\u4f7f\u60a8\u80fd\u591f \u6307\u5b9a\u8be6\u7ec6\u7684\u9519\u8bef\u6d88\u606f\uff0c\u5e76\u5728\u6355\u83b7\u4e2d\u5177\u6709\u66f4\u591a\u81ea\u5b9a\u4e49\u9519\u8bef\u5904\u7406 \u5757\u3002<\/p>\n\n\n\n<p>\u5f02\u5e38\u53ef\u4ee5\u662f\u9876\u7ea7\u7c7b\uff0c\u4e5f\u5c31\u662f\u8bf4\uff0c\u5b83\u4eec\u53ef\u4ee5\u5177\u6709\u6210\u5458\u53d8\u91cf\u3001\u65b9\u6cd5\u548c \u6784\u9020\u51fd\u6570\uff0c\u5b83\u4eec\u53ef\u4ee5\u5b9e\u73b0\u63a5\u53e3\uff0c\u7b49\u7b49\u3002<\/p>\n\n\n\n<p>\u82e5\u8981\u521b\u5efa\u81ea\u5b9a\u4e49\u5f02\u5e38\u7c7b\uff0c\u8bf7\u6269\u5c55\u5185\u7f6e\u7c7b\uff0c\u5e76\u786e\u4fdd\u7c7b\u540d\u4ee5\u5355\u8bcd\u7ed3\u5c3e\uff0c\u4f8b\u5982\u201cMyException\u201d\u6216 \u201cPurchaseException\u201d\u3002\u6240\u6709\u5f02\u5e38\u7c7b\u90fd\u6269\u5c55\u4e86\u7cfb\u7edf\u5b9a\u4e49\u7684\u57fa\u7c7b\uff0c\u56e0\u6b64\u7ee7\u627f\u4e86\u6240\u6709\u516c\u5171\u7c7b \u5f02\u5e38\u65b9\u6cd5\u3002<samp>Exception<\/samp><samp>Exception<\/samp><samp>Exception<\/samp>\u6b64\u793a\u4f8b\u5b9a\u4e49\u4e86\u4e00\u4e2a\u540d\u4e3a \u7684\u81ea\u5b9a\u4e49\u5f02\u5e38\u3002<\/p>\n\n\n\n<p><samp>MyException<\/samp><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public class <em>MyException<\/em> extends Exception {}<\/code><\/pre>\n\n\n\n<p>\u4e0e Java \u7c7b\u4e00\u6837\uff0c\u7528\u6237\u5b9a\u4e49\u7684\u5f02\u5e38\u7c7b\u578b\u53ef\u4ee5\u5f62\u6210\u7ee7\u627f\u6811\uff0c\u5e76\u6355\u83b7 \u5757\u53ef\u4ee5\u6355\u83b7\u6b64\u7ee7\u627f\u6811\u4e2d\u7684\u4efb\u4f55\u5bf9\u8c61\u3002\u4f8b\u5982\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public class ExceptionExample {\n    public virtual class BaseException extends Exception {}\n    public class OtherException extends BaseException {}\n\n    public static void testExtendedException() {\n        try {\n            Integer i=0;\n            \/\/ Your code here\n            if (i &lt; 5) throw new OtherException('This is bad');\n        } catch (BaseException e) {  \n            \/\/ This catches the OtherException\n            System.debug(e.getMessage());\n        }  \n    }\n}<\/code><\/pre>\n\n\n\n<p>\u60a8\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u51e0\u79cd\u65b9\u5f0f\u521b\u5efa\u5f02\u5e38\u5bf9\u8c61\uff0c\u7136\u540e\u53ef\u4ee5\u629b\u51fa\u8fd9\u4e9b\u5bf9\u8c61\u3002\u60a8\u53ef\u4ee5\u6784\u9020\u5f02\u5e38\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u6ca1\u6709 \u53c2\u6570\uff1a<code>new MyException();<\/code><\/li>\n\n\n\n<li>\u4f7f\u7528\u6307\u5b9a\u9519\u8bef\u7684\u5355\u4e2a String \u53c2\u6570 \u6d88\u606f\uff1a<code>new MyException('This is bad');<\/code><\/li>\n\n\n\n<li>\u4f7f\u7528\u5355\u4e2a Exception \u53c2\u6570\uff0c\u8be5\u53c2\u6570\u6307\u5b9a\u539f\u56e0\u5e76\u663e\u793a\u5728 \u4efb\u4f55\u5806\u6808 \u8ddf\u8e2a\uff1a<code>new MyException(e);<\/code><\/li>\n\n\n\n<li>\u540c\u65f6\u663e\u793a String \u9519\u8bef\u6d88\u606f\u548c\u94fe\u5f0f\u5f02\u5e38\u539f\u56e0 \u4efb\u4f55\u5806\u6808 \u8ddf\u8e2a\uff1a<code>new MyException('This is bad', e);<\/code><\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">\u91cd\u65b0\u5f15\u53d1\u5f02\u5e38\u548c\u5185\u90e8\u5f02\u5e38<\/h2>\n\n\n\n<p>\u5728 catch \u5757\u4e2d\u6355\u83b7\u5f02\u5e38\u540e\uff0c\u60a8\u53ef\u4ee5\u9009\u62e9\u91cd\u65b0\u629b\u51fa \u6355\u83b7\u5f02\u5e38\u53d8\u91cf\u3002\u5982\u679c\u65b9\u6cd5\u88ab\u53e6\u4e00\u4e2a\u65b9\u6cd5\u8c03\u7528\uff0c\u8fd9\u5c06\u975e\u5e38\u6709\u7528 \u5e76\u4e14\u60a8\u5e0c\u671b\u5c06\u5f02\u5e38\u7684\u5904\u7406\u59d4\u6258\u7ed9\u8c03\u7528\u65b9\u65b9\u6cd5\u3002\u60a8\u53ef\u4ee5 \u5c06\u6355\u83b7\u7684\u5f02\u5e38\u4f5c\u4e3a\u5185\u90e8\u5f02\u5e38\u91cd\u65b0\u629b\u51fa\uff0c\u5e76\u5177\u6709 main \u65b9\u6cd5\u6355\u83b7\u81ea\u5b9a\u4e49\u5f02\u5e38\u7c7b\u578b\u3002<\/p>\n\n\n\n<p>\u4e0b\u9762\u7684\u793a\u4f8b\u6f14\u793a\u5982\u4f55\u5c06\u5f02\u5e38\u91cd\u65b0\u5f15\u53d1\u4e3a\u5185\u90e8\u5f02\u5e38\u3002\u8fd9 example \u5b9a\u4e49\u4e86\u4e24\u4e2a\u81ea\u5b9a\u4e49\u5f02\u5e38\u548c \uff0c\u5e76\u751f\u6210\u4e00\u4e2a\u5806\u6808\u8ddf\u8e2a\uff0c\u5176\u4e2d\u5305\u542b\u6709\u5173\u4e24\u8005\u7684\u4fe1\u606f\u3002<samp>My1Exception<\/samp><samp>My2Exception<\/samp><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ Define two custom exceptions\npublic class My1Exception extends Exception {} \npublic class My2Exception extends Exception {} \n\ntry { \n    \/\/ Throw first exception\n    throw new My1Exception('First exception'); \n} catch (My1Exception e) { \n    \/\/ Throw second exception with the first \n    \/\/ exception variable as the inner exception\n    throw new My2Exception('Thrown with inner exception', e);\n}<\/code><\/pre>\n\n\n\n<p>\u8fd9\u662f\u8fd0\u884c\u4e0a\u8ff0\u4ee3\u7801\u540e\u751f\u6210\u7684\u5806\u6808\u8ddf\u8e2a\u7684\u6837\u5b50\uff1a<\/p>\n\n\n\n<p><samp>15:52:21:073 EXCEPTION_THROWN [7]|My1Exception: First exception<\/samp><\/p>\n\n\n\n<p><samp>15:52:21:077 EXCEPTION_THROWN [11]|My2Exception: Throw with inner exception<\/samp><\/p>\n\n\n\n<p><samp>15:52:21:000 FATAL_ERROR AnonymousBlock: line 11, column 1<\/samp><\/p>\n\n\n\n<p><samp>15:52:21:000 FATAL_ERROR Caused by<\/samp><\/p>\n\n\n\n<p><samp>15:52:21:000 FATAL_ERROR AnonymousBlock: line 7, column 1<\/samp><\/p>\n\n\n\n<p>\u4e0b\u4e00\u8282\u4e2d\u7684\u793a\u4f8b\u6f14\u793a\u5982\u4f55\u4f7f\u7528 \u5f02\u5e38\u3002<samp>getCause<\/samp><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u5185\u90e8\u5f02\u5e38\u793a\u4f8b<\/h2>\n\n\n\n<p>\u73b0\u5728\uff0c\u60a8\u5df2\u7ecf\u4e86\u89e3\u4e86\u5982\u4f55\u521b\u5efa\u81ea\u5b9a\u4e49\u5f02\u5e38\u7c7b\u4ee5\u53ca\u5982\u4f55\u6784\u9020 \u60a8\u7684\u5f02\u5e38\u5bf9\u8c61\uff0c\u8ba9\u6211\u4eec\u521b\u5efa\u5e76\u8fd0\u884c\u4e00\u4e2a\u793a\u4f8b\u6765\u6f14\u793a \u81ea\u5b9a\u4e49\u5f02\u5e38\u7684\u6709\u7528\u6027\u3002<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u5728\u5f00\u53d1\u8005\u63a7\u5236\u53f0\u4e2d\uff0c\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a \u5185\u5bb9\u3002<samp>MerchandiseException<\/samp><code>public class MerchandiseException extends Exception { }<\/code>\u4f60\u4f1a \u5728\u521b\u5efa\u7684\u7b2c\u4e8c\u4e2a\u7c7b\u4e2d\u4f7f\u7528\u6b64\u5f02\u5e38\u7c7b\u3002\u5927\u62ec\u53f7 \u6700\u540e\u5c06\u5f02\u5e38\u7c7b\u7684\u4e3b\u4f53\u62ec\u8d77\u6765\uff0c\u6211\u4eec\u5c06\u5176\u7559\u7a7a\uff0c\u56e0\u4e3a \u6211\u4eec\u5f97\u5230\u4e86\u4e00\u4e9b\u514d\u8d39\u4ee3\u7801\u2014\u2014\u6211\u4eec\u7684\u7c7b\u7ee7\u627f\u4e86\u6240\u6709\u7684\u6784\u9020\u51fd\u6570\u548c\u516c\u5171 \u5f02\u5e38\u65b9\u6cd5\uff0c\u4f8b\u5982 \u6765\u81ea\u5185\u7f6e\u7c7b\u3002<samp>getMessage<\/samp><samp>Exception<\/samp><\/li>\n\n\n\n<li>\u63a5\u4e0b\u6765\uff0c\u521b\u5efa\u540d\u4e3a .<samp>MerchandiseUtility<\/samp><code>public class MerchandiseUtility { public static void mainProcessing() { try { insertMerchandise(); } catch(MerchandiseException me) { System.debug('Message: ' + me.getMessage()); System.debug('Cause: ' + me.getCause()); System.debug('Line number: ' + me.getLineNumber()); System.debug('Stack trace: ' + me.getStackTraceString()); } } public static void insertMerchandise() { try { \/\/ Insert merchandise without required fields Merchandise__c m = new Merchandise__c(); insert m; } catch(DmlException e) { \/\/ Something happened that prevents the insertion \/\/ of Employee custom objects, so throw a more \/\/ specific exception. throw new MerchandiseException( 'Merchandise item could not be inserted.', e); } } }<\/code>\u8fd9 class \u5305\u542b\u8c03\u7528 . \u540e\u8005\u901a\u8fc7\u63d2\u5165\u4e0d\u9700\u8981\u7684\u5546\u54c1\u6765\u5bfc\u81f4\u5f02\u5e38 \u9886\u57df\u3002catch \u5757\u6355\u83b7\u6b64\u5f02\u5e38\u5e76\u629b\u51fa\u4e00\u4e2a\u65b0\u5f02\u5e38\uff0c\u5373 \u60a8\u4e4b\u524d\u521b\u5efa\u7684\u81ea\u5b9a\u4e49 MerchandiseException\u3002\u8bf7\u6ce8\u610f\uff0c\u6211\u4eec\u8c03\u7528\u4e86 \u63a5\u53d7\u4e24\u4e2a\u53c2\u6570\u7684\u5f02\u5e38\u7684\u6784\u9020\u51fd\u6570\uff1a\u9519\u8bef\u6d88\u606f\u548c \u539f\u59cb\u5f02\u5e38\u5bf9\u8c61\u3002\u60a8\u53ef\u80fd\u60f3\u77e5\u9053\u4e3a\u4ec0\u4e48\u6211\u4eec\u8981\u4f20\u9012\u539f\u59cb\u7248\u672c \u4f8b\u5916\uff1f\u56e0\u4e3a\u5b83\u662f\u6709\u7528\u7684\u4fe1\u606f &#8211; \u5f53 MerchandiseException \u5728\u7b2c\u4e00\u79cd\u65b9\u6cd5\u4e2d\u6355\u83b7\uff0c\u539f\u59cb\u5f02\u5e38\uff08\u79f0\u4e3a\u5185\u90e8 exception\uff09 \u786e\u5b9e\u662f\u5bfc\u81f4\u6b64\u5f02\u5e38\u7684\u539f\u56e0\uff0c\u56e0\u4e3a\u5b83\u53d1\u751f\u5728 MerchandiseException\u3002<samp>mainProcessing<\/samp><samp>insertMerchandise<\/samp><samp>mainProcessing<\/samp><\/li>\n\n\n\n<li>\u73b0\u5728\u8ba9\u6211\u4eec\u770b\u770b\u6240\u6709\u8fd9\u4e9b\u7684\u5b9e\u9645\u6548\u679c\uff0c\u4ee5\u4fbf\u66f4\u597d\u5730\u7406\u89e3\u3002\u6267\u884c \u4ee5\u540e\uff1a<code>MerchandiseUtility.mainProcessing();<\/code><\/li>\n\n\n\n<li>\u68c0\u67e5\u8c03\u8bd5\u65e5\u5fd7\u8f93\u51fa\u3002\u60a8\u5e94\u8be5\u770b\u5230\u7c7b\u4f3c\u4e8e \u4ee5\u540e\uff1a<samp>18:12:34:928 USER_DEBUG [6]|DEBUG|Message: Merchandise item could not be inserted.<\/samp><samp>18:12:34:929 USER_DEBUG [7]|DEBUG|Cause: System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [Description, Price, Total Inventory]: [Description, Price, Total Inventory]<\/samp><samp>18:12:34:929 USER_DEBUG [8]|DEBUG|Line number: 22<\/samp><samp>18:12:34:930 USER_DEBUG [9]|DEBUG|Stack trace: Class.EmployeeUtilityClass.insertMerchandise: line 22, column 1<\/samp>\u4e00\u4e9b\u611f\u5174\u8da3\u7684\u9879\u76ee\uff1a\n<ul class=\"wp-block-list\">\n<li>MerchandiseException \u7684\u539f\u56e0\u662f DmlException\u3002\u60a8\u53ef\u4ee5\u770b\u5230 DmlException \u6d88\u606f\u8fd8\u6307\u51fa\u5fc5\u586b\u5b57\u6bb5\u662f \u5931\u8e2a\u3002<\/li>\n\n\n\n<li>\u5806\u6808\u8ddf\u8e2a\u662f\u7b2c 22 \u884c\uff0c\u8fd9\u662f\u7b2c\u4e8c\u6b21\u51fa\u73b0\u5f02\u5e38 \u6254\u3002\u5b83\u5bf9\u5e94\u4e8e throw \u8bed\u53e5 MerchandiseException\u3002<code>throw new MerchandiseException('Merchandise item could not be inserted.', e);<\/code><\/li>\n<\/ul>\n<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>\u5f02\u5e38\u4f1a\u8bb0\u5f55\u4e2d\u65ad\u6b63\u5e38\u4ee3\u7801\u6d41\u7684\u9519\u8bef\u548c\u5176\u4ed6\u4e8b\u4ef6 \u6267\u884c\u3002 \u8bed\u53e5\u7528\u4e8e \u751f\u6210\u5f02\u5e38\uff0c\u800c \u3001 \u548c \u8bed\u53e5\u7528\u4e8e\u4ece\u5f02\u5e38\u4e2d\u6b63\u5e38\u6062\u590d\u3002throwtrycatchfinally<\/p>\n<p>\u6709\u8bb8\u591a\u65b9\u6cd5\u53ef\u4ee5\u5904\u7406\u4ee3\u7801\u4e2d\u7684\u9519\u8bef\uff0c\u5305\u62ec\u4f7f\u7528\u65ad\u8a00\uff08\u5982\u8c03\u7528\uff09\u6216\u8fd4\u56de\u9519\u8bef \u4ee3\u7801\u6216\u5e03\u5c14\u503c\uff0c\u90a3\u4e48\u4e3a\u4ec0\u4e48\u8981\u4f7f\u7528\u5f02\u5e38\u5462\uff1f\u4f7f\u7528\u5f02\u5e38\u7684\u4f18\u70b9\u662f \u5b83\u4eec\u7b80\u5316\u4e86\u9519\u8bef\u5904\u7406\u3002\u5f02\u5e38\u4ece\u88ab\u8c03\u7528\u7684\u65b9\u6cd5\u5192\u6ce1\u5230 caller\uff0c\u6839\u636e\u9700\u8981\u8bbe\u7f6e\u591a\u4e2a\u7ea7\u522b\uff0c\u76f4\u5230\u627e\u5230\u5904\u7406\u9519\u8bef\u7684\u8bed\u53e5\u3002\u8fd9\u79cd\u5192\u6ce1\u7f13\u89e3\u4e86 \u60a8\u4e0d\u5fc5\u5728\u6bcf\u4e2a\u65b9\u6cd5\u4e2d\u7f16\u5199\u9519\u8bef\u5904\u7406\u4ee3\u7801\u3002\u6b64\u5916\uff0c\u901a\u8fc7\u4f7f\u7528\u8bed\u53e5\uff0c\u60a8\u53ef\u4ee5\u5728\u4e00\u4e2a\u4f4d\u7f6e\u8fdb\u884c\u6062\u590d \u4ece\u5f02\u5e38\uff0c\u4f8b\u5982\u91cd\u7f6e\u53d8\u91cf\u548c\u5220\u9664\u6570\u636e\u3002System.assertcatchfinally<\/p>\n","protected":false},"author":1,"featured_media":3776,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[199],"tags":[229],"class_list":["post-3775","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-apex-","tag-apex-"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v21.7 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Apex \u4e2d\u7684\u5f02\u5e38 - \u767d\u767d\u968f\u7b14<\/title>\n<meta name=\"description\" content=\"\u5f02\u5e38\u4f1a\u8bb0\u5f55\u4e2d\u65ad\u6b63\u5e38\u4ee3\u7801\u6d41\u7684\u9519\u8bef\u548c\u5176\u4ed6\u4e8b\u4ef6 \u6267\u884c\u3002 \u8bed\u53e5\u7528\u4e8e \u751f\u6210\u5f02\u5e38\uff0c\u800c \u3001 \u548c \u8bed\u53e5\u7528\u4e8e\u4ece\u5f02\u5e38\u4e2d\u6b63\u5e38\u6062\u590d\u3002throwtrycatchfinally\u6709\u8bb8\u591a\u65b9\u6cd5\u53ef\u4ee5\u5904\u7406\u4ee3\u7801\u4e2d\u7684\u9519\u8bef\uff0c\u5305\u62ec\u4f7f\u7528\u65ad\u8a00\uff08\u5982\u8c03\u7528\uff09\u6216\u8fd4\u56de\u9519\u8bef \u4ee3\u7801\u6216\u5e03\u5c14\u503c\uff0c\u90a3\u4e48\u4e3a\u4ec0\u4e48\u8981\u4f7f\u7528\u5f02\u5e38\u5462\uff1f\u4f7f\u7528\u5f02\u5e38\u7684\u4f18\u70b9\u662f \u5b83\u4eec\u7b80\u5316\u4e86\u9519\u8bef\u5904\u7406\u3002\u5f02\u5e38\u4ece\u88ab\u8c03\u7528\u7684\u65b9\u6cd5\u5192\u6ce1\u5230 caller\uff0c\u6839\u636e\u9700\u8981\u8bbe\u7f6e\u591a\u4e2a\u7ea7\u522b\uff0c\u76f4\u5230\u627e\u5230\u5904\u7406\u9519\u8bef\u7684\u8bed\u53e5\u3002\u8fd9\u79cd\u5192\u6ce1\u7f13\u89e3\u4e86 \u60a8\u4e0d\u5fc5\u5728\u6bcf\u4e2a\u65b9\u6cd5\u4e2d\u7f16\u5199\u9519\u8bef\u5904\u7406\u4ee3\u7801\u3002\u6b64\u5916\uff0c\u901a\u8fc7\u4f7f\u7528\u8bed\u53e5\uff0c\u60a8\u53ef\u4ee5\u5728\u4e00\u4e2a\u4f4d\u7f6e\u8fdb\u884c\u6062\u590d \u4ece\u5f02\u5e38\uff0c\u4f8b\u5982\u91cd\u7f6e\u53d8\u91cf\u548c\u5220\u9664\u6570\u636e\u3002System.assertcatchfinally\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"http:\/\/www.ponybai.com\/?p=3775\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Apex \u4e2d\u7684\u5f02\u5e38 - \u767d\u767d\u968f\u7b14\" \/>\n<meta property=\"og:description\" content=\"\u5f02\u5e38\u4f1a\u8bb0\u5f55\u4e2d\u65ad\u6b63\u5e38\u4ee3\u7801\u6d41\u7684\u9519\u8bef\u548c\u5176\u4ed6\u4e8b\u4ef6 \u6267\u884c\u3002 \u8bed\u53e5\u7528\u4e8e \u751f\u6210\u5f02\u5e38\uff0c\u800c \u3001 \u548c \u8bed\u53e5\u7528\u4e8e\u4ece\u5f02\u5e38\u4e2d\u6b63\u5e38\u6062\u590d\u3002throwtrycatchfinally\u6709\u8bb8\u591a\u65b9\u6cd5\u53ef\u4ee5\u5904\u7406\u4ee3\u7801\u4e2d\u7684\u9519\u8bef\uff0c\u5305\u62ec\u4f7f\u7528\u65ad\u8a00\uff08\u5982\u8c03\u7528\uff09\u6216\u8fd4\u56de\u9519\u8bef \u4ee3\u7801\u6216\u5e03\u5c14\u503c\uff0c\u90a3\u4e48\u4e3a\u4ec0\u4e48\u8981\u4f7f\u7528\u5f02\u5e38\u5462\uff1f\u4f7f\u7528\u5f02\u5e38\u7684\u4f18\u70b9\u662f \u5b83\u4eec\u7b80\u5316\u4e86\u9519\u8bef\u5904\u7406\u3002\u5f02\u5e38\u4ece\u88ab\u8c03\u7528\u7684\u65b9\u6cd5\u5192\u6ce1\u5230 caller\uff0c\u6839\u636e\u9700\u8981\u8bbe\u7f6e\u591a\u4e2a\u7ea7\u522b\uff0c\u76f4\u5230\u627e\u5230\u5904\u7406\u9519\u8bef\u7684\u8bed\u53e5\u3002\u8fd9\u79cd\u5192\u6ce1\u7f13\u89e3\u4e86 \u60a8\u4e0d\u5fc5\u5728\u6bcf\u4e2a\u65b9\u6cd5\u4e2d\u7f16\u5199\u9519\u8bef\u5904\u7406\u4ee3\u7801\u3002\u6b64\u5916\uff0c\u901a\u8fc7\u4f7f\u7528\u8bed\u53e5\uff0c\u60a8\u53ef\u4ee5\u5728\u4e00\u4e2a\u4f4d\u7f6e\u8fdb\u884c\u6062\u590d \u4ece\u5f02\u5e38\uff0c\u4f8b\u5982\u91cd\u7f6e\u53d8\u91cf\u548c\u5220\u9664\u6570\u636e\u3002System.assertcatchfinally\" \/>\n<meta property=\"og:url\" content=\"http:\/\/www.ponybai.com\/?p=3775\" \/>\n<meta property=\"og:site_name\" content=\"\u767d\u767d\u968f\u7b14\" \/>\n<meta property=\"article:published_time\" content=\"2023-12-29T13:53:58+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-11-28T14:01:33+00:00\" \/>\n<meta property=\"og:image\" content=\"http:\/\/www.ponybai.com\/wp-content\/uploads\/2023\/11\/try.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1188\" \/>\n\t<meta property=\"og:image:height\" content=\"585\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"ponybai\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"ponybai\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"5 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"http:\/\/www.ponybai.com\/?p=3775#article\",\"isPartOf\":{\"@id\":\"http:\/\/www.ponybai.com\/?p=3775\"},\"author\":{\"name\":\"ponybai\",\"@id\":\"http:\/\/www.ponybai.com\/#\/schema\/person\/935c7592c850c65e1e5eba4530dbf883\"},\"headline\":\"Apex \u4e2d\u7684\u5f02\u5e38\",\"datePublished\":\"2023-12-29T13:53:58+00:00\",\"dateModified\":\"2023-11-28T14:01:33+00:00\",\"mainEntityOfPage\":{\"@id\":\"http:\/\/www.ponybai.com\/?p=3775\"},\"wordCount\":563,\"publisher\":{\"@id\":\"http:\/\/www.ponybai.com\/#\/schema\/person\/935c7592c850c65e1e5eba4530dbf883\"},\"keywords\":[\"Apex \u4e2d\u7684\u5f02\u5e38\"],\"articleSection\":[\"Apex \u5f00\u53d1\u4eba\u5458\u6307\u5357\"],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"WebPage\",\"@id\":\"http:\/\/www.ponybai.com\/?p=3775\",\"url\":\"http:\/\/www.ponybai.com\/?p=3775\",\"name\":\"Apex \u4e2d\u7684\u5f02\u5e38 - \u767d\u767d\u968f\u7b14\",\"isPartOf\":{\"@id\":\"http:\/\/www.ponybai.com\/#website\"},\"datePublished\":\"2023-12-29T13:53:58+00:00\",\"dateModified\":\"2023-11-28T14:01:33+00:00\",\"description\":\"\u5f02\u5e38\u4f1a\u8bb0\u5f55\u4e2d\u65ad\u6b63\u5e38\u4ee3\u7801\u6d41\u7684\u9519\u8bef\u548c\u5176\u4ed6\u4e8b\u4ef6 \u6267\u884c\u3002 \u8bed\u53e5\u7528\u4e8e \u751f\u6210\u5f02\u5e38\uff0c\u800c \u3001 \u548c \u8bed\u53e5\u7528\u4e8e\u4ece\u5f02\u5e38\u4e2d\u6b63\u5e38\u6062\u590d\u3002throwtrycatchfinally\u6709\u8bb8\u591a\u65b9\u6cd5\u53ef\u4ee5\u5904\u7406\u4ee3\u7801\u4e2d\u7684\u9519\u8bef\uff0c\u5305\u62ec\u4f7f\u7528\u65ad\u8a00\uff08\u5982\u8c03\u7528\uff09\u6216\u8fd4\u56de\u9519\u8bef \u4ee3\u7801\u6216\u5e03\u5c14\u503c\uff0c\u90a3\u4e48\u4e3a\u4ec0\u4e48\u8981\u4f7f\u7528\u5f02\u5e38\u5462\uff1f\u4f7f\u7528\u5f02\u5e38\u7684\u4f18\u70b9\u662f \u5b83\u4eec\u7b80\u5316\u4e86\u9519\u8bef\u5904\u7406\u3002\u5f02\u5e38\u4ece\u88ab\u8c03\u7528\u7684\u65b9\u6cd5\u5192\u6ce1\u5230 caller\uff0c\u6839\u636e\u9700\u8981\u8bbe\u7f6e\u591a\u4e2a\u7ea7\u522b\uff0c\u76f4\u5230\u627e\u5230\u5904\u7406\u9519\u8bef\u7684\u8bed\u53e5\u3002\u8fd9\u79cd\u5192\u6ce1\u7f13\u89e3\u4e86 \u60a8\u4e0d\u5fc5\u5728\u6bcf\u4e2a\u65b9\u6cd5\u4e2d\u7f16\u5199\u9519\u8bef\u5904\u7406\u4ee3\u7801\u3002\u6b64\u5916\uff0c\u901a\u8fc7\u4f7f\u7528\u8bed\u53e5\uff0c\u60a8\u53ef\u4ee5\u5728\u4e00\u4e2a\u4f4d\u7f6e\u8fdb\u884c\u6062\u590d \u4ece\u5f02\u5e38\uff0c\u4f8b\u5982\u91cd\u7f6e\u53d8\u91cf\u548c\u5220\u9664\u6570\u636e\u3002System.assertcatchfinally\",\"breadcrumb\":{\"@id\":\"http:\/\/www.ponybai.com\/?p=3775#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"http:\/\/www.ponybai.com\/?p=3775\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"http:\/\/www.ponybai.com\/?p=3775#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"http:\/\/www.ponybai.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Apex \u4e2d\u7684\u5f02\u5e38\"}]},{\"@type\":\"WebSite\",\"@id\":\"http:\/\/www.ponybai.com\/#website\",\"url\":\"http:\/\/www.ponybai.com\/\",\"name\":\"\u767d\u767d\u968f\u7b14\",\"description\":\"Salesforce\u5b98\u65b9\u8bb2\u5e08\",\"publisher\":{\"@id\":\"http:\/\/www.ponybai.com\/#\/schema\/person\/935c7592c850c65e1e5eba4530dbf883\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"http:\/\/www.ponybai.com\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"http:\/\/www.ponybai.com\/#\/schema\/person\/935c7592c850c65e1e5eba4530dbf883\",\"name\":\"ponybai\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"http:\/\/www.ponybai.com\/#\/schema\/person\/image\/\",\"url\":\"http:\/\/www.ponybai.com\/wp-content\/uploads\/2025\/03\/cropped-Ihsans-WeChatQR.jpg\",\"contentUrl\":\"http:\/\/www.ponybai.com\/wp-content\/uploads\/2025\/03\/cropped-Ihsans-WeChatQR.jpg\",\"width\":248,\"height\":248,\"caption\":\"ponybai\"},\"logo\":{\"@id\":\"http:\/\/www.ponybai.com\/#\/schema\/person\/image\/\"},\"sameAs\":[\"http:\/\/121.37.188.161\"],\"url\":\"http:\/\/www.ponybai.com\/?author=1\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Apex \u4e2d\u7684\u5f02\u5e38 - \u767d\u767d\u968f\u7b14","description":"\u5f02\u5e38\u4f1a\u8bb0\u5f55\u4e2d\u65ad\u6b63\u5e38\u4ee3\u7801\u6d41\u7684\u9519\u8bef\u548c\u5176\u4ed6\u4e8b\u4ef6 \u6267\u884c\u3002 \u8bed\u53e5\u7528\u4e8e \u751f\u6210\u5f02\u5e38\uff0c\u800c \u3001 \u548c \u8bed\u53e5\u7528\u4e8e\u4ece\u5f02\u5e38\u4e2d\u6b63\u5e38\u6062\u590d\u3002throwtrycatchfinally\u6709\u8bb8\u591a\u65b9\u6cd5\u53ef\u4ee5\u5904\u7406\u4ee3\u7801\u4e2d\u7684\u9519\u8bef\uff0c\u5305\u62ec\u4f7f\u7528\u65ad\u8a00\uff08\u5982\u8c03\u7528\uff09\u6216\u8fd4\u56de\u9519\u8bef \u4ee3\u7801\u6216\u5e03\u5c14\u503c\uff0c\u90a3\u4e48\u4e3a\u4ec0\u4e48\u8981\u4f7f\u7528\u5f02\u5e38\u5462\uff1f\u4f7f\u7528\u5f02\u5e38\u7684\u4f18\u70b9\u662f \u5b83\u4eec\u7b80\u5316\u4e86\u9519\u8bef\u5904\u7406\u3002\u5f02\u5e38\u4ece\u88ab\u8c03\u7528\u7684\u65b9\u6cd5\u5192\u6ce1\u5230 caller\uff0c\u6839\u636e\u9700\u8981\u8bbe\u7f6e\u591a\u4e2a\u7ea7\u522b\uff0c\u76f4\u5230\u627e\u5230\u5904\u7406\u9519\u8bef\u7684\u8bed\u53e5\u3002\u8fd9\u79cd\u5192\u6ce1\u7f13\u89e3\u4e86 \u60a8\u4e0d\u5fc5\u5728\u6bcf\u4e2a\u65b9\u6cd5\u4e2d\u7f16\u5199\u9519\u8bef\u5904\u7406\u4ee3\u7801\u3002\u6b64\u5916\uff0c\u901a\u8fc7\u4f7f\u7528\u8bed\u53e5\uff0c\u60a8\u53ef\u4ee5\u5728\u4e00\u4e2a\u4f4d\u7f6e\u8fdb\u884c\u6062\u590d \u4ece\u5f02\u5e38\uff0c\u4f8b\u5982\u91cd\u7f6e\u53d8\u91cf\u548c\u5220\u9664\u6570\u636e\u3002System.assertcatchfinally","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:\/\/www.ponybai.com\/?p=3775","og_locale":"zh_CN","og_type":"article","og_title":"Apex \u4e2d\u7684\u5f02\u5e38 - \u767d\u767d\u968f\u7b14","og_description":"\u5f02\u5e38\u4f1a\u8bb0\u5f55\u4e2d\u65ad\u6b63\u5e38\u4ee3\u7801\u6d41\u7684\u9519\u8bef\u548c\u5176\u4ed6\u4e8b\u4ef6 \u6267\u884c\u3002 \u8bed\u53e5\u7528\u4e8e \u751f\u6210\u5f02\u5e38\uff0c\u800c \u3001 \u548c \u8bed\u53e5\u7528\u4e8e\u4ece\u5f02\u5e38\u4e2d\u6b63\u5e38\u6062\u590d\u3002throwtrycatchfinally\u6709\u8bb8\u591a\u65b9\u6cd5\u53ef\u4ee5\u5904\u7406\u4ee3\u7801\u4e2d\u7684\u9519\u8bef\uff0c\u5305\u62ec\u4f7f\u7528\u65ad\u8a00\uff08\u5982\u8c03\u7528\uff09\u6216\u8fd4\u56de\u9519\u8bef \u4ee3\u7801\u6216\u5e03\u5c14\u503c\uff0c\u90a3\u4e48\u4e3a\u4ec0\u4e48\u8981\u4f7f\u7528\u5f02\u5e38\u5462\uff1f\u4f7f\u7528\u5f02\u5e38\u7684\u4f18\u70b9\u662f \u5b83\u4eec\u7b80\u5316\u4e86\u9519\u8bef\u5904\u7406\u3002\u5f02\u5e38\u4ece\u88ab\u8c03\u7528\u7684\u65b9\u6cd5\u5192\u6ce1\u5230 caller\uff0c\u6839\u636e\u9700\u8981\u8bbe\u7f6e\u591a\u4e2a\u7ea7\u522b\uff0c\u76f4\u5230\u627e\u5230\u5904\u7406\u9519\u8bef\u7684\u8bed\u53e5\u3002\u8fd9\u79cd\u5192\u6ce1\u7f13\u89e3\u4e86 \u60a8\u4e0d\u5fc5\u5728\u6bcf\u4e2a\u65b9\u6cd5\u4e2d\u7f16\u5199\u9519\u8bef\u5904\u7406\u4ee3\u7801\u3002\u6b64\u5916\uff0c\u901a\u8fc7\u4f7f\u7528\u8bed\u53e5\uff0c\u60a8\u53ef\u4ee5\u5728\u4e00\u4e2a\u4f4d\u7f6e\u8fdb\u884c\u6062\u590d \u4ece\u5f02\u5e38\uff0c\u4f8b\u5982\u91cd\u7f6e\u53d8\u91cf\u548c\u5220\u9664\u6570\u636e\u3002System.assertcatchfinally","og_url":"http:\/\/www.ponybai.com\/?p=3775","og_site_name":"\u767d\u767d\u968f\u7b14","article_published_time":"2023-12-29T13:53:58+00:00","article_modified_time":"2023-11-28T14:01:33+00:00","og_image":[{"width":1188,"height":585,"url":"http:\/\/www.ponybai.com\/wp-content\/uploads\/2023\/11\/try.png","type":"image\/png"}],"author":"ponybai","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"ponybai","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"5 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"http:\/\/www.ponybai.com\/?p=3775#article","isPartOf":{"@id":"http:\/\/www.ponybai.com\/?p=3775"},"author":{"name":"ponybai","@id":"http:\/\/www.ponybai.com\/#\/schema\/person\/935c7592c850c65e1e5eba4530dbf883"},"headline":"Apex \u4e2d\u7684\u5f02\u5e38","datePublished":"2023-12-29T13:53:58+00:00","dateModified":"2023-11-28T14:01:33+00:00","mainEntityOfPage":{"@id":"http:\/\/www.ponybai.com\/?p=3775"},"wordCount":563,"publisher":{"@id":"http:\/\/www.ponybai.com\/#\/schema\/person\/935c7592c850c65e1e5eba4530dbf883"},"keywords":["Apex \u4e2d\u7684\u5f02\u5e38"],"articleSection":["Apex \u5f00\u53d1\u4eba\u5458\u6307\u5357"],"inLanguage":"zh-Hans"},{"@type":"WebPage","@id":"http:\/\/www.ponybai.com\/?p=3775","url":"http:\/\/www.ponybai.com\/?p=3775","name":"Apex \u4e2d\u7684\u5f02\u5e38 - \u767d\u767d\u968f\u7b14","isPartOf":{"@id":"http:\/\/www.ponybai.com\/#website"},"datePublished":"2023-12-29T13:53:58+00:00","dateModified":"2023-11-28T14:01:33+00:00","description":"\u5f02\u5e38\u4f1a\u8bb0\u5f55\u4e2d\u65ad\u6b63\u5e38\u4ee3\u7801\u6d41\u7684\u9519\u8bef\u548c\u5176\u4ed6\u4e8b\u4ef6 \u6267\u884c\u3002 \u8bed\u53e5\u7528\u4e8e \u751f\u6210\u5f02\u5e38\uff0c\u800c \u3001 \u548c \u8bed\u53e5\u7528\u4e8e\u4ece\u5f02\u5e38\u4e2d\u6b63\u5e38\u6062\u590d\u3002throwtrycatchfinally\u6709\u8bb8\u591a\u65b9\u6cd5\u53ef\u4ee5\u5904\u7406\u4ee3\u7801\u4e2d\u7684\u9519\u8bef\uff0c\u5305\u62ec\u4f7f\u7528\u65ad\u8a00\uff08\u5982\u8c03\u7528\uff09\u6216\u8fd4\u56de\u9519\u8bef \u4ee3\u7801\u6216\u5e03\u5c14\u503c\uff0c\u90a3\u4e48\u4e3a\u4ec0\u4e48\u8981\u4f7f\u7528\u5f02\u5e38\u5462\uff1f\u4f7f\u7528\u5f02\u5e38\u7684\u4f18\u70b9\u662f \u5b83\u4eec\u7b80\u5316\u4e86\u9519\u8bef\u5904\u7406\u3002\u5f02\u5e38\u4ece\u88ab\u8c03\u7528\u7684\u65b9\u6cd5\u5192\u6ce1\u5230 caller\uff0c\u6839\u636e\u9700\u8981\u8bbe\u7f6e\u591a\u4e2a\u7ea7\u522b\uff0c\u76f4\u5230\u627e\u5230\u5904\u7406\u9519\u8bef\u7684\u8bed\u53e5\u3002\u8fd9\u79cd\u5192\u6ce1\u7f13\u89e3\u4e86 \u60a8\u4e0d\u5fc5\u5728\u6bcf\u4e2a\u65b9\u6cd5\u4e2d\u7f16\u5199\u9519\u8bef\u5904\u7406\u4ee3\u7801\u3002\u6b64\u5916\uff0c\u901a\u8fc7\u4f7f\u7528\u8bed\u53e5\uff0c\u60a8\u53ef\u4ee5\u5728\u4e00\u4e2a\u4f4d\u7f6e\u8fdb\u884c\u6062\u590d \u4ece\u5f02\u5e38\uff0c\u4f8b\u5982\u91cd\u7f6e\u53d8\u91cf\u548c\u5220\u9664\u6570\u636e\u3002System.assertcatchfinally","breadcrumb":{"@id":"http:\/\/www.ponybai.com\/?p=3775#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["http:\/\/www.ponybai.com\/?p=3775"]}]},{"@type":"BreadcrumbList","@id":"http:\/\/www.ponybai.com\/?p=3775#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"http:\/\/www.ponybai.com\/"},{"@type":"ListItem","position":2,"name":"Apex \u4e2d\u7684\u5f02\u5e38"}]},{"@type":"WebSite","@id":"http:\/\/www.ponybai.com\/#website","url":"http:\/\/www.ponybai.com\/","name":"\u767d\u767d\u968f\u7b14","description":"Salesforce\u5b98\u65b9\u8bb2\u5e08","publisher":{"@id":"http:\/\/www.ponybai.com\/#\/schema\/person\/935c7592c850c65e1e5eba4530dbf883"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"http:\/\/www.ponybai.com\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":["Person","Organization"],"@id":"http:\/\/www.ponybai.com\/#\/schema\/person\/935c7592c850c65e1e5eba4530dbf883","name":"ponybai","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"http:\/\/www.ponybai.com\/#\/schema\/person\/image\/","url":"http:\/\/www.ponybai.com\/wp-content\/uploads\/2025\/03\/cropped-Ihsans-WeChatQR.jpg","contentUrl":"http:\/\/www.ponybai.com\/wp-content\/uploads\/2025\/03\/cropped-Ihsans-WeChatQR.jpg","width":248,"height":248,"caption":"ponybai"},"logo":{"@id":"http:\/\/www.ponybai.com\/#\/schema\/person\/image\/"},"sameAs":["http:\/\/121.37.188.161"],"url":"http:\/\/www.ponybai.com\/?author=1"}]}},"_links":{"self":[{"href":"http:\/\/www.ponybai.com\/index.php?rest_route=\/wp\/v2\/posts\/3775","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.ponybai.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.ponybai.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.ponybai.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.ponybai.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3775"}],"version-history":[{"count":1,"href":"http:\/\/www.ponybai.com\/index.php?rest_route=\/wp\/v2\/posts\/3775\/revisions"}],"predecessor-version":[{"id":3777,"href":"http:\/\/www.ponybai.com\/index.php?rest_route=\/wp\/v2\/posts\/3775\/revisions\/3777"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/www.ponybai.com\/index.php?rest_route=\/wp\/v2\/media\/3776"}],"wp:attachment":[{"href":"http:\/\/www.ponybai.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3775"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.ponybai.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3775"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.ponybai.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3775"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}