[{"data":1,"prerenderedAt":378},["ShallowReactive",2],{"term-t\u002Ftry-catch":3,"related-t\u002Ftry-catch":360},{"id":4,"title":5,"acronym":6,"body":7,"category":340,"description":341,"difficulty":342,"extension":343,"letter":344,"meta":345,"navigation":346,"path":347,"related":348,"seo":354,"sitemap":355,"stem":358,"subcategory":6,"__hash__":359},"terms\u002Fterms\u002Ft\u002Ftry-catch.md","Try\u002FCatch",null,{"type":8,"value":9,"toc":334},"minimark",[10,15,19,23,39,43,323,327,330],[11,12,14],"h2",{"id":13},"eli5-the-vibe-check","ELI5 — The Vibe Check",[16,17,18],"p",{},"Try\u002Fcatch is your safety net. You put risky code in the 'try' box, and if it blows up, the 'catch' box catches the explosion and handles it gracefully instead of crashing. It is like trying to open a jar — try it, and if you cannot, catch the situation and ask for help.",[11,20,22],{"id":21},"real-talk","Real Talk",[16,24,25,26,30,31,34,35,38],{},"Try\u002Fcatch is a control flow structure for exception handling. Code in the ",[27,28,29],"code",{},"try"," block executes normally; if an exception is thrown, execution jumps immediately to the ",[27,32,33],{},"catch"," block, which receives the error object. An optional ",[27,36,37],{},"finally"," block always executes regardless of success or failure, commonly used for cleanup (closing files, releasing resources).",[11,40,42],{"id":41},"show-me-the-code","Show Me The Code",[44,45,50],"pre",{"className":46,"code":47,"language":48,"meta":49,"style":49},"language-javascript shiki shiki-themes material-theme-lighter material-theme material-theme-palenight","async function fetchUser(id) {\n  try {\n    const response = await fetch(`\u002Fapi\u002Fusers\u002F${id}`);\n    if (!response.ok) throw new Error(`HTTP ${response.status}`);\n    return await response.json();\n  } catch (error) {\n    console.error(\"Failed to fetch user:\", error.message);\n    return null; \u002F\u002F graceful fallback\n  } finally {\n    console.log(\"Request completed\"); \u002F\u002F always runs\n  }\n}\n","javascript","",[27,51,52,82,91,133,188,208,227,262,274,284,311,317],{"__ignoreMap":49},[53,54,57,61,64,68,72,76,79],"span",{"class":55,"line":56},"line",1,[53,58,60],{"class":59},"spNyl","async",[53,62,63],{"class":59}," function",[53,65,67],{"class":66},"s2Zo4"," fetchUser",[53,69,71],{"class":70},"sMK4o","(",[53,73,75],{"class":74},"sHdIc","id",[53,77,78],{"class":70},")",[53,80,81],{"class":70}," {\n",[53,83,85,89],{"class":55,"line":84},2,[53,86,88],{"class":87},"s7zQu","  try",[53,90,81],{"class":70},[53,92,94,97,101,104,107,110,113,116,120,123,125,128,130],{"class":55,"line":93},3,[53,95,96],{"class":59},"    const",[53,98,100],{"class":99},"sTEyZ"," response",[53,102,103],{"class":70}," =",[53,105,106],{"class":87}," await",[53,108,109],{"class":66}," fetch",[53,111,71],{"class":112},"swJcz",[53,114,115],{"class":70},"`",[53,117,119],{"class":118},"sfazB","\u002Fapi\u002Fusers\u002F",[53,121,122],{"class":70},"${",[53,124,75],{"class":99},[53,126,127],{"class":70},"}`",[53,129,78],{"class":112},[53,131,132],{"class":70},";\n",[53,134,136,139,142,145,148,151,154,157,160,163,166,168,170,173,175,177,179,182,184,186],{"class":55,"line":135},4,[53,137,138],{"class":87},"    if",[53,140,141],{"class":112}," (",[53,143,144],{"class":70},"!",[53,146,147],{"class":99},"response",[53,149,150],{"class":70},".",[53,152,153],{"class":99},"ok",[53,155,156],{"class":112},") ",[53,158,159],{"class":87},"throw",[53,161,162],{"class":70}," new",[53,164,165],{"class":66}," Error",[53,167,71],{"class":112},[53,169,115],{"class":70},[53,171,172],{"class":118},"HTTP ",[53,174,122],{"class":70},[53,176,147],{"class":99},[53,178,150],{"class":70},[53,180,181],{"class":99},"status",[53,183,127],{"class":70},[53,185,78],{"class":112},[53,187,132],{"class":70},[53,189,191,194,196,198,200,203,206],{"class":55,"line":190},5,[53,192,193],{"class":87},"    return",[53,195,106],{"class":87},[53,197,100],{"class":99},[53,199,150],{"class":70},[53,201,202],{"class":66},"json",[53,204,205],{"class":112},"()",[53,207,132],{"class":70},[53,209,211,214,217,219,222,224],{"class":55,"line":210},6,[53,212,213],{"class":70},"  }",[53,215,216],{"class":87}," catch",[53,218,141],{"class":112},[53,220,221],{"class":99},"error",[53,223,156],{"class":112},[53,225,226],{"class":70},"{\n",[53,228,230,233,235,237,239,242,245,247,250,253,255,258,260],{"class":55,"line":229},7,[53,231,232],{"class":99},"    console",[53,234,150],{"class":70},[53,236,221],{"class":66},[53,238,71],{"class":112},[53,240,241],{"class":70},"\"",[53,243,244],{"class":118},"Failed to fetch user:",[53,246,241],{"class":70},[53,248,249],{"class":70},",",[53,251,252],{"class":99}," error",[53,254,150],{"class":70},[53,256,257],{"class":99},"message",[53,259,78],{"class":112},[53,261,132],{"class":70},[53,263,265,267,270],{"class":55,"line":264},8,[53,266,193],{"class":87},[53,268,269],{"class":70}," null;",[53,271,273],{"class":272},"sHwdD"," \u002F\u002F graceful fallback\n",[53,275,277,279,282],{"class":55,"line":276},9,[53,278,213],{"class":70},[53,280,281],{"class":87}," finally",[53,283,81],{"class":70},[53,285,287,289,291,294,296,298,301,303,305,308],{"class":55,"line":286},10,[53,288,232],{"class":99},[53,290,150],{"class":70},[53,292,293],{"class":66},"log",[53,295,71],{"class":112},[53,297,241],{"class":70},[53,299,300],{"class":118},"Request completed",[53,302,241],{"class":70},[53,304,78],{"class":112},[53,306,307],{"class":70},";",[53,309,310],{"class":272}," \u002F\u002F always runs\n",[53,312,314],{"class":55,"line":313},11,[53,315,316],{"class":70},"  }\n",[53,318,320],{"class":55,"line":319},12,[53,321,322],{"class":70},"}\n",[11,324,326],{"id":325},"when-youll-hear-this","When You'll Hear This",[16,328,329],{},"\"Wrap the API call in a try\u002Fcatch.\" \u002F \"The try\u002Fcatch swallowed the error — log it!\"",[331,332,333],"style",{},"html pre.shiki code .spNyl, html code.shiki .spNyl{--shiki-light:#9C3EDA;--shiki-default:#C792EA;--shiki-dark:#C792EA}html pre.shiki code .s2Zo4, html code.shiki .s2Zo4{--shiki-light:#6182B8;--shiki-default:#82AAFF;--shiki-dark:#82AAFF}html pre.shiki code .sMK4o, html code.shiki .sMK4o{--shiki-light:#39ADB5;--shiki-default:#89DDFF;--shiki-dark:#89DDFF}html pre.shiki code .sHdIc, html code.shiki .sHdIc{--shiki-light:#90A4AE;--shiki-light-font-style:italic;--shiki-default:#EEFFFF;--shiki-default-font-style:italic;--shiki-dark:#BABED8;--shiki-dark-font-style:italic}html pre.shiki code .s7zQu, html code.shiki .s7zQu{--shiki-light:#39ADB5;--shiki-light-font-style:italic;--shiki-default:#89DDFF;--shiki-default-font-style:italic;--shiki-dark:#89DDFF;--shiki-dark-font-style:italic}html pre.shiki code .sTEyZ, html code.shiki .sTEyZ{--shiki-light:#90A4AE;--shiki-default:#EEFFFF;--shiki-dark:#BABED8}html pre.shiki code .swJcz, html code.shiki .swJcz{--shiki-light:#E53935;--shiki-default:#F07178;--shiki-dark:#F07178}html pre.shiki code .sfazB, html code.shiki .sfazB{--shiki-light:#91B859;--shiki-default:#C3E88D;--shiki-dark:#C3E88D}html pre.shiki code .sHwdD, html code.shiki .sHwdD{--shiki-light:#90A4AE;--shiki-light-font-style:italic;--shiki-default:#546E7A;--shiki-default-font-style:italic;--shiki-dark:#676E95;--shiki-dark-font-style:italic}html .light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html.light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}",{"title":49,"searchDepth":84,"depth":84,"links":335},[336,337,338,339],{"id":13,"depth":84,"text":14},{"id":21,"depth":84,"text":22},{"id":41,"depth":84,"text":42},{"id":325,"depth":84,"text":326},"general","Try\u002Fcatch is your safety net. You put risky code in the 'try' box, and if it blows up, the 'catch' box catches the explosion and handles it gracefully inst...","beginner","md","t",{},true,"\u002Fterms\u002Ft\u002Ftry-catch",[349,350,351,352,353],"Exception","Error Handling","Error","Async\u002FAwait","Logging",{"title":5,"description":341},{"changefreq":356,"priority":357},"weekly",0.7,"terms\u002Ft\u002Ftry-catch","iia-7icVFaZ6OloEkdf1V8SE2bBgrRzYC6yN-4ro_to",[361,366,369,372,375],{"title":352,"path":362,"acronym":6,"category":363,"difficulty":364,"description":365},"\u002Fterms\u002Fa\u002Fasync-await","frontend","intermediate","Async\u002Fawait is syntactic sugar that makes Promises look like normal, readable code. Instead of chaining .then().then().",{"title":351,"path":367,"acronym":6,"category":340,"difficulty":342,"description":368},"\u002Fterms\u002Fe\u002Ferror","An error is when your program says 'I cannot do that' and either stops or complains loudly. It is the computer's way of telling you something went wrong.",{"title":350,"path":370,"acronym":6,"category":340,"difficulty":364,"description":371},"\u002Fterms\u002Fe\u002Ferror-handling","Error handling is the art of planning for things to go wrong and dealing with them gracefully instead of letting everything catch fire.",{"title":349,"path":373,"acronym":6,"category":340,"difficulty":364,"description":374},"\u002Fterms\u002Fe\u002Fexception","An exception is a special kind of error that 'throws' itself up through your code like a hot potato, looking for someone to catch it.",{"title":353,"path":376,"acronym":6,"category":340,"difficulty":342,"description":377},"\u002Fterms\u002Fl\u002Flogging","Logging is writing a diary for your program.",1776518320142]