{"id":151,"date":"2026-05-06T08:59:29","date_gmt":"2026-05-06T08:59:29","guid":{"rendered":"https:\/\/gigz.pk\/javascript\/?post_type=lesson&#038;p=151"},"modified":"2026-05-06T08:59:30","modified_gmt":"2026-05-06T08:59:30","slug":"try-catch","status":"publish","type":"lesson","link":"https:\/\/gigz.pk\/javascript\/?lesson=try-catch","title":{"rendered":"try, catch"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Introduction<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In JavaScript, error handling is an important concept that helps developers manage unexpected issues in their code. The try and catch statements allow you to test a block of code for errors and handle them gracefully without breaking the entire application. This improves user experience and makes applications more reliable.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Understanding Try and Catch<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The try block is used to wrap code that might produce an error during execution. If an error occurs, JavaScript immediately stops running the code inside the try block and transfers control to the catch block. The catch block then handles the error and prevents the program from crashing.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Syntax<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>try {<br>  \/\/ code that may cause an error<br>} catch (error) {<br>  \/\/ code to handle the error<br>}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">How It Works<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">JavaScript executes the code inside the try block first. If no error occurs, the catch block is ignored. If an error occurs, execution jumps to the catch block where the error can be handled or displayed.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Example<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>try {<br>  let result = undefinedVariable + 5;<br>  console.log(result);<br>} catch (error) {<br>  console.log(\"An error occurred:\", error.message);<br>}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">In this example, the variable is not defined, so an error occurs. Instead of stopping the program, the catch block handles the error and prints a message.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Using Finally<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You can also use the finally block with try and catch. The code inside finally always runs, whether an error occurs or not. It is often used for cleanup tasks.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>try {<br>  console.log(\"Trying code\");<br>} catch (error) {<br>  console.log(\"Error occurred\");<br>} finally {<br>  console.log(\"This always runs\");<br>}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Throwing Custom Errors<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">JavaScript allows developers to create custom errors using the throw statement. This is useful when you want to define your own error conditions.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>try {<br>  let age = -5;<br>  if (age &lt; 0) {<br>    throw new Error(\"Age cannot be negative\");<br>  }<br>} catch (error) {<br>  console.log(error.message);<br>}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Best Practices<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Use try and catch only where errors are likely to occur<br>Do not overuse error handling as it can affect performance<br>Always provide meaningful error messages<br>Use finally for cleanup tasks like closing connections<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Conclusion<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The try and catch statements are essential tools in JavaScript for handling errors effectively. They help maintain smooth program execution and improve the reliability of your applications. Learning how to use them properly is important for building professional and stable software.<\/p>\n\n\n<div class=\"yoast-breadcrumbs\"><span><span><a href=\"https:\/\/gigz.pk\/javascript\">Home<\/a><\/span> \u00bb <span class=\"breadcrumb_last\" aria-current=\"page\">Advanced JavaScript > Error Handling > try, catch<\/span><\/span><\/div>\n\n\n<div class=\"schema-faq wp-block-yoast-faq-block\"><div class=\"schema-faq-section\" id=\"faq-question-1778057950217\"><strong class=\"schema-faq-question\"><\/strong> <p class=\"schema-faq-answer\"><\/p> <\/div> <\/div>\n\n\n\n<div class=\"schema-faq wp-block-yoast-faq-block\"><div class=\"schema-faq-section\" id=\"faq-question-1778057950020\"><strong class=\"schema-faq-question\"><\/strong> <p class=\"schema-faq-answer\"><\/p> <\/div> <\/div>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"menu_order":0,"template":"","class_list":["post-151","lesson","type-lesson","status-publish","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.6 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>try, catch - Learn JavaScript with GIGZ.PK<\/title>\n<meta name=\"description\" content=\"Learn JavaScript try catch to handle errors, prevent crashes, and build reliable applications with simple examples and best practices\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/gigz.pk\/javascript\/?lesson=try-catch\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"try, catch - Learn JavaScript with GIGZ.PK\" \/>\n<meta property=\"og:description\" content=\"Learn JavaScript try catch to handle errors, prevent crashes, and build reliable applications with simple examples and best practices\" \/>\n<meta property=\"og:url\" content=\"https:\/\/gigz.pk\/javascript\/?lesson=try-catch\" \/>\n<meta property=\"og:site_name\" content=\"Learn JavaScript with GIGZ.PK\" \/>\n<meta property=\"article:modified_time\" content=\"2026-05-06T08:59:30+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":[\"WebPage\",\"FAQPage\"],\"@id\":\"https:\\\/\\\/gigz.pk\\\/javascript\\\/?lesson=try-catch\",\"url\":\"https:\\\/\\\/gigz.pk\\\/javascript\\\/?lesson=try-catch\",\"name\":\"try, catch - Learn JavaScript with GIGZ.PK\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/javascript\\\/#website\"},\"datePublished\":\"2026-05-06T08:59:29+00:00\",\"dateModified\":\"2026-05-06T08:59:30+00:00\",\"description\":\"Learn JavaScript try catch to handle errors, prevent crashes, and build reliable applications with simple examples and best practices\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/javascript\\\/?lesson=try-catch#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/gigz.pk\\\/javascript\\\/?lesson=try-catch\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/gigz.pk\\\/javascript\\\/?lesson=try-catch#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/gigz.pk\\\/javascript\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Advanced JavaScript > Error Handling > try, catch\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/gigz.pk\\\/javascript\\\/#website\",\"url\":\"https:\\\/\\\/gigz.pk\\\/javascript\\\/\",\"name\":\"Learn JavaScript with GIGZ.PK\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/gigz.pk\\\/javascript\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"try, catch - Learn JavaScript with GIGZ.PK","description":"Learn JavaScript try catch to handle errors, prevent crashes, and build reliable applications with simple examples and best practices","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:\/\/gigz.pk\/javascript\/?lesson=try-catch","og_locale":"en_US","og_type":"article","og_title":"try, catch - Learn JavaScript with GIGZ.PK","og_description":"Learn JavaScript try catch to handle errors, prevent crashes, and build reliable applications with simple examples and best practices","og_url":"https:\/\/gigz.pk\/javascript\/?lesson=try-catch","og_site_name":"Learn JavaScript with GIGZ.PK","article_modified_time":"2026-05-06T08:59:30+00:00","twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":["WebPage","FAQPage"],"@id":"https:\/\/gigz.pk\/javascript\/?lesson=try-catch","url":"https:\/\/gigz.pk\/javascript\/?lesson=try-catch","name":"try, catch - Learn JavaScript with GIGZ.PK","isPartOf":{"@id":"https:\/\/gigz.pk\/javascript\/#website"},"datePublished":"2026-05-06T08:59:29+00:00","dateModified":"2026-05-06T08:59:30+00:00","description":"Learn JavaScript try catch to handle errors, prevent crashes, and build reliable applications with simple examples and best practices","breadcrumb":{"@id":"https:\/\/gigz.pk\/javascript\/?lesson=try-catch#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/gigz.pk\/javascript\/?lesson=try-catch"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/gigz.pk\/javascript\/?lesson=try-catch#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/gigz.pk\/javascript"},{"@type":"ListItem","position":2,"name":"Advanced JavaScript > Error Handling > try, catch"}]},{"@type":"WebSite","@id":"https:\/\/gigz.pk\/javascript\/#website","url":"https:\/\/gigz.pk\/javascript\/","name":"Learn JavaScript with GIGZ.PK","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/gigz.pk\/javascript\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/gigz.pk\/javascript\/index.php?rest_route=\/wp\/v2\/lesson\/151","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gigz.pk\/javascript\/index.php?rest_route=\/wp\/v2\/lesson"}],"about":[{"href":"https:\/\/gigz.pk\/javascript\/index.php?rest_route=\/wp\/v2\/types\/lesson"}],"wp:attachment":[{"href":"https:\/\/gigz.pk\/javascript\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=151"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}