{"id":56524,"date":"2023-01-25T15:03:55","date_gmt":"2023-01-25T09:33:55","guid":{"rendered":"https:\/\/www.tothenew.com\/blog\/?p=56524"},"modified":"2024-01-02T17:41:48","modified_gmt":"2024-01-02T12:11:48","slug":"why-and-how-finally-block-is-not-meaningless","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/why-and-how-finally-block-is-not-meaningless\/","title":{"rendered":"Why and how finally{\u2026} block is NOT meaningless"},"content":{"rendered":"<p><img decoding=\"async\" src=\"\/blog\/wp-ttn-blog\/uploads\/2024\/01\/1i__cDHMZ7lr508TD06h-xw.png\" \/><\/p>\n<p>A definition first (from the internet):<\/p>\n<p id=\"63a5\" class=\"pw-post-body-paragraph abs abt abu ql b abv afq vz abx aby afr wc aca acb afs acd ace acf aft ach aci acj afu acl acm acn nd bx\" data-selectable-paragraph=\"\"><code class=\"em agj agk agl aga b\"><strong class=\"ql mx\">try-catch-finally<\/strong><\/code><strong class=\"ql mx\">\u00a0is used to handle runtime errors and prevent them from halting the execution of a program.<\/strong><\/p>\n<p id=\"16e3\" class=\"pw-post-body-paragraph abs abt abu ql b abv afq vz abx aby afr wc aca acb afs acd ace acf aft ach aci acj afu acl acm acn nd bx\" data-selectable-paragraph=\"\">The finally block triggers regardless of:<\/p>\n<ol class=\"\">\n<li id=\"c5b4\" class=\"ahd ahe abu ql b abv afq aby afr acb ahf acf ahg acj ahh acn ahi ahj ahk dz bx\" data-selectable-paragraph=\"\">what is implemented inside the\u00a0<code class=\"em agj agk agl aga b\">try-catch<\/code>\u00a0block<\/li>\n<li id=\"6364\" class=\"ahd ahe abu ql b abv ahl aby ahm acb ahn acf aho acj ahp acn ahi ahj ahk dz bx\" data-selectable-paragraph=\"\">any errors encountered inside\u00a0<code class=\"em agj agk agl aga b\">try-catch<\/code>\u00a0block<\/li>\n<li id=\"3161\" class=\"ahd ahe abu ql b abv ahl aby ahm acb ahn acf aho acj ahp acn ahi ahj ahk dz bx\" data-selectable-paragraph=\"\">any\u00a0<code class=\"em agj agk agl aga b\">return<\/code>\u00a0statement written inside the\u00a0<code class=\"em agj agk agl aga b\">try-catch<\/code>\u00a0block<\/li>\n<\/ol>\n<h4 id=\"773e\" class=\"agn age abu bu wr ago agp agq pp agr ags agt pu pv agu pw pz qa agv qb qe qf agw qg qj agx bx\">Example 1<\/h4>\n<p id=\"6784\" class=\"pw-post-body-paragraph abs abt abu ql b abv agy vz abx aby agz wc aca acb aha acd ace acf ahb ach aci acj ahc acl acm acn nd bx\" data-selectable-paragraph=\"\">Consider this example\u00a0<strong class=\"ql mx\">where there\u2019s no error<\/strong>:<\/p>\n<pre>(<span class=\"hljs-function\">()=&gt;<\/span>{\r\n<span class=\"hljs-keyword\">try<\/span> {\r\n<span class=\"hljs-variable.language\">console<\/span>.<span class=\"hljs-title.function\">log<\/span>(<span class=\"hljs-string\">`Try block triggered`<\/span>)\r\n<span class=\"hljs-keyword\">return<\/span> \r\n} <span class=\"hljs-keyword\">catch<\/span> (error) {\r\n<span class=\"hljs-variable.language\">console<\/span>.<span class=\"hljs-title.function\">log<\/span>(error)\r\n}<span class=\"hljs-keyword\">finally<\/span>{\r\n<span class=\"hljs-variable.language\">console<\/span>.<span class=\"hljs-title.function\">log<\/span>(<span class=\"hljs-string\">'Finally triggered'<\/span>)\r\n}\r\n\r\n<span class=\"hljs-variable.language\">console<\/span>.<span class=\"hljs-title.function\">log<\/span>(<span class=\"hljs-string\">`Rest of the code outside try-catch`<\/span>)\r\n\r\n})()<\/pre>\n<pre class=\"ui afz aga anh aoj iq nr bx\"><span id=\"ead2\" class=\"aii age abu aga b eb aok aol y aom agi\" data-selectable-paragraph=\"\">\/\/ Output\r\n<\/span><span id=\"bf17\" class=\"aii age abu aga b eb aon aol y aom agi\" data-selectable-paragraph=\"\">Try block triggered\r\n<\/span><span id=\"35e6\" class=\"aii age abu aga b eb aon aol y aom agi\" data-selectable-paragraph=\"\">Finally triggered<\/span><\/pre>\n<blockquote><p><em><strong class=\"ql mx\">Note that the \u201cRest of the code outside try-catch\u201d doesn\u2019t get logged on the console<\/strong><\/em><\/p><\/blockquote>\n<p>The finally block will override the return statement written inside the try block.<\/p>\n<pre>(<span class=\"hljs-function\">()=&gt;<\/span>{\r\n<span class=\"hljs-keyword\">try<\/span> {\r\n\r\n<span class=\"hljs-variable.language\">console<\/span>.<span class=\"hljs-title.function\">log<\/span>(<span class=\"hljs-string\">`Try block triggered`<\/span>)\r\n\r\n<span class=\"hljs-keyword\">throw<\/span> <span class=\"hljs-string\">`Oops... an error occurred`<\/span> \r\n\r\n} <span class=\"hljs-keyword\">catch<\/span> (error) {\r\n\r\n<span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-variable.language\">console<\/span>.<span class=\"hljs-title.function\">log<\/span>(error)\r\n\r\n} <span class=\"hljs-keyword\">finally<\/span>{\r\n\r\n<span class=\"hljs-variable.language\">console<\/span>.<span class=\"hljs-title.function\">log<\/span>(<span class=\"hljs-string\">'Finally triggered'<\/span>)\r\n}\r\n\r\n<span class=\"hljs-variable.language\">console<\/span>.<span class=\"hljs-title.function\">log<\/span>(<span class=\"hljs-string\">`Rest of the code outside try-catch`<\/span>)\r\n})()<\/pre>\n<pre class=\"ui afz aga anh aoj iq nr bx\"><span id=\"b9bc\" class=\"aii age abu aga b eb aok aol y aom agi\" data-selectable-paragraph=\"\">\/\/Output\r\n<\/span><span id=\"2937\" class=\"aii age abu aga b eb aon aol y aom agi\" data-selectable-paragraph=\"\">Try block triggered\r\n<\/span><span id=\"ba0f\" class=\"aii age abu aga b eb aon aol y aom agi\" data-selectable-paragraph=\"\">Oops... an error occurred\r\n<\/span><span id=\"5b3d\" class=\"aii age abu aga b eb aon aol y aom agi\" data-selectable-paragraph=\"\">Finally triggered\r\n<\/span><\/pre>\n<blockquote><p><em><strong class=\"ql mx\">Note that the \u201cRest of the code outside try-catch\u201d doesn\u2019t get logged on the console because of the return statement<\/strong><\/em><\/p><\/blockquote>\n<p>From both the example, it becomes apparent that\u00a0<code class=\"em agj agk agl aga b\">finally<\/code>\u00a0block gets executed while the code written outside the\u00a0<code class=\"em agj agk agl aga b\">try-catch-finally<\/code>\u00a0block doesn\u2019t get executed.<\/p>\n<h4 id=\"7687\" class=\"agn age abu bu wr ago agp agq pp agr ags agt pu pv agu pw pz qa agv qb qe qf agw qg qj agx bx\">Example 3<\/h4>\n<p id=\"4948\" class=\"pw-post-body-paragraph abs abt abu ql b abv agy vz abx aby agz wc aca acb aha acd ace acf ahb ach aci acj ahc acl acm acn nd bx\" data-selectable-paragraph=\"\">Consider this example\u00a0<strong class=\"ql mx\">where there is no<\/strong>\u00a0<code class=\"em agj agk agl aga b\">return<\/code>\u00a0<strong class=\"ql mx\">statement<\/strong>:<\/p>\n<pre class=\"afv afw afx afy adr afz aga eg gm agb agc bx\"><span id=\"f174\" class=\"agd age abu aga b bv agf agg y agh agi\" data-selectable-paragraph=\"\">(<span class=\"hljs-function\">()=&gt;<\/span>{\r\n    <span class=\"hljs-keyword\">try<\/span> {\r\n      \r\n      <span class=\"hljs-variable.language\">console<\/span>.<span class=\"hljs-title.function\">log<\/span>(<span class=\"hljs-string\">`Try block triggered`<\/span>)\r\n      \r\n      <span class=\"hljs-keyword\">throw<\/span> <span class=\"hljs-string\">`Oops... an error occurred`<\/span>\r\n    \r\n    } <span class=\"hljs-keyword\">catch<\/span> (error) {\r\n    \r\n      <span class=\"hljs-variable.language\">console<\/span>.<span class=\"hljs-title.function\">log<\/span>(error)\r\n   \r\n     }<span class=\"hljs-keyword\">finally<\/span>{\r\n    \r\n      <span class=\"hljs-variable.language\">console<\/span>.<span class=\"hljs-title.function\">log<\/span>(<span class=\"hljs-string\">'Finally triggered'<\/span>)\r\n   \r\n     }\r\n    \r\n  <span class=\"hljs-variable.language\">console<\/span>.<span class=\"hljs-title.function\">log<\/span>(<span class=\"hljs-string\">`Rest of the code outside try-catch`<\/span>)\r\n})()<\/span><\/pre>\n<pre class=\"ui afz aga anh aoj iq nr bx\"><span id=\"f243\" class=\"aii age abu aga b eb aok aol y aom agi\" data-selectable-paragraph=\"\">\/\/Output\r\nTry block triggered\r\n<\/span><span id=\"ea18\" class=\"aii age abu aga b eb aon aol y aom agi\" data-selectable-paragraph=\"\">Oops... an error occurred\r\n<\/span><span id=\"4702\" class=\"aii age abu aga b eb aon aol y aom agi\" data-selectable-paragraph=\"\">Finally triggered\r\n<\/span><span id=\"0cfc\" class=\"aii age abu aga b eb aon aol y aom agi\" data-selectable-paragraph=\"\">Rest of the code outside try-catch<\/span><\/pre>\n<blockquote>\n<p data-selectable-paragraph=\"\"><em><strong class=\"ql mx\">Note that this time, \u201cRest of the code outside try-catch\u201d gets printed on the console because there is no return statement in this example implementation<\/strong><\/em><\/p>\n<\/blockquote>\n<p id=\"4421\" class=\"pw-post-body-paragraph abs abt abu ql b abv afq vz abx aby afr wc aca acb afs acd ace acf aft ach aci acj afu acl acm acn nd bx\" data-selectable-paragraph=\"\"><strong class=\"ql mx\">A good use-case for finally block can be to put important codes such as clean up code e.g. closing the file or closing the connection<\/strong>.<\/p>\n<p id=\"50b9\" class=\"pw-post-body-paragraph abs abt abu ql b abv afq vz abx aby afr wc aca acb afs acd ace acf aft ach aci acj afu acl acm acn nd bx\" data-selectable-paragraph=\"\">Hence, we may not necessarily need a finally{\u2026} block in our code all the time, but it can serve an important purpose for certain similar use-cases.<\/p>\n<p data-selectable-paragraph=\"\">***<\/p>\n<h3 id=\"1328\" class=\"agn age abu bu wr ago aix agq pp agr aiy agt pu pv aiz pw pz qa aja qb qe qf ajb qg qj agx bx\">Note of thanks \u2764\ufe0f<\/h3>\n<p><img decoding=\"async\" src=\"\/blog\/wp-ttn-blog\/uploads\/2024\/01\/1L-DG46rgfgciZTXEDmAEbw.gif\" \/><\/p>\n<p>Thank you for stopping by. Hope, you find this article useful. Please <a class=\"ax aht\" href=\"https:\/\/medium.com\/@shiva.chaturvedi91\" rel=\"noopener\"><strong class=\"ql mx\"><em class=\"ahq\">follow me<\/em><\/strong><\/a>\u00a0on medium and help me reach 1k followers ??. That will truly encourage me to put out more content.<\/p>\n<div class=\"ap-custom-wrapper\"><\/div><!--ap-custom-wrapper-->","protected":false},"excerpt":{"rendered":"<p>A definition first (from the internet): try-catch-finally\u00a0is used to handle runtime errors and prevent them from halting the execution of a program. The finally block triggers regardless of: what is implemented inside the\u00a0try-catch\u00a0block any errors encountered inside\u00a0try-catch\u00a0block any\u00a0return\u00a0statement written inside the\u00a0try-catch\u00a0block Example 1 Consider this example\u00a0where there\u2019s no error: (()=&gt;{ try { console.log(`Try block triggered`) [&hellip;]<\/p>\n","protected":false},"author":1535,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":23},"categories":[3429,4684,1185,1994,1],"tags":[5084,5093,1787,229,55,1177,1472],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/56524"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/1535"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=56524"}],"version-history":[{"count":3,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/56524\/revisions"}],"predecessor-version":[{"id":59745,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/56524\/revisions\/59745"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=56524"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=56524"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=56524"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}