{"id":1050,"date":"2019-01-31T13:29:08","date_gmt":"2019-01-31T07:59:08","guid":{"rendered":"http:\/\/blog.openwebsolutions.in\/?p=1050"},"modified":"2019-01-31T13:29:50","modified_gmt":"2019-01-31T07:59:50","slug":"know-perfect-way-error-handling-javascript","status":"publish","type":"post","link":"https:\/\/openwebsolutions.in\/blog\/know-perfect-way-error-handling-javascript\/","title":{"rendered":"Know the perfect way of Error handling in JavaScript"},"content":{"rendered":"<p><span style=\"font-weight: 400;\">Sometimes a developer is great at programming, but the scripts may have errors. There are various reasons for these errors. Sometimes it occurs because of our mistakes or maybe an unexpected user input, or it could be an erroneous server response and there could be a thousand of other reasons.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Generally, a script stops it\u2019s execution after an error occurred, printing it to console.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">To overcome this there\u2019s a syntax construct try&#8230;catch which allows to \u201ccatch\u201d errors and, instead of stopping the execution, do something more reasonable.<\/span><\/p>\n<h2><span style=\"font-weight: 400;\">The \u201ctry\u2026catch\u201d syntax<\/span><\/h2>\n<p><span style=\"font-weight: 400;\">The try&#8230;catch consists of two blocks: try, and then catch<\/span><\/p>\n<p><b>try<\/b> <b>{<\/b><b><br \/>\n<\/b><b>\/\/ Here is your code&#8230;<\/b><b><br \/>\n<\/b><b>}<\/b> <b>catch<\/b> <b>(<\/b><b>err<\/b><b>)<\/b> <b>{<\/b><b><br \/>\n<\/b><b>\u00a0<\/b><b>\/\/ error handling<\/b><b><br \/>\n<\/b><b>}<\/b><\/p>\n<h2><span style=\"font-weight: 400;\">It works like this<\/span><\/h2>\n<ol>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">First, the code in <\/span>try{&#8230;} block is<span style=\"font-weight: 400;\"> executed.<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">If there were no errors, then <\/span><span style=\"font-weight: 400;\">catch(err)<\/span><span style=\"font-weight: 400;\"> block is ignored and the execution reaches the end of <\/span><span style=\"font-weight: 400;\">try<\/span><span style=\"font-weight: 400;\"> and then jumps over <\/span><span style=\"font-weight: 400;\">catch<\/span><span style=\"font-weight: 400;\">.<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">If any error occurred during the execution of the try block, then <\/span><span style=\"font-weight: 400;\">the<\/span> execution is stopped, and the control flows to catch<span style=\"font-weight: 400;\">(err)<\/span><span style=\"font-weight: 400;\">. The <\/span><span style=\"font-weight: 400;\">err <\/span><span style=\"font-weight: 400;\">variable contains an error object contains the details of the error&#8230;<\/span><\/li>\n<\/ol>\n<p><span style=\"font-weight: 400;\">Example:<\/span><\/p>\n<p>try {<br \/>\nalert(&#8216;Start of try block&#8217;);<br \/>\nabcd; \/\/ error, variable is not defined!<br \/>\nalert(&#8216;End of try&#8217;);<br \/>\n} catch(err) {<br \/>\nalert(`Some error has occurred!`);<\/p>\n<p>}<br \/>\nalert(&#8220;the execution continues&#8221;);<\/p>\n<p>&nbsp;<\/p>\n<h2><span style=\"font-weight: 400;\">Things to be remembered<\/span><\/h2>\n<p><b>try..catch<\/b><b> only works for runtime errors<\/b><\/p>\n<p><span style=\"font-weight: 400;\">For <\/span><span style=\"font-weight: 400;\">try..catch<\/span><span style=\"font-weight: 400;\"> to work, the code must be valid. i.e., it should be a valid JavaScript.<\/span><\/p>\n<p><b>try..catch<\/b><b> works synchronously<\/b><\/p>\n<p><span style=\"font-weight: 400;\">If any exception happens within a \u00a0\u201cscheduled\u201d code, like <\/span><span style=\"font-weight: 400;\">setTimeout<\/span><span style=\"font-weight: 400;\">, then <\/span><span style=\"font-weight: 400;\">try..catch<\/span><span style=\"font-weight: 400;\"> won\u2019t catch the error: That\u2019s because within a setTimeout call the function executed later, when the engine already left the try&#8230;catch block. To catch an exception inside a setTimeout function, <\/span><span style=\"font-weight: 400;\">try..catch<\/span><span style=\"font-weight: 400;\"> must be inside that function:<\/span><\/p>\n<h2><span style=\"font-weight: 400;\">Error object<\/span><\/h2>\n<p><span style=\"font-weight: 400;\">During the execution, if an error occurred then JavaScript generates an object which contains the details of the error. Then the error object is passed as an argument to <\/span><span style=\"font-weight: 400;\">catch<\/span><span style=\"font-weight: 400;\">:<\/span><\/p>\n<p><strong>try {<\/strong><br \/>\n<strong> \u00a0\/\/ &#8230;<\/strong><br \/>\n<strong>} catch(err) { \/\/ err is the &#8220;error object&#8221;, you could use another word instead of err<\/strong><br \/>\n<strong> \u00a0\/\/ &#8230;<\/strong><br \/>\n<strong>}<\/strong><\/p>\n<p><span style=\"font-weight: 400;\">The error object inside <\/span><span style=\"font-weight: 400;\">catch<\/span><span style=\"font-weight: 400;\"> block has two main properties:<\/span><\/p>\n<p><b>Name<\/b><\/p>\n<p><span style=\"font-weight: 400;\">Error name. For an undefined variable that\u2019s <\/span><span style=\"font-weight: 400;\">&#8220;ReferenceError&#8221;<\/span><span style=\"font-weight: 400;\">.<\/span><\/p>\n<p><b>Message<\/b><\/p>\n<p><span style=\"font-weight: 400;\">Textual message about error details.<\/span><\/p>\n<h2>Try\u2026catch\u2026finally<\/h2>\n<p><span style=\"font-weight: 400;\">The <\/span><span style=\"font-weight: 400;\">try..catch<\/span><span style=\"font-weight: 400;\"> block may have one more code clause: <\/span><span style=\"font-weight: 400;\">finally<\/span><span style=\"font-weight: 400;\">.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">If it exists, it runs in all cases:<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">after <\/span><span style=\"font-weight: 400;\">try<\/span><span style=\"font-weight: 400;\">, if there were no errors,<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">after <\/span><span style=\"font-weight: 400;\">catch<\/span><span style=\"font-weight: 400;\">, if there were errors.<\/span><\/li>\n<\/ul>\n<p><span style=\"font-weight: 400;\">The syntax looks like this:<\/span><span style=\"font-weight: 400;\"> \u00a0<\/span><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><strong>try {<br \/>\ntry to execute the code<br \/>\n} catch(e) {<br \/>\nhandle errors<br \/>\n} finally {<br \/>\nexecute always<br \/>\n}<\/strong><\/p>\n<p><span style=\"font-weight: 400;\">The code has two ways of execution:<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">If the code has some error,then <\/span><span style=\"font-weight: 400;\">try -&gt; catch -&gt; finally<\/span><span style=\"font-weight: 400;\">.<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">If there were no error, then <\/span><span style=\"font-weight: 400;\">try -&gt; finally<\/span><span style=\"font-weight: 400;\">.<\/span><\/li>\n<\/ul>\n<h2><span style=\"font-weight: 400;\">Summary<\/span><\/h2>\n<p><span style=\"font-weight: 400;\">The <\/span><span style=\"font-weight: 400;\">try..catch<\/span><span style=\"font-weight: 400;\"> block allows to handle any runtime errors.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">The syntax is:<\/span><\/p>\n<p><strong>try {<br \/>\n\/\/ Hereis your code<br \/>\n} catch(err) {<br \/>\n\/\/ if an error happened, then jump here<br \/>\n\/\/ err is the error object<br \/>\n} finally {<br \/>\n\/\/ execute this in any case after try\/catch<br \/>\n}<\/strong><\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Sometimes a developer is great at programming, but the scripts may have errors. There are various reasons for these errors. Sometimes it occurs because of our mistakes or maybe an unexpected user input, or it could be an erroneous server response and there could be a thousand of other reasons. Generally, a script stops it\u2019s [&hellip;]<\/p>\n","protected":false},"author":7,"featured_media":1106,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[12,72],"tags":[43,73,15],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v14.8.1 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Know the perfect way of Error handling in JavaScript<\/title>\n<meta name=\"robots\" content=\"index, follow\" \/>\n<meta name=\"googlebot\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<meta name=\"bingbot\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/openwebsolutions.in\/blog\/know-perfect-way-error-handling-javascript\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Know the perfect way of Error handling in JavaScript\" \/>\n<meta property=\"og:description\" content=\"Sometimes a developer is great at programming, but the scripts may have errors. There are various reasons for these errors. Sometimes it occurs because of our mistakes or maybe an unexpected user input, or it could be an erroneous server response and there could be a thousand of other reasons. Generally, a script stops it\u2019s [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/openwebsolutions.in\/blog\/know-perfect-way-error-handling-javascript\/\" \/>\n<meta property=\"og:site_name\" content=\"Openweb Solutions Blog\" \/>\n<meta property=\"article:published_time\" content=\"2019-01-31T07:59:08+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2019-01-31T07:59:50+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/openwebsolutions.in\/blog\/wp-content\/uploads\/2019\/01\/mistake-3085712_1920.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1920\" \/>\n\t<meta property=\"og:image:height\" content=\"1297\" \/>\n<meta name=\"twitter:card\" content=\"summary\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebSite\",\"@id\":\"https:\/\/openwebsolutions.in\/blog\/#website\",\"url\":\"https:\/\/openwebsolutions.in\/blog\/\",\"name\":\"Openweb Solutions Blog\",\"description\":\"Transforming ideas into reality\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":\"https:\/\/openwebsolutions.in\/blog\/?s={search_term_string}\",\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/openwebsolutions.in\/blog\/know-perfect-way-error-handling-javascript\/#primaryimage\",\"inLanguage\":\"en-US\",\"url\":\"https:\/\/openwebsolutions.in\/blog\/wp-content\/uploads\/2019\/01\/mistake-3085712_1920.jpg\",\"width\":1920,\"height\":1297,\"caption\":\"error handling in javascript\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/openwebsolutions.in\/blog\/know-perfect-way-error-handling-javascript\/#webpage\",\"url\":\"https:\/\/openwebsolutions.in\/blog\/know-perfect-way-error-handling-javascript\/\",\"name\":\"Know the perfect way of Error handling in JavaScript\",\"isPartOf\":{\"@id\":\"https:\/\/openwebsolutions.in\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/openwebsolutions.in\/blog\/know-perfect-way-error-handling-javascript\/#primaryimage\"},\"datePublished\":\"2019-01-31T07:59:08+00:00\",\"dateModified\":\"2019-01-31T07:59:50+00:00\",\"author\":{\"@id\":\"https:\/\/openwebsolutions.in\/blog\/#\/schema\/person\/399d10640115db2d90b44eb60be86bbf\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/openwebsolutions.in\/blog\/know-perfect-way-error-handling-javascript\/\"]}]},{\"@type\":\"Person\",\"@id\":\"https:\/\/openwebsolutions.in\/blog\/#\/schema\/person\/399d10640115db2d90b44eb60be86bbf\",\"name\":\"Suman Saha\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/openwebsolutions.in\/blog\/#personlogo\",\"inLanguage\":\"en-US\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/05a5f8f27a59bb3550048704dcc6b041?s=96&r=g\",\"caption\":\"Suman Saha\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","_links":{"self":[{"href":"https:\/\/openwebsolutions.in\/blog\/wp-json\/wp\/v2\/posts\/1050"}],"collection":[{"href":"https:\/\/openwebsolutions.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/openwebsolutions.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/openwebsolutions.in\/blog\/wp-json\/wp\/v2\/users\/7"}],"replies":[{"embeddable":true,"href":"https:\/\/openwebsolutions.in\/blog\/wp-json\/wp\/v2\/comments?post=1050"}],"version-history":[{"count":4,"href":"https:\/\/openwebsolutions.in\/blog\/wp-json\/wp\/v2\/posts\/1050\/revisions"}],"predecessor-version":[{"id":1104,"href":"https:\/\/openwebsolutions.in\/blog\/wp-json\/wp\/v2\/posts\/1050\/revisions\/1104"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/openwebsolutions.in\/blog\/wp-json\/wp\/v2\/media\/1106"}],"wp:attachment":[{"href":"https:\/\/openwebsolutions.in\/blog\/wp-json\/wp\/v2\/media?parent=1050"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/openwebsolutions.in\/blog\/wp-json\/wp\/v2\/categories?post=1050"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/openwebsolutions.in\/blog\/wp-json\/wp\/v2\/tags?post=1050"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}