{"id":823,"date":"2025-05-06T07:25:17","date_gmt":"2025-05-06T07:25:17","guid":{"rendered":"https:\/\/www.wordpress-prod.sharpener.tech\/?p=823"},"modified":"2025-05-24T14:25:56","modified_gmt":"2025-05-24T14:25:56","slug":"error-handling-try-catch-finally-block","status":"publish","type":"post","link":"https:\/\/www.sharpener.tech\/blog\/error-handling-try-catch-finally-block\/","title":{"rendered":"Error Handling -(Try-catch-finally-block)"},"content":{"rendered":"\n<figure class=\"wp-block-image size-large\"><img fetchpriority=\"high\" decoding=\"async\" width=\"1024\" height=\"683\" src=\"https:\/\/www.wordpress-prod.sharpener.tech\/wp-content\/uploads\/2025\/05\/Error-Handling-Try-catch-finally-block-1024x683.jpg\" alt=\"Error Handling -(Try-catch-finally-block)\" class=\"wp-image-826\" srcset=\"https:\/\/sharpener-wordpress.s3.ap-south-1.amazonaws.com\/blog\/wp-content\/uploads\/2025\/05\/06073846\/Error-Handling-Try-catch-finally-block-1024x683.jpg 1024w, https:\/\/sharpener-wordpress.s3.ap-south-1.amazonaws.com\/blog\/wp-content\/uploads\/2025\/05\/06073846\/Error-Handling-Try-catch-finally-block-300x200.jpg 300w, https:\/\/sharpener-wordpress.s3.ap-south-1.amazonaws.com\/blog\/wp-content\/uploads\/2025\/05\/06073846\/Error-Handling-Try-catch-finally-block-768x512.jpg 768w, https:\/\/sharpener-wordpress.s3.ap-south-1.amazonaws.com\/blog\/wp-content\/uploads\/2025\/05\/06073846\/Error-Handling-Try-catch-finally-block.jpg 1536w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>Hello everyone. I hope you are doing well.<\/p>\n\n\n\n<p>I am Julian Toppo and&nbsp;today we will learn more about error handling and it\u2019s implementation. We will also learn how we handled empty input values in the previous video.&nbsp;<\/p>\n\n\n\n<p>Error handling is like having a plan or dealing with mistakes when they happen.<\/p>\n\n\n\n<p>Let me share a story about a to-do app I created.&nbsp;<\/p>\n\n\n\n<p>I was excited that my friends would use it,&nbsp;<\/p>\n\n\n\n<p>but soon, they started facing problems.<\/p>\n\n\n\n<p>The app crashed when users submitted tasks with empty fields or special characters.<\/p>\n\n\n\n<p>This was because my code didn\u2019t handle errors properly.&nbsp;<\/p>\n\n\n\n<p>Luckily, I learned my lesson about the importance of error handling.<\/p>\n\n\n\n<p>Let\u2019s try to understand error handling&nbsp;<\/p>\n\n\n\n<p>by taking an example of an airport security check.<\/p>\n\n\n\n<p>Airport security is strict.&nbsp;<\/p>\n\n\n\n<p>They have clear rules on what you can bring.<\/p>\n\n\n\n<p>For example, you can only carry 100 ml of liquid.&nbsp;<\/p>\n\n\n\n<p>You can\u2019t bring flammable items like lighters.&nbsp;<\/p>\n\n\n\n<p>Sharp objects like knives are also not allowed.<\/p>\n\n\n\n<p>These are the basics, but rules can vary a bit .<\/p>\n\n\n\n<p><strong>-Why do you think there\u2019s so much strictness?&nbsp;<\/strong><\/p>\n\n\n\n<p>Let a person with a gun sneaks into the airport.<br>This is dangerous for everyone. It could disrupt airport operations.<br>To prevent this, we check for weapons during security.<br>This way, we stop problems before they start.<\/p>\n\n\n\n<p>So Being proactive helps keep everyone safe.<\/p>\n\n\n\n<p><strong>Similarly in coding<\/strong>:<\/p>\n\n\n\n<p>Errors can crash your app or cause incorrect results.<br>Instead of waiting for a crash, we handle errors ahead of time.<\/p>\n\n\n\n<p>(New Additions after this)<\/p>\n\n\n\n<p><strong>Implementation Example:<\/strong><\/p>\n\n\n\n<p>Let\u2019s go back to the example of a form<br>When we are writing code to read the name and age value in the form<br>errors can come anywhere.<br>In this code, the error could occur if there\u2019s a misspelled word or<br>if we are trying to access something that is not present.<br>Let\u2019s say (referring to the code) we are trying to access the name using the event. target,<br>But wait we have misspelled the word name and we are using \u201c.value\u201d for it.<br>This will result in standard predefined errors like Type errors .<br>This will cause our application to work incorrectly.<br>Take a look:<br>Wouldn\u2019t it be better if we could handle the code,<br>without disrupting the flow of our application.<br>For implementing this we enclose the code which will result in possible errors inside the try block.<\/p>\n\n\n\n<p>This will ensure that any potential error inside the block is handled.<br><br><em>Will show the code implementation of error occuring using.<\/em><\/p>\n\n\n\n<p><strong>Catch:<\/strong><\/p>\n\n\n\n<p>Now that error is occurring. Who will handle it?<br>The error occurring in the try block is caught by the catch block.&nbsp;<\/p>\n\n\n\n<p>Let\u2019s have a look:<br>It processes the error, typically by logging an error message or performing other error recovery actions.<br>This helps us in handling errors more efficiently without crashing the whole application or displaying incorrect results in our case no results.<\/p>\n\n\n\n<p><em>Will show the code implementation of error handling using code.<\/em><\/p>\n\n\n\n<p><strong>Throw:&nbsp;<\/strong><\/p>\n\n\n\n<p>In addition to handling standard errors such as Type Errors, Reference Errors, and Syntax Errors,<br>we can also raise custom errors based on specific conditions in our code.<\/p>\n\n\n\n<p>Extra custom errors help us specify problems easily,<br>which makes fixing them simpler.<br>They also give clearer messages to developers and users, making the app work better.<\/p>\n\n\n\n<p>For example, if we need to check that the name and age fields aren&#8217;t empty,<br>we can create a custom error.<br>If either field is empty, it will show a message like,<br>Value of age or name cannot be empty.\u201d<br>This message is one we&#8217;ve created to improve the app and is<br>not just a standard JavaScript error.<\/p>\n\n\n\n<p>Let\u2019s take a&nbsp; look at how we can implement this.<\/p>\n\n\n\n<p><br><br>If we go by the traditional format of checking whether the element is empty or not.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Form Inputs<\/strong>: You manually check if inputs like name and age are valid, not empty, and correctly formatted.<br>Each condition has its own error handling.<\/li>\n\n\n\n<li><strong>DOM Elements<\/strong>: You also check if elements accessed with getElementById exist, handling errors separately if they\u2019re missing.<\/li>\n<\/ul>\n\n\n\n<p><em>Will show the code implementation of error handling using code.<\/em><\/p>\n\n\n\n<p><strong>One of the biggest disadvantage of doing this :<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Repetitive Code<\/strong>: Each if statement has its own error-handling logic.<\/li>\n\n\n\n<li><strong>Manual Control<\/strong>: You need to manage the flow and error handling manually for each condition<\/li>\n<\/ul>\n\n\n\n<p>So <strong>if you want to raise an error when some values are empty,<\/strong><strong><br><\/strong><strong>you can use throw new Error(&#8220;Your custom message here&#8221;).<\/strong><strong><br><\/strong>This lets you show a clear message about what went wrong,<br>making it easier to fix the problem.<\/p>\n\n\n\n<p>Let\u2019s take a look:<\/p>\n\n\n\n<p>A try block helps make error handling easier by<br>putting all the checks for line of code in one place.<br>This way, you don\u2019t need to write multiple if statements for different errors.<br><br><strong>Centralized Management<\/strong>:<br>If something goes wrong inside the try block,<br>then an error will be triggered<br>which will be caught in the catch block.<br>Instead of handling each error separately,<br>the catch block provides a single place to manage<br>all errors.<\/p>\n\n\n\n<p>For example&nbsp;<\/p>\n\n\n\n<p>The errors are thrown while accessing name and age<br>can be accessed in the catch block.<br>We can use this error to notify the user about<br>the error or use it for creating error reports.<\/p>\n\n\n\n<p>It deals with the error, without stopping the entire application.<\/p>\n\n\n\n<p>Let\u2019s take a look at the code:<\/p>\n\n\n\n<p><em>Will show the code implementation of error handling using throw.<\/em><\/p>\n\n\n\n<p><strong><em>Advantages:<\/em><\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><em>Cleaner Code:<\/em><\/strong><em> Keeps error handling in one place, reducing repetitive code.<\/em><\/li>\n\n\n\n<li><strong><em>Automatic Flow Control:<\/em><\/strong><em> The <\/em><em>catch<\/em><em> block handles errors thrown in the <\/em><em>try<\/em><em> block automatically.<\/em><\/li>\n\n\n\n<li><strong><em>Easier Management:<\/em><\/strong><em> Manages all errors in one spot, making the code easier to maintain.<\/em><\/li>\n<\/ul>\n\n\n\n<p>Using a try block makes error handling more efficient and organized.<\/p>\n\n\n\n<p><strong>Finally:<\/strong><\/p>\n\n\n\n<p>Now we want to keep track of how many times the form is submitted.<strong><br><\/strong>Also&nbsp; need to show a list of all the information submitted through the form.<br>We can do this using&nbsp; a special section of the code called finally<br>This is&nbsp; update the submission count and list of entries<br>, even if there are mistakes during submission.<\/p>\n\n\n\n<p><br>Let\u2019s understand <strong>how It Works:<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>The user fills out the form with a name and age and clicks the &#8220;Submit&#8221; button.<\/li>\n\n\n\n<li>The code checks if the name and age are valid.<\/li>\n\n\n\n<li>If everything is correct, the entry is added to the list of entries and counts are updated for successful submissions.<\/li>\n\n\n\n<li>If there are any issues (like missing name or invalid age), the error is caught and the counts for failed submissions are updated.<\/li>\n\n\n\n<li>&nbsp;The finally block runs no matter if the try block had an error or not.<\/li>\n\n\n\n<li>&nbsp;It updates the total form submissions.<\/li>\n<\/ol>\n\n\n\n<p><strong>Will show the code:<\/strong><\/p>\n\n\n\n<p><strong>Let\u2019s say we have initialized a variable called totalSubmission =0 and it get\u2019s incremented with every submission. <\/strong><strong><br><\/strong><strong><br><\/strong><strong>finally {<\/strong><\/p>\n\n\n\n<p><strong>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/ Always update counters and list<\/strong><\/p>\n\n\n\n<p><strong>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;totalSubmissions++;<\/strong><\/p>\n\n\n\n<p><strong>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;document.getElementById(&#8216;totalCount&#8217;).textContent = totalSubmissions;<\/strong><\/p>\n\n\n\n<p><strong>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/ Update the list of all entries<\/strong><\/p>\n\n\n\n<p><strong>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;updateEntriesList();<\/strong><\/p>\n\n\n\n<p><strong>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<\/strong><\/p>\n\n\n\n<p><strong>Let\u2019s take a look.<\/strong><\/p>\n\n\n\n<p>This setup keeps everything organized and count the form submission whether submissions are successful or not.<\/p>\n\n\n\n<p><strong>Summary<\/strong>:<\/p>\n\n\n\n<p>Handling errors is like having a backup plan.&nbsp;<\/p>\n\n\n\n<p>Try and catch help you fix problems without stopping your app,&nbsp;<\/p>\n\n\n\n<p>and finally makes sure everything is cleaned up.<\/p>\n\n\n\n<p>This way, your app stays smooth and reliable.<br>Thanks for being here today, and keep coding!<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Enroll in Sharpener NOW !<\/h2>\n\n\n\n<p>At&nbsp;<strong>Sharpener Tech<\/strong>, we don\u2019t just teach, we transform<strong>&nbsp;careers<\/strong>! Our&nbsp;<a href=\"https:\/\/www.sharpener.tech\/\"><strong>Pay After Placement<\/strong><\/a>&nbsp;model ensures&nbsp;<strong>ZERO financial risk<\/strong>&nbsp;you pay only when you land a high-paying job. With&nbsp;<strong>industry-driven Full Stack Development training<\/strong>, hands-on projects, and expert mentorship, we&nbsp;<strong>guarantee your success<\/strong>&nbsp;in the tech world. Don\u2019t just dream&nbsp;<strong>BUILD your future with Sharpener Tech!<\/strong>&nbsp;&nbsp;<strong>Apply now and start your journey!<\/strong><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Sharpenerian\u2019s work at the best companies!<\/h2>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" width=\"1024\" height=\"429\" src=\"https:\/\/www.wordpress-prod.sharpener.tech\/wp-content\/uploads\/2025\/05\/Sharpener-works--1024x429.png\" alt=\"Sharpenerians work at the best companies\" class=\"wp-image-1059\" srcset=\"https:\/\/sharpener-wordpress.s3.ap-south-1.amazonaws.com\/blog\/wp-content\/uploads\/2025\/05\/13092711\/Sharpener-works--1024x429.png 1024w, https:\/\/sharpener-wordpress.s3.ap-south-1.amazonaws.com\/blog\/wp-content\/uploads\/2025\/05\/13092711\/Sharpener-works--300x126.png 300w, https:\/\/sharpener-wordpress.s3.ap-south-1.amazonaws.com\/blog\/wp-content\/uploads\/2025\/05\/13092711\/Sharpener-works--768x321.png 768w, https:\/\/sharpener-wordpress.s3.ap-south-1.amazonaws.com\/blog\/wp-content\/uploads\/2025\/05\/13092711\/Sharpener-works-.png 1534w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<div class=\"wp-block-buttons is-content-justification-center is-layout-flex wp-container-core-buttons-is-layout-16018d1d wp-block-buttons-is-layout-flex\">\n<div class=\"wp-block-button\"><a class=\"wp-block-button__link wp-element-button\" href=\"https:\/\/student.sharpener.tech\/register?blogName=error-handling-try-catch-finally-block\">Register Now<\/a><\/div>\n<\/div>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hello everyone. I hope you are doing well. I am Julian Toppo and&nbsp;today we will learn more about error handling and it\u2019s implementation. We will also learn how we handled&hellip;<\/p>\n","protected":false},"author":2,"featured_media":826,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[24],"tags":[],"class_list":["post-823","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tips"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.6 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Error Handling -(Try-catch-finally-block)<\/title>\n<meta name=\"description\" content=\"Learn how error handling works in JavaScript using try-catch-finally blocks. Understand how to catch exceptions, clean up code, and prevent app crashes with simple examples.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.sharpener.tech\/blog\/error-handling-try-catch-finally-block\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Error Handling -(Try-catch-finally-block)\" \/>\n<meta property=\"og:description\" content=\"Learn how error handling works in JavaScript using try-catch-finally blocks. Understand how to catch exceptions, clean up code, and prevent app crashes with simple examples.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.sharpener.tech\/blog\/error-handling-try-catch-finally-block\/\" \/>\n<meta property=\"og:site_name\" content=\"Sharpener Tech\" \/>\n<meta property=\"article:published_time\" content=\"2025-05-06T07:25:17+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-05-24T14:25:56+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/sharpener-wordpress.s3.ap-south-1.amazonaws.com\/blog\/wp-content\/uploads\/2025\/05\/06073846\/Error-Handling-Try-catch-finally-block.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1536\" \/>\n\t<meta property=\"og:image:height\" content=\"1024\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Rajesh\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Rajesh\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"7 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.sharpener.tech\\\/blog\\\/error-handling-try-catch-finally-block\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.sharpener.tech\\\/blog\\\/error-handling-try-catch-finally-block\\\/\"},\"author\":{\"name\":\"Rajesh\",\"@id\":\"https:\\\/\\\/www.sharpener.tech\\\/blog\\\/#\\\/schema\\\/person\\\/2be016d57f83e697eac0258e669d499b\"},\"headline\":\"Error Handling -(Try-catch-finally-block)\",\"datePublished\":\"2025-05-06T07:25:17+00:00\",\"dateModified\":\"2025-05-24T14:25:56+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.sharpener.tech\\\/blog\\\/error-handling-try-catch-finally-block\\\/\"},\"wordCount\":1399,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/www.sharpener.tech\\\/blog\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/www.sharpener.tech\\\/blog\\\/error-handling-try-catch-finally-block\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/sharpener-wordpress.s3.ap-south-1.amazonaws.com\\\/blog\\\/wp-content\\\/uploads\\\/2025\\\/05\\\/06073846\\\/Error-Handling-Try-catch-finally-block.jpg\",\"articleSection\":[\"Tips\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.sharpener.tech\\\/blog\\\/error-handling-try-catch-finally-block\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.sharpener.tech\\\/blog\\\/error-handling-try-catch-finally-block\\\/\",\"url\":\"https:\\\/\\\/www.sharpener.tech\\\/blog\\\/error-handling-try-catch-finally-block\\\/\",\"name\":\"Error Handling -(Try-catch-finally-block)\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.sharpener.tech\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.sharpener.tech\\\/blog\\\/error-handling-try-catch-finally-block\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.sharpener.tech\\\/blog\\\/error-handling-try-catch-finally-block\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/sharpener-wordpress.s3.ap-south-1.amazonaws.com\\\/blog\\\/wp-content\\\/uploads\\\/2025\\\/05\\\/06073846\\\/Error-Handling-Try-catch-finally-block.jpg\",\"datePublished\":\"2025-05-06T07:25:17+00:00\",\"dateModified\":\"2025-05-24T14:25:56+00:00\",\"description\":\"Learn how error handling works in JavaScript using try-catch-finally blocks. Understand how to catch exceptions, clean up code, and prevent app crashes with simple examples.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.sharpener.tech\\\/blog\\\/error-handling-try-catch-finally-block\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.sharpener.tech\\\/blog\\\/error-handling-try-catch-finally-block\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.sharpener.tech\\\/blog\\\/error-handling-try-catch-finally-block\\\/#primaryimage\",\"url\":\"https:\\\/\\\/sharpener-wordpress.s3.ap-south-1.amazonaws.com\\\/blog\\\/wp-content\\\/uploads\\\/2025\\\/05\\\/06073846\\\/Error-Handling-Try-catch-finally-block.jpg\",\"contentUrl\":\"https:\\\/\\\/sharpener-wordpress.s3.ap-south-1.amazonaws.com\\\/blog\\\/wp-content\\\/uploads\\\/2025\\\/05\\\/06073846\\\/Error-Handling-Try-catch-finally-block.jpg\",\"width\":1536,\"height\":1024,\"caption\":\"Error Handling -(Try-catch-finally-block)\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.sharpener.tech\\\/blog\\\/error-handling-try-catch-finally-block\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.sharpener.tech\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Error Handling -(Try-catch-finally-block)\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.sharpener.tech\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/www.sharpener.tech\\\/blog\\\/\",\"name\":\"Sharpener Tech\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.sharpener.tech\\\/blog\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.sharpener.tech\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.sharpener.tech\\\/blog\\\/#organization\",\"name\":\"Sharpener Tech\",\"url\":\"https:\\\/\\\/www.sharpener.tech\\\/blog\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.sharpener.tech\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/wordpress-prod.sharpener.tech\\\/wp-content\\\/uploads\\\/2026\\\/05\\\/Sharpener_logo-removebg-preview.png\",\"contentUrl\":\"https:\\\/\\\/wordpress-prod.sharpener.tech\\\/wp-content\\\/uploads\\\/2026\\\/05\\\/Sharpener_logo-removebg-preview.png\",\"width\":187,\"height\":62,\"caption\":\"Sharpener Tech\"},\"image\":{\"@id\":\"https:\\\/\\\/www.sharpener.tech\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.sharpener.tech\\\/blog\\\/#\\\/schema\\\/person\\\/2be016d57f83e697eac0258e669d499b\",\"name\":\"Rajesh\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/79394324a9c92c934544e2087155c65b4faa4d5209211f290ba72064fc62aa61?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/79394324a9c92c934544e2087155c65b4faa4d5209211f290ba72064fc62aa61?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/79394324a9c92c934544e2087155c65b4faa4d5209211f290ba72064fc62aa61?s=96&d=mm&r=g\",\"caption\":\"Rajesh\"},\"sameAs\":[\"https:\\\/\\\/www.wordpress-prod.sharpener.tech\"],\"url\":\"https:\\\/\\\/www.sharpener.tech\\\/blog\\\/author\\\/rajesh\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Error Handling -(Try-catch-finally-block)","description":"Learn how error handling works in JavaScript using try-catch-finally blocks. Understand how to catch exceptions, clean up code, and prevent app crashes with simple examples.","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:\/\/www.sharpener.tech\/blog\/error-handling-try-catch-finally-block\/","og_locale":"en_US","og_type":"article","og_title":"Error Handling -(Try-catch-finally-block)","og_description":"Learn how error handling works in JavaScript using try-catch-finally blocks. Understand how to catch exceptions, clean up code, and prevent app crashes with simple examples.","og_url":"https:\/\/www.sharpener.tech\/blog\/error-handling-try-catch-finally-block\/","og_site_name":"Sharpener Tech","article_published_time":"2025-05-06T07:25:17+00:00","article_modified_time":"2025-05-24T14:25:56+00:00","og_image":[{"width":1536,"height":1024,"url":"https:\/\/sharpener-wordpress.s3.ap-south-1.amazonaws.com\/blog\/wp-content\/uploads\/2025\/05\/06073846\/Error-Handling-Try-catch-finally-block.jpg","type":"image\/jpeg"}],"author":"Rajesh","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Rajesh","Est. reading time":"7 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.sharpener.tech\/blog\/error-handling-try-catch-finally-block\/#article","isPartOf":{"@id":"https:\/\/www.sharpener.tech\/blog\/error-handling-try-catch-finally-block\/"},"author":{"name":"Rajesh","@id":"https:\/\/www.sharpener.tech\/blog\/#\/schema\/person\/2be016d57f83e697eac0258e669d499b"},"headline":"Error Handling -(Try-catch-finally-block)","datePublished":"2025-05-06T07:25:17+00:00","dateModified":"2025-05-24T14:25:56+00:00","mainEntityOfPage":{"@id":"https:\/\/www.sharpener.tech\/blog\/error-handling-try-catch-finally-block\/"},"wordCount":1399,"commentCount":0,"publisher":{"@id":"https:\/\/www.sharpener.tech\/blog\/#organization"},"image":{"@id":"https:\/\/www.sharpener.tech\/blog\/error-handling-try-catch-finally-block\/#primaryimage"},"thumbnailUrl":"https:\/\/sharpener-wordpress.s3.ap-south-1.amazonaws.com\/blog\/wp-content\/uploads\/2025\/05\/06073846\/Error-Handling-Try-catch-finally-block.jpg","articleSection":["Tips"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.sharpener.tech\/blog\/error-handling-try-catch-finally-block\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.sharpener.tech\/blog\/error-handling-try-catch-finally-block\/","url":"https:\/\/www.sharpener.tech\/blog\/error-handling-try-catch-finally-block\/","name":"Error Handling -(Try-catch-finally-block)","isPartOf":{"@id":"https:\/\/www.sharpener.tech\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.sharpener.tech\/blog\/error-handling-try-catch-finally-block\/#primaryimage"},"image":{"@id":"https:\/\/www.sharpener.tech\/blog\/error-handling-try-catch-finally-block\/#primaryimage"},"thumbnailUrl":"https:\/\/sharpener-wordpress.s3.ap-south-1.amazonaws.com\/blog\/wp-content\/uploads\/2025\/05\/06073846\/Error-Handling-Try-catch-finally-block.jpg","datePublished":"2025-05-06T07:25:17+00:00","dateModified":"2025-05-24T14:25:56+00:00","description":"Learn how error handling works in JavaScript using try-catch-finally blocks. Understand how to catch exceptions, clean up code, and prevent app crashes with simple examples.","breadcrumb":{"@id":"https:\/\/www.sharpener.tech\/blog\/error-handling-try-catch-finally-block\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.sharpener.tech\/blog\/error-handling-try-catch-finally-block\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.sharpener.tech\/blog\/error-handling-try-catch-finally-block\/#primaryimage","url":"https:\/\/sharpener-wordpress.s3.ap-south-1.amazonaws.com\/blog\/wp-content\/uploads\/2025\/05\/06073846\/Error-Handling-Try-catch-finally-block.jpg","contentUrl":"https:\/\/sharpener-wordpress.s3.ap-south-1.amazonaws.com\/blog\/wp-content\/uploads\/2025\/05\/06073846\/Error-Handling-Try-catch-finally-block.jpg","width":1536,"height":1024,"caption":"Error Handling -(Try-catch-finally-block)"},{"@type":"BreadcrumbList","@id":"https:\/\/www.sharpener.tech\/blog\/error-handling-try-catch-finally-block\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.sharpener.tech\/blog\/"},{"@type":"ListItem","position":2,"name":"Error Handling -(Try-catch-finally-block)"}]},{"@type":"WebSite","@id":"https:\/\/www.sharpener.tech\/blog\/#website","url":"https:\/\/www.sharpener.tech\/blog\/","name":"Sharpener Tech","description":"","publisher":{"@id":"https:\/\/www.sharpener.tech\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.sharpener.tech\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.sharpener.tech\/blog\/#organization","name":"Sharpener Tech","url":"https:\/\/www.sharpener.tech\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.sharpener.tech\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/wordpress-prod.sharpener.tech\/wp-content\/uploads\/2026\/05\/Sharpener_logo-removebg-preview.png","contentUrl":"https:\/\/wordpress-prod.sharpener.tech\/wp-content\/uploads\/2026\/05\/Sharpener_logo-removebg-preview.png","width":187,"height":62,"caption":"Sharpener Tech"},"image":{"@id":"https:\/\/www.sharpener.tech\/blog\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/www.sharpener.tech\/blog\/#\/schema\/person\/2be016d57f83e697eac0258e669d499b","name":"Rajesh","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/79394324a9c92c934544e2087155c65b4faa4d5209211f290ba72064fc62aa61?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/79394324a9c92c934544e2087155c65b4faa4d5209211f290ba72064fc62aa61?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/79394324a9c92c934544e2087155c65b4faa4d5209211f290ba72064fc62aa61?s=96&d=mm&r=g","caption":"Rajesh"},"sameAs":["https:\/\/www.wordpress-prod.sharpener.tech"],"url":"https:\/\/www.sharpener.tech\/blog\/author\/rajesh\/"}]}},"_links":{"self":[{"href":"https:\/\/www.sharpener.tech\/blog\/wp-json\/wp\/v2\/posts\/823","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.sharpener.tech\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.sharpener.tech\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.sharpener.tech\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sharpener.tech\/blog\/wp-json\/wp\/v2\/comments?post=823"}],"version-history":[{"count":7,"href":"https:\/\/www.sharpener.tech\/blog\/wp-json\/wp\/v2\/posts\/823\/revisions"}],"predecessor-version":[{"id":1389,"href":"https:\/\/www.sharpener.tech\/blog\/wp-json\/wp\/v2\/posts\/823\/revisions\/1389"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.sharpener.tech\/blog\/wp-json\/wp\/v2\/media\/826"}],"wp:attachment":[{"href":"https:\/\/www.sharpener.tech\/blog\/wp-json\/wp\/v2\/media?parent=823"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.sharpener.tech\/blog\/wp-json\/wp\/v2\/categories?post=823"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.sharpener.tech\/blog\/wp-json\/wp\/v2\/tags?post=823"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}