{"id":1011,"date":"2021-04-10T10:05:16","date_gmt":"2021-04-10T10:05:16","guid":{"rendered":"https:\/\/phptutorial.net\/?page_id=1011"},"modified":"2021-05-21T13:07:24","modified_gmt":"2021-05-21T13:07:24","slug":"php-try-catch-finally","status":"publish","type":"page","link":"https:\/\/www.phptutorial.net\/php-oop\/php-try-catch-finally\/","title":{"rendered":"PHP try&#8230;catch&#8230;finally"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you&#8217;ll learn how to use the PHP <code>try...catch...finally<\/code> statement to handle exceptions and clean up the resources.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='introduction-to-the-php-try-catch-finally-statement'>Introduction to the PHP try&#8230;catch&#8230;finally statement <a href=\"#introduction-to-the-php-try-catch-finally-statement\" class=\"anchor\" id=\"introduction-to-the-php-try-catch-finally-statement\" title=\"Anchor for Introduction to the PHP try...catch...finally statement\">#<\/a><\/h2>\n\n\n\n<p>The <a href=\"https:\/\/phptutorial.net\/php-oop\/php-try-catch\/\">try&#8230;catch<\/a> statement allows you to handle exceptions. When an exception occurs in the <code>try<\/code> block, the execution jumps to the <code>catch<\/code> block. In the <code>catch<\/code> block, you can place the code that handles the exception. <\/p>\n\n\n\n<p>The following example uses the try&#8230;catch block to <a href=\"https:\/\/phptutorial.net\/php-tutorial\/php-csv\/\">read a CSV file<\/a> into an <a href=\"https:\/\/phptutorial.net\/php-tutorial\/php-array\/\">array<\/a>:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml\"><span class=\"php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\n$data = &#91;];\n\n<span class=\"hljs-keyword\">try<\/span> {\n\t$f = fopen(<span class=\"hljs-string\">'data.csv'<\/span>, <span class=\"hljs-string\">'r'<\/span>);\n\n\t<span class=\"hljs-keyword\">while<\/span> ($row = $fgetcsv($f)) {\n\t\t$data&#91;] = $row;\n\t}\n\n\tfclose($f);\n} <span class=\"hljs-keyword\">catch<\/span> (<span class=\"hljs-keyword\">Exception<\/span> $ex) {\n\t<span class=\"hljs-keyword\">echo<\/span> $ex-&gt;getMessage();\n}\n<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In this example, if an error occurs while reading the file, the execution jumps to the <code>catch<\/code> block. Therefore, the following statement will never run:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">fclose($f);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>When the file is not closed properply, it may be inaccessible later.<\/p>\n\n\n\n<p>To ensure that the file will be closed properly regardless of whatever exception occurs or not, you can close the file in the <code>finally<\/code> block like this:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml\"><span class=\"php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\n$data = &#91;];\n\n<span class=\"hljs-keyword\">try<\/span> {\n\t$f = fopen(<span class=\"hljs-string\">'data.csv'<\/span>, <span class=\"hljs-string\">'r'<\/span>);\n\n\t<span class=\"hljs-keyword\">while<\/span> ($row = $fgetcsv($f)) {\n\t\t$data&#91;] = $row;\n\t}\n} <span class=\"hljs-keyword\">catch<\/span> (<span class=\"hljs-keyword\">Exception<\/span> $ex) {\n\t<span class=\"hljs-keyword\">echo<\/span> $ex-&gt;getMessage();\n} <span class=\"hljs-keyword\">finally<\/span> {\n\t<span class=\"hljs-keyword\">if<\/span> ($f) {\n\t\tfclose($f);\n\t}\n}\n<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The <code>finally<\/code> is an optional block of the try&#8230;catch statement. The <code>finally<\/code> block always executes after the <code>try<\/code> or <code>catch<\/code> block.<\/p>\n\n\n\n<p>In this case, if an exception occurs while reading the file, the execution jumps to the <code>catch<\/code> block to handle it and then the <code>finally<\/code> block executes to close the file.<\/p>\n\n\n\n<p>The syntax of the <code>try...catch...finally<\/code> block is as follows:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml\"><span class=\"php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\n<span class=\"hljs-keyword\">try<\/span> {\n\t<span class=\"hljs-comment\">\/\/ do something<\/span>\n} <span class=\"hljs-keyword\">catch<\/span> (<span class=\"hljs-keyword\">Exception<\/span> $e) {\n\t<span class=\"hljs-comment\">\/\/ code to handle exception<\/span>\n} <span class=\"hljs-keyword\">finally<\/span> {\n\t<span class=\"hljs-comment\">\/\/ code to clean up the resource<\/span>\n}<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id='php-finally-return'>PHP finally &amp; return <a href=\"#php-finally-return\" class=\"anchor\" id=\"php-finally-return\" title=\"Anchor for PHP finally &amp; return\">#<\/a><\/h2>\n\n\n\n<p>The following defines the <code>divide()<\/code> function that returns the division of two numbers. If an error occurs, it returns <code>null<\/code>.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml\"><span class=\"php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">divide<\/span><span class=\"hljs-params\">($x, $y)<\/span>\n<\/span>{\n\t<span class=\"hljs-keyword\">try<\/span> {\n\t\t$result = $x \/ $y;\n\n\t\t<span class=\"hljs-keyword\">return<\/span> $result;\n\t} <span class=\"hljs-keyword\">catch<\/span> (<span class=\"hljs-keyword\">Exception<\/span> $e) {\n\t\t<span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-keyword\">null<\/span>;\n\t} <span class=\"hljs-keyword\">finally<\/span> {\n\t\t<span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-keyword\">null<\/span>;\n\t}\n}<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>However, the following displays NULL instead of 5:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml\"><span class=\"php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n<span class=\"hljs-comment\">\/\/ ...<\/span>\n$result = divide(<span class=\"hljs-number\">10<\/span>, <span class=\"hljs-number\">2<\/span>);\nvar_dump($result); <span class=\"hljs-comment\">\/\/ NULL<\/span><\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Typically, the <code>return<\/code> statement immediately stops the execution and returns a value. However, it doesn&#8217;t work that way when it is used with the <code>try...catch...finally<\/code> statement.<\/p>\n\n\n\n<p>When you use the <code>return<\/code> statement with the <code>try...catch...finally<\/code> statement, the <strong>finally block still executes<\/strong> after the <code>return<\/code> statement.<\/p>\n\n\n\n<p>The result will be returned after the <code>finally<\/code> block is executed. Also, if the <code>finally<\/code> block has a <code>return<\/code> statement, the value from the <code>finally<\/code> block will be returned.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='the-try-finally-statement'>The try&#8230;finally statement <a href=\"#the-try-finally-statement\" class=\"anchor\" id=\"the-try-finally-statement\" title=\"Anchor for The try...finally statement\">#<\/a><\/h2>\n\n\n\n<p id=\"block-3aa6c271-8d37-4b91-9bb3-de69dfe1da59\">In the <code>try...catch..finally<\/code> statement, either the <code>catch<\/code> or <code>finally<\/code> block is optional. Therefore, you can have the <code>try...finally<\/code> statement like this:<\/p>\n\n\n<pre id=\"block-3aab14f9-7de3-421a-8419-7c3a76c2fea8\" class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml\"><span class=\"php\"><span class=\"hljs-meta\">&lt;?php<\/span> \n<span class=\"hljs-keyword\">try<\/span> {\n\t<span class=\"hljs-comment\">\/\/ do something<\/span>\n} <span class=\"hljs-keyword\">finally<\/span> {\n\t<span class=\"hljs-comment\">\/\/ clean up<\/span>\n}<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p id=\"block-7b2a0aaa-1bad-49d1-bef0-20d3688b2fc6\">The <code>try...finally<\/code> statement allows you to throw an exception while cleaning up the resource appropriately.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='summary'>Summary <a href=\"#summary\" class=\"anchor\" id=\"summary\" title=\"Anchor for Summary\">#<\/a><\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li>Use the <code>try...catch...finally<\/code> statement to handle exceptions and clean up the resources.<\/li><li>The <code>finally<\/code> block always executes after the try or catch block.<\/li><li>If the <code>try<\/code> or <code>catch<\/code> has the <code>return<\/code> statement, the value will be returned only after the <code>finally<\/code> block executes.<\/li><\/ul>\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Did you find this tutorial useful?<\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"1011\"\n\t\t\t\tdata-post-url=\"https:\/\/www.phptutorial.net\/php-oop\/php-try-catch-finally\/\"\n\t\t\t\tdata-post-title=\"PHP try&#8230;catch&#8230;finally\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"1011\"\n\t\t\t\tdata-post-url=\"https:\/\/www.phptutorial.net\/php-oop\/php-try-catch-finally\/\"\n\t\t\t\tdata-post-title=\"PHP try&#8230;catch&#8230;finally\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\n\t\t\t<button class=\"btn btn-primary wth-btn-submit\">Send<\/button>\n\t\t\t<button class=\"btn wth-btn-cancel\">Cancel<\/button>\n\t\t\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, you&#8217;ll learn how to use the PHP try&#8230;catch&#8230;finally statement to handle exceptions and clean up the resources.<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":1753,"menu_order":32,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-1011","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/1011","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/comments?post=1011"}],"version-history":[{"count":5,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/1011\/revisions"}],"predecessor-version":[{"id":1020,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/1011\/revisions\/1020"}],"up":[{"embeddable":true,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/1753"}],"wp:attachment":[{"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/media?parent=1011"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}