

{"id":68067,"date":"2019-08-17T10:27:37","date_gmt":"2019-08-17T04:57:37","guid":{"rendered":"https:\/\/data-flair.training\/blogs\/?p=68067"},"modified":"2019-08-17T10:27:37","modified_gmt":"2019-08-17T04:57:37","slug":"javascript-errors","status":"publish","type":"post","link":"https:\/\/data-flair.training\/blogs\/javascript-errors\/","title":{"rendered":"JavaScript Errors &#8211; A Comprehensive Guide to master Error Handling"},"content":{"rendered":"<div class='__iawmlf-post-loop-links' style='display:none;' data-iawmlf-post-links='[{&quot;id&quot;:1444,&quot;href&quot;:&quot;https:\\\/\\\/en.wikipedia.org\\\/wiki\\\/String_(computer_science)&quot;,&quot;archived_href&quot;:&quot;http:\\\/\\\/web-wp.archive.org\\\/web\\\/20251006132412\\\/https:\\\/\\\/en.wikipedia.org\\\/wiki\\\/String_(computer_science)&quot;,&quot;redirect_href&quot;:&quot;&quot;,&quot;checks&quot;:[{&quot;date&quot;:&quot;2025-12-09 07:14:04&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2025-12-16 11:20:55&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2025-12-26 15:10:35&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-01-04 14:18:24&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-02-01 06:01:11&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-02-04 11:56:10&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-02-09 06:05:54&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-03-02 12:08:33&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-03-09 07:45:50&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-03-16 18:26:07&quot;,&quot;http_code&quot;:429},{&quot;date&quot;:&quot;2026-03-30 07:38:57&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-04-28 02:38:05&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-05-09 22:44:28&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-05-23 12:51:03&quot;,&quot;http_code&quot;:200}],&quot;broken&quot;:false,&quot;last_checked&quot;:{&quot;date&quot;:&quot;2026-05-23 12:51:03&quot;,&quot;http_code&quot;:200},&quot;process&quot;:&quot;done&quot;}]'><\/div>\n<p>There often comes a situation when you run into an error that may or may not be fatal for your application. After learning all you did on <em><strong><a href=\"https:\/\/data-flair.training\/blogs\/javascript-garbage-collection\/\">JavaScript Garbage Collection<\/a><\/strong><\/em>, I think you are ready to explore what all the errors we stumbled upon in our previous tutorials. We saw many errors, including <strong>ReferenceError<\/strong> and <strong>SyntaxError<\/strong> while we tried to understand various JavaScript concepts. This tutorial will help you understand why we get them and how to resolve them. Let\u2019s get on with the concept by first understanding what JavaScript errors actually are.<\/p>\n<h3>What do you mean by Errors?<\/h3>\n<p>What is the first thing you get when you read the word \u201cerror\u201d? It may be a mistake, wrong, incorrect, fault, misinterpretation, etc. You will be correct in some ways, not so much in others. Okay, okay. I\u2019ll stop being so cryptic and help you understand it all in simpler terms.<\/p>\n<p>In programming, <em>an error is a piece of code that disrupts the normal program flow.<\/em> The same definition applies to JavaScript, but with an additional factor. While working in JavaScript, <em>if your code throws an error, the JavaScript interpreter will stop the execution of the program that instant.<\/em> Do not think of JavaScript errors as your adversaries, they can be your best friends while developing a new application. The only way you can debug your JavaScript code is by utilizing the errors and warnings the best you can. You can observe any error, along with the line in which the error occurred, in your browser console window.<\/p>\n<p>In JavaScript, we use the Error constructor to create an error object. Every time a runtime error occurs, the script throws an Error instance. You can create an error object in the following manner:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">new Error([msg]);<\/pre>\n<p>In the above statement, all the parameters are optional, but you need to remember the order of their appearance. The <strong>msg<\/strong> is a human-readable description of the error.<\/p>\n<h3>Error as a Function<\/h3>\n<p>You can use Error as a function, by skipping the new keyword. You will get the same output as above: a new Error object. For example, the following statement creates a new Error object:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">const err = Error(\u201cWe created this error using the function call.\u201d);<\/pre>\n<p><em><strong>Don&#8217;t forget to check &#8211; <a href=\"https:\/\/data-flair.training\/blogs\/javascript-objects\/\">Methods to Create JavaScript Objects<\/a><\/strong><\/em><\/p>\n<h3>Types of JavaScript Errors<\/h3>\n<h4>1. EvalError<\/h4>\n<p>This error creates a JavaScript instance that represents an error occurrence concerning the global function <strong>eval()<\/strong>. Newer versions of JavaScript don\u2019t throw these errors. Instead, they rely on SyntaxError.<\/p>\n<h4>2. RangeError<\/h4>\n<p>It creates an Error instance that occurs when a numeric value or parameter exceeds its allowed range.<\/p>\n<h4>3. ReferenceError<\/h4>\n<p>This error occurs while de-referencing an invalid reference and creates an instance for the same. Dereferencing refers to getting a value stored in memory using another object.<\/p>\n<h4>4. SyntaxError<\/h4>\n<p>This script raises this error and creates an instance when a syntactically incorrect statement occurs while parsing the JavaScript code.<\/p>\n<h4>5. TypeError<\/h4>\n<p>This error occurs when a variable or parameter does not have a valid data type. One such instance where you get a TypeError instance is when you try to perform numeric calculations on a <a href=\"https:\/\/en.wikipedia.org\/wiki\/String_(computer_science)\">String<\/a> object.<\/p>\n<h4>6. URIError<\/h4>\n<p>It occurs and creates an error instance when the <strong>encodeURI()<\/strong> or <strong>decodeURI()<\/strong> methods have incorrect parameters.<\/p>\n<p><em><strong>Time to gain expertise in <a href=\"https:\/\/data-flair.training\/blogs\/javascript-numbers\/\">JavaScript Number Methods<\/a><\/strong><\/em><\/p>\n<h3>The try, catch and finally statements<\/h3>\n<p>JavaScript consists of three blocks associated with error handling: try, catch and finally. The <strong>try<\/strong> block catches whatever exception occurs in the program and throws it to the <strong>catch<\/strong> block. When any of the above errors occur at runtime, the script uses <strong>throw<\/strong> internally to send program control to the catch block. The finally block is optional with a try-catch block. The statements inside this block execute regardless of an error occurring. The catch block must be followed by either exactly one catch or a <strong>finally<\/strong> block or one of both. The syntax for a <strong>try&#8230;catch&#8230;finally<\/strong> statements looks like this:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">try{\r\n        \/\/ statements to execute\r\n        [break;]\r\n} catch(exception){\r\n        \/\/ statements if an error occurs\r\n        [break;]\r\n} finally{\r\n        \/\/ statements that always execute\r\n}<\/pre>\n<p>You will create a JavaScript program to implement these statements before this tutorial ends.<\/p>\n<p><strong>Note:<\/strong> The try-catch statements only work with runtime errors, thus you cannot handle <strong>SyntaxError<\/strong> with their help. If SyntaxError occurs, the script shows the error in the console window, rather than performing error handling.<\/p>\n<h3>Throwing a Generic Error in JavaScript<\/h3>\n<p>It is very easy to throw an error when you want to do so. This is possible with the help of the JavaScript keyword <strong>throw<\/strong> that raises the specified error. You are free to throw any error you might want as per your requirements.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&lt;html&gt;\r\n    &lt;body&gt;\r\n\r\n        &lt;script&gt;\r\n            try{ \/\/ try block searches for an error\r\n                throw new Error(\"Oops, try again!\"); \/\/ throwing an error intentionally\r\n                } catch(e){ \/\/ control jumps over to the catch block when error found\r\n                    document.write(e.name + \": \" + e.message); \/\/message to print when error occurs\r\n                }\r\n        &lt;\/script&gt;\r\n\r\n    &lt;\/body&gt;\r\n&lt;\/html&gt;<\/pre>\n<p><strong>Screenshot:<\/strong><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/generic-error.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-68070\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/generic-error.jpg\" alt=\"generic error code\" width=\"1299\" height=\"742\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/generic-error.jpg 1299w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/generic-error-150x86.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/generic-error-300x171.jpg 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/generic-error-768x439.jpg 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/generic-error-1024x585.jpg 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/generic-error-520x297.jpg 520w\" sizes=\"auto, (max-width: 1299px) 100vw, 1299px\" \/><\/a><\/p>\n<p><strong>Output:<\/strong><\/p>\n<h3><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/generic-error-output.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"img-gray-border aligncenter wp-image-68071 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/generic-error-output.jpg\" alt=\"generic error output - JavaScript Errors\" width=\"1298\" height=\"741\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/generic-error-output.jpg 1298w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/generic-error-output-150x86.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/generic-error-output-300x171.jpg 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/generic-error-output-768x438.jpg 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/generic-error-output-1024x585.jpg 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/generic-error-output-520x297.jpg 520w\" sizes=\"auto, (max-width: 1298px) 100vw, 1298px\" \/><\/a><br \/>\nThe onerror() method<\/h3>\n<p>This was the very first feature to introduce error handling in JavaScript. This event fires whenever an error occurs on the page. It provides three pieces of information to identify the exact nature of the error. These are:<\/p>\n<ul>\n<li><strong>Error message &#8211;<\/strong>\u00a0It is the same message that the browser would display for the given error.<\/li>\n<li><strong>URL &#8211;<\/strong> It is the file location where the error occurred.<\/li>\n<li><strong>Line number &#8211;<\/strong> It is the line number in the given URL that caused the error.<\/li>\n<\/ul>\n<p>The following JavaScript program throws an error on the page and extracts the above three details.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&lt;html&gt;\r\n    &lt;body onload = \"myFunc();\"&gt;\r\n    &lt;p&gt;&lt;\/p&gt;\r\n    &lt;p&gt;&lt;\/p&gt;\r\n\r\n        &lt;script&gt;\r\n                \/\/function executed when error occurs\r\n                window.onerror = function (msg, url, line) {\r\n    document.querySelectorAll('p')[0].innerHTML = \"An error occurred on the page.\";\r\n    document.querySelectorAll('p')[1].innerHTML = \"&lt;b&gt;Message :&lt;\/b&gt; \" + msg + \"&lt;\/br&gt;&lt;b&gt;url :&lt;\/b&gt; \" + url + \"&lt;\/br&gt;&lt;b&gt;Line number :&lt;\/b&gt; \" + line; \r\n}\r\n        &lt;\/script&gt;\r\n\r\n    &lt;\/body&gt;\r\n&lt;\/html&gt;\r\n<\/pre>\n<p><strong>Screenshot:<\/strong><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/JavaScript-error-method.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-68116\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/JavaScript-error-method.jpg\" alt=\"JavaScript error method\" width=\"1299\" height=\"741\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/JavaScript-error-method.jpg 1299w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/JavaScript-error-method-150x86.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/JavaScript-error-method-300x171.jpg 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/JavaScript-error-method-768x438.jpg 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/JavaScript-error-method-1024x584.jpg 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/JavaScript-error-method-520x297.jpg 520w\" sizes=\"auto, (max-width: 1299px) 100vw, 1299px\" \/><\/a><\/p>\n<p><strong>Output:<\/strong><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/JavaScript-error-output.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"img-gray-border aligncenter wp-image-68117 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/JavaScript-error-output.jpg\" alt=\"JavaScript error output\" width=\"1300\" height=\"742\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/JavaScript-error-output.jpg 1300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/JavaScript-error-output-150x86.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/JavaScript-error-output-300x171.jpg 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/JavaScript-error-output-768x438.jpg 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/JavaScript-error-output-1024x584.jpg 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/JavaScript-error-output-520x297.jpg 520w\" sizes=\"auto, (max-width: 1300px) 100vw, 1300px\" \/><\/a><\/p>\n<h3>Handling a Specific Error in JavaScript<\/h3>\n<p>Unlike other programming languages like <a href=\"https:\/\/data-flair.training\/blogs\/java-tutorials-home\/\"><em><strong>Java<\/strong><\/em><\/a>, defining multiple catch blocks is invalid in JavaScript. There is another way in JavaScript that allows you to handle specific errors in your program and work accordingly. You can check for multiple errors in a JavaScript program by following the code below.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&lt;html&gt;\r\n    &lt;body&gt;\r\n        &lt;h3&gt;DataFlair: try-catch-finally statements&lt;\/h3&gt;\r\n\r\n        &lt;script&gt;\r\n            var num1;\r\n            try{ \/\/ try block\r\n                var num1 = num2 * 4; \/\/ you cannot use the reference for num2\r\n                \/\/ creates an Error object and throws it to the catch block\r\n                document.write(\"This is an additional line.&lt;\/br&gt;\"); \/\/ this line is skipped\r\n            } catch(e){ \/\/ catch block\r\n                if(e instanceof ReferenceError)\r\n                    document.write(\"Reference Error&lt;\/br&gt;\");\r\n                else if(e instanceof SyntaxError)\r\n                    document.write(\"Syntax Error&lt;\/br&gt;\");\r\n                else\r\n                    document.write(\"Other Error&lt;\/br&gt;\");\r\n            } finally{ \/\/finally block\r\n                document.write(\"This statement executes whether an error occurs or not.&lt;\/br&gt;\");\r\n            }\r\n        &lt;\/script&gt;\r\n\r\n    &lt;\/body&gt;\r\n&lt;\/html&gt;<\/pre>\n<p><strong>Screenshot:<\/strong><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/try-catch-1.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-68072 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/try-catch-1.jpg\" alt=\"try catch code -JavaScript Errors\" width=\"1298\" height=\"742\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/try-catch-1.jpg 1298w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/try-catch-1-150x86.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/try-catch-1-300x171.jpg 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/try-catch-1-768x439.jpg 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/try-catch-1-1024x585.jpg 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/try-catch-1-520x297.jpg 520w\" sizes=\"auto, (max-width: 1298px) 100vw, 1298px\" \/><\/a><\/p>\n<p><strong>Output:<\/strong><\/p>\n<h3><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/try-catch-output.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"img-gray-border aligncenter wp-image-68073 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/try-catch-output.jpg\" alt=\"try catch output - JavaScript Errors\" width=\"1300\" height=\"742\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/try-catch-output.jpg 1300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/try-catch-output-150x86.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/try-catch-output-300x171.jpg 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/try-catch-output-768x438.jpg 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/try-catch-output-1024x584.jpg 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/try-catch-output-520x297.jpg 520w\" sizes=\"auto, (max-width: 1300px) 100vw, 1300px\" \/><\/a><br \/>\nCustom Errors<\/h3>\n<p>To define your error in a JavaScript program, the first thing you need to do is create your error class that extends the functionality of the Error class in your program. Then you throw your custom exception in the try-catch block to deal with it. The following program defines a custom error in the program and produces the desired output.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&lt;html&gt;\r\n    &lt;body&gt;\r\n\r\n        &lt;script&gt;\r\n            class myCustomError extends Error{\r\n                \/\/ constructor function\r\n                constructor(team, course = \"JavaScript\"){\r\n                    super(team, course);\r\n                    \/\/ assigning values to the myCustomError object\r\n                    this.name = \"myCustomError\";\r\n                    this.team = team;\r\n                    this.course = course;\r\n                }\r\n            }\r\n            try{ \/\/ try block\r\n                throw new myCustomError(\"DataFlair\"); \/\/ custom error thrown\r\n            }catch(e){ \/\/ catch block\r\ndocument.write(\"&lt;b&gt;\" + e.name + \"&lt;\/b&gt;&lt;\/br&gt;\");\r\ndocument.write(\"Team: \" + e.team + \"&lt;\/br&gt;\");\r\ndocument.write(\"Course: \" + e.course + \"&lt;\/br&gt;\");\r\n            }\r\n        &lt;\/script&gt;\r\n\r\n    &lt;\/body&gt;\r\n&lt;\/html&gt;<\/pre>\n<p><strong>Screenshot:<\/strong><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/custom-error.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-68074 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/custom-error.jpg\" alt=\"custom error code - JavaScript Errors\" width=\"1298\" height=\"742\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/custom-error.jpg 1298w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/custom-error-150x86.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/custom-error-300x171.jpg 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/custom-error-768x439.jpg 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/custom-error-1024x585.jpg 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/custom-error-520x297.jpg 520w\" sizes=\"auto, (max-width: 1298px) 100vw, 1298px\" \/><\/a><\/p>\n<p><strong>Output:<\/strong><\/p>\n<h2><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/custom-error-output.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"img-gray-border aligncenter wp-image-68075 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/custom-error-output.jpg\" alt=\"custom error output - JavaScript Errors\" width=\"1297\" height=\"741\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/custom-error-output.jpg 1297w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/custom-error-output-150x86.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/custom-error-output-300x171.jpg 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/custom-error-output-768x439.jpg 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/custom-error-output-1024x585.jpg 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/custom-error-output-520x297.jpg 520w\" sizes=\"auto, (max-width: 1297px) 100vw, 1297px\" \/><\/a><\/h2>\n<h2>Summary<\/h2>\n<p>In this tutorial of DataFlair\u2019s JavaScript tutorial series, we studied about JavaScript Errors. We discussed different errors that occur in JavaScript, along with the various methods to prevent the program to stop the execution. We understood how to handle a general as well as a specific exception. We also created our own error class in JavaScript.<\/p>\n<p><em><strong>Next tutorial to explore &#8211; <a href=\"https:\/\/data-flair.training\/blogs\/javascript-style-attribute\/\">JavaScript Style Attribute<\/a><\/strong><\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>There often comes a situation when you run into an error that may or may not be fatal for your application. After learning all you did on JavaScript Garbage Collection, I think you are&#46;&#46;&#46;<\/p>\n","protected":false},"author":7,"featured_media":68078,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[18979],"tags":[20890,20888,20889,20892,20891],"class_list":["post-68067","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-javascript","tag-finally-statement","tag-javascript-errors","tag-javascript-errors-types","tag-specific-error-handling","tag-try-and-catch-statements"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>JavaScript Errors - A Comprehensive Guide to master Error Handling - DataFlair<\/title>\n<meta name=\"description\" content=\"Learn the concept of JavaScript errors, its types, try, catch and finally statements, throwing a generic error, onerror() method, handling specific error and custom errors in JavaScript.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/data-flair.training\/blogs\/javascript-errors\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"JavaScript Errors - A Comprehensive Guide to master Error Handling - DataFlair\" \/>\n<meta property=\"og:description\" content=\"Learn the concept of JavaScript errors, its types, try, catch and finally statements, throwing a generic error, onerror() method, handling specific error and custom errors in JavaScript.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/data-flair.training\/blogs\/javascript-errors\/\" \/>\n<meta property=\"og:site_name\" content=\"DataFlair\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/DataFlairWS\/\" \/>\n<meta property=\"article:published_time\" content=\"2019-08-17T04:57:37+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/JavaScript-Errors.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"802\" \/>\n\t<meta property=\"og:image:height\" content=\"420\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"DataFlair Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:site\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"DataFlair Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"7 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"JavaScript Errors - A Comprehensive Guide to master Error Handling - DataFlair","description":"Learn the concept of JavaScript errors, its types, try, catch and finally statements, throwing a generic error, onerror() method, handling specific error and custom errors in JavaScript.","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":"https:\/\/data-flair.training\/blogs\/javascript-errors\/","og_locale":"en_US","og_type":"article","og_title":"JavaScript Errors - A Comprehensive Guide to master Error Handling - DataFlair","og_description":"Learn the concept of JavaScript errors, its types, try, catch and finally statements, throwing a generic error, onerror() method, handling specific error and custom errors in JavaScript.","og_url":"https:\/\/data-flair.training\/blogs\/javascript-errors\/","og_site_name":"DataFlair","article_publisher":"https:\/\/www.facebook.com\/DataFlairWS\/","article_published_time":"2019-08-17T04:57:37+00:00","og_image":[{"width":802,"height":420,"url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/JavaScript-Errors.jpg","type":"image\/jpeg"}],"author":"DataFlair Team","twitter_card":"summary_large_image","twitter_creator":"@DataFlairWS","twitter_site":"@DataFlairWS","twitter_misc":{"Written by":"DataFlair Team","Est. reading time":"7 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/data-flair.training\/blogs\/javascript-errors\/#article","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/javascript-errors\/"},"author":{"name":"DataFlair Team","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/beb0cab24b7aa54423a3b50e669a9dcd"},"headline":"JavaScript Errors &#8211; A Comprehensive Guide to master Error Handling","datePublished":"2019-08-17T04:57:37+00:00","mainEntityOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/javascript-errors\/"},"wordCount":1045,"commentCount":0,"publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/javascript-errors\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/JavaScript-Errors.jpg","keywords":["finally statement","JavaScript Errors","JavaScript Errors\u00a0Types","Specific Error Handling","try and catch statements"],"articleSection":["JavaScript Tutorial"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/data-flair.training\/blogs\/javascript-errors\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/data-flair.training\/blogs\/javascript-errors\/","url":"https:\/\/data-flair.training\/blogs\/javascript-errors\/","name":"JavaScript Errors - A Comprehensive Guide to master Error Handling - DataFlair","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/#website"},"primaryImageOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/javascript-errors\/#primaryimage"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/javascript-errors\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/JavaScript-Errors.jpg","datePublished":"2019-08-17T04:57:37+00:00","description":"Learn the concept of JavaScript errors, its types, try, catch and finally statements, throwing a generic error, onerror() method, handling specific error and custom errors in JavaScript.","breadcrumb":{"@id":"https:\/\/data-flair.training\/blogs\/javascript-errors\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/data-flair.training\/blogs\/javascript-errors\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/javascript-errors\/#primaryimage","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/JavaScript-Errors.jpg","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/JavaScript-Errors.jpg","width":802,"height":420,"caption":"JavaScript Errors Tutorial"},{"@type":"BreadcrumbList","@id":"https:\/\/data-flair.training\/blogs\/javascript-errors\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog Home","item":"https:\/\/data-flair.training\/blogs\/"},{"@type":"ListItem","position":2,"name":"JavaScript Tutorial","item":"https:\/\/data-flair.training\/blogs\/category\/javascript\/"},{"@type":"ListItem","position":3,"name":"JavaScript Errors &#8211; A Comprehensive Guide to master Error Handling"}]},{"@type":"WebSite","@id":"https:\/\/data-flair.training\/blogs\/#website","url":"https:\/\/data-flair.training\/blogs\/","name":"DataFlair","description":"Learn Today. Lead Tomorrow.","publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/data-flair.training\/blogs\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/data-flair.training\/blogs\/#organization","name":"DataFlair","url":"https:\/\/data-flair.training\/blogs\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","width":106,"height":48,"caption":"DataFlair"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/DataFlairWS\/","https:\/\/x.com\/DataFlairWS","https:\/\/www.linkedin.com\/company\/dataflair-web-services-pvt-ltd\/","https:\/\/www.youtube.com\/user\/DataFlairWS"]},{"@type":"Person","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/beb0cab24b7aa54423a3b50e669a9dcd","name":"DataFlair Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/c322416204232f4dd97ef3901b0a499a5d34d7ba7fe333f4bfe53a907873d293?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/c322416204232f4dd97ef3901b0a499a5d34d7ba7fe333f4bfe53a907873d293?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/c322416204232f4dd97ef3901b0a499a5d34d7ba7fe333f4bfe53a907873d293?s=96&d=mm&r=g","caption":"DataFlair Team"},"description":"DataFlair Team specializes in creating clear, actionable content on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Backed by industry expertise, we make learning easy and career-oriented for beginners and pros alike.","url":"https:\/\/data-flair.training\/blogs\/author\/dfteam3\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/68067","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/users\/7"}],"replies":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/comments?post=68067"}],"version-history":[{"count":4,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/68067\/revisions"}],"predecessor-version":[{"id":68119,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/68067\/revisions\/68119"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media\/68078"}],"wp:attachment":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media?parent=68067"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/categories?post=68067"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/tags?post=68067"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}