[{"data":1,"prerenderedAt":2798},["ShallowReactive",2],{"doc-\u002Flearn\u002Fusing-try-except-else-and-finally-in-python":3},{"id":4,"title":5,"body":6,"description":2791,"extension":2792,"meta":2793,"navigation":942,"path":2794,"seo":2795,"stem":2796,"__hash__":2797},"content\u002Flearn\u002Fusing-try-except-else-and-finally-in-python.md","Using try, except, else, and finally in Python",{"type":7,"value":8,"toc":2745},"minimark",[9,13,17,22,166,181,185,211,218,224,227,233,249,252,319,322,335,353,361,366,373,376,379,395,397,451,453,465,471,521,524,527,579,586,591,599,602,614,619,621,699,701,716,719,733,742,747,752,755,769,771,841,843,858,874,876,957,959,977,981,986,991,994,1009,1011,1102,1104,1124,1128,1130,1144,1146,1238,1240,1254,1258,1267,1338,1340,1378,1382,1385,1407,1410,1433,1437,1440,1463,1563,1569,1600,1604,1631,1638,1642,1645,1669,1858,1865,1882,1889,1893,1917,1920,1994,2000,2007,2011,2017,2020,2118,2121,2218,2221,2228,2231,2234,2247,2250,2261,2270,2273,2285,2291,2297,2301,2304,2343,2346,2349,2353,2356,2393,2397,2400,2415,2472,2475,2501,2505,2509,2517,2526,2534,2541,2544,2550,2553,2560,2566,2568,2707,2711,2741],[10,11,5],"h1",{"id":12},"using-try-except-else-and-finally-in-python",[14,15,16],"p",{},"Learn how Python exception handling blocks work together so you can catch errors, run code only when no error happens, and always clean up resources.",[18,19,21],"h2",{"id":20},"quick-example","Quick example",[23,24,29],"pre",{"className":25,"code":26,"language":27,"meta":28,"style":28},"language-python shiki shiki-themes material-theme-lighter github-light github-dark","try:\n    number = int(input(\"Enter a number: \"))\nexcept ValueError:\n    print(\"Please enter a valid whole number.\")\nelse:\n    print(\"You entered:\", number)\nfinally:\n    print(\"Program finished.\")\n","python","",[30,31,32,45,82,93,111,119,142,150],"code",{"__ignoreMap":28},[33,34,37,41],"span",{"class":35,"line":36},"line",1,[33,38,40],{"class":39},"sVHd0","try",[33,42,44],{"class":43},"sP7_E",":\n",[33,46,48,52,56,60,63,67,69,73,77,79],{"class":35,"line":47},2,[33,49,51],{"class":50},"su5hD","    number ",[33,53,55],{"class":54},"smGrS","=",[33,57,59],{"class":58},"sZMiF"," int",[33,61,62],{"class":43},"(",[33,64,66],{"class":65},"sptTA","input",[33,68,62],{"class":43},[33,70,72],{"class":71},"sjJ54","\"",[33,74,76],{"class":75},"s_sjI","Enter a number: ",[33,78,72],{"class":71},[33,80,81],{"class":43},"))\n",[33,83,85,88,91],{"class":35,"line":84},3,[33,86,87],{"class":39},"except",[33,89,90],{"class":58}," ValueError",[33,92,44],{"class":43},[33,94,96,99,101,103,106,108],{"class":35,"line":95},4,[33,97,98],{"class":65},"    print",[33,100,62],{"class":43},[33,102,72],{"class":71},[33,104,105],{"class":75},"Please enter a valid whole number.",[33,107,72],{"class":71},[33,109,110],{"class":43},")\n",[33,112,114,117],{"class":35,"line":113},5,[33,115,116],{"class":39},"else",[33,118,44],{"class":43},[33,120,122,124,126,128,131,133,136,140],{"class":35,"line":121},6,[33,123,98],{"class":65},[33,125,62],{"class":43},[33,127,72],{"class":71},[33,129,130],{"class":75},"You entered:",[33,132,72],{"class":71},[33,134,135],{"class":43},",",[33,137,139],{"class":138},"slqww"," number",[33,141,110],{"class":43},[33,143,145,148],{"class":35,"line":144},7,[33,146,147],{"class":39},"finally",[33,149,44],{"class":43},[33,151,153,155,157,159,162,164],{"class":35,"line":152},8,[33,154,98],{"class":65},[33,156,62],{"class":43},[33,158,72],{"class":71},[33,160,161],{"class":75},"Program finished.",[33,163,72],{"class":71},[33,165,110],{"class":43},[14,167,168,169,171,172,174,175,177,178,180],{},"Use ",[30,170,40],{}," for risky code, ",[30,173,87],{}," for handling specific errors, ",[30,176,116],{}," for success-only code, and ",[30,179,147],{}," for cleanup code that must always run.",[18,182,184],{"id":183},"what-this-page-covers","What this page covers",[186,187,188,202,205,208],"ul",{},[189,190,191,192,194,195,194,197,199,200],"li",{},"Explain the purpose of ",[30,193,40],{},", ",[30,196,87],{},[30,198,116],{},", and ",[30,201,147],{},[189,203,204],{},"Show the order these blocks run",[189,206,207],{},"Help beginners choose the right block for each task",[189,209,210],{},"Focus on practical exception handling patterns",[18,212,214,215,217],{"id":213},"what-try-does","What ",[30,216,40],{}," does",[14,219,220,221,223],{},"Put code that might fail inside a ",[30,222,40],{}," block.",[14,225,226],{},"Python watches this block for exceptions. An exception is an error that happens while the program is running.",[14,228,229,230,232],{},"If an error happens inside ",[30,231,40],{},":",[186,234,235,241,246],{},[189,236,237,238,240],{},"Python stops running the rest of that ",[30,239,40],{}," block",[189,242,243,244,240],{},"Python looks for a matching ",[30,245,87],{},[189,247,248],{},"If it finds one, that handler runs",[14,250,251],{},"Example:",[23,253,255],{"className":25,"code":254,"language":27,"meta":28,"style":28},"try:\n    result = 10 \u002F 0\n    print(\"This line will not run.\")\nexcept ZeroDivisionError:\n    print(\"You cannot divide by zero.\")\n",[30,256,257,263,280,295,304],{"__ignoreMap":28},[33,258,259,261],{"class":35,"line":36},[33,260,40],{"class":39},[33,262,44],{"class":43},[33,264,265,268,270,274,277],{"class":35,"line":47},[33,266,267],{"class":50},"    result ",[33,269,55],{"class":54},[33,271,273],{"class":272},"srdBf"," 10",[33,275,276],{"class":54}," \u002F",[33,278,279],{"class":272}," 0\n",[33,281,282,284,286,288,291,293],{"class":35,"line":84},[33,283,98],{"class":65},[33,285,62],{"class":43},[33,287,72],{"class":71},[33,289,290],{"class":75},"This line will not run.",[33,292,72],{"class":71},[33,294,110],{"class":43},[33,296,297,299,302],{"class":35,"line":95},[33,298,87],{"class":39},[33,300,301],{"class":58}," ZeroDivisionError",[33,303,44],{"class":43},[33,305,306,308,310,312,315,317],{"class":35,"line":113},[33,307,98],{"class":65},[33,309,62],{"class":43},[33,311,72],{"class":71},[33,313,314],{"class":75},"You cannot divide by zero.",[33,316,72],{"class":71},[33,318,110],{"class":43},[14,320,321],{},"Output:",[23,323,325],{"className":25,"code":324,"language":27,"meta":28,"style":28},"You cannot divide by zero.\n",[30,326,327],{"__ignoreMap":28},[33,328,329,332],{"class":35,"line":36},[33,330,331],{"class":50},"You cannot divide by zero",[33,333,334],{"class":43},".\n",[14,336,337,338,341,342,345,346,349,350,352],{},"Here, ",[30,339,340],{},"10 \u002F 0"," raises a ",[30,343,344],{},"ZeroDivisionError",", so Python skips the ",[30,347,348],{},"print()"," inside ",[30,351,40],{},".",[14,354,355,356,352],{},"If you are new to exceptions, see ",[357,358,360],"a",{"href":359},"\u002Flearn\u002Fpython-errors-and-exceptions-explained\u002F","Python errors and exceptions explained",[18,362,214,364,217],{"id":363},"what-except-does",[30,365,87],{},[14,367,368,370,371,352],{},[30,369,87],{}," handles an error that happened in ",[30,372,40],{},[14,374,375],{},"You should catch specific exceptions when possible. This makes your code easier to understand and debug.",[14,377,378],{},"Common exception types include:",[186,380,381,386,390],{},[189,382,383],{},[30,384,385],{},"ValueError",[189,387,388],{},[30,389,344],{},[189,391,392],{},[30,393,394],{},"FileNotFoundError",[14,396,251],{},[23,398,400],{"className":25,"code":399,"language":27,"meta":28,"style":28},"try:\n    age = int(\"hello\")\nexcept ValueError:\n    print(\"That value cannot be converted to an integer.\")\n",[30,401,402,408,428,436],{"__ignoreMap":28},[33,403,404,406],{"class":35,"line":36},[33,405,40],{"class":39},[33,407,44],{"class":43},[33,409,410,413,415,417,419,421,424,426],{"class":35,"line":47},[33,411,412],{"class":50},"    age ",[33,414,55],{"class":54},[33,416,59],{"class":58},[33,418,62],{"class":43},[33,420,72],{"class":71},[33,422,423],{"class":75},"hello",[33,425,72],{"class":71},[33,427,110],{"class":43},[33,429,430,432,434],{"class":35,"line":84},[33,431,87],{"class":39},[33,433,90],{"class":58},[33,435,44],{"class":43},[33,437,438,440,442,444,447,449],{"class":35,"line":95},[33,439,98],{"class":65},[33,441,62],{"class":43},[33,443,72],{"class":71},[33,445,446],{"class":75},"That value cannot be converted to an integer.",[33,448,72],{"class":71},[33,450,110],{"class":43},[14,452,321],{},[23,454,456],{"className":25,"code":455,"language":27,"meta":28,"style":28},"That value cannot be converted to an integer.\n",[30,457,458],{"__ignoreMap":28},[33,459,460,463],{"class":35,"line":36},[33,461,462],{"class":50},"That value cannot be converted to an integer",[33,464,334],{"class":43},[14,466,467,468,470],{},"Try to avoid a bare ",[30,469,87],{}," like this:",[23,472,474],{"className":25,"code":473,"language":27,"meta":28,"style":28},"try:\n    age = int(\"hello\")\nexcept:\n    print(\"Something went wrong.\")\n",[30,475,476,482,500,506],{"__ignoreMap":28},[33,477,478,480],{"class":35,"line":36},[33,479,40],{"class":39},[33,481,44],{"class":43},[33,483,484,486,488,490,492,494,496,498],{"class":35,"line":47},[33,485,412],{"class":50},[33,487,55],{"class":54},[33,489,59],{"class":58},[33,491,62],{"class":43},[33,493,72],{"class":71},[33,495,423],{"class":75},[33,497,72],{"class":71},[33,499,110],{"class":43},[33,501,502,504],{"class":35,"line":84},[33,503,87],{"class":39},[33,505,44],{"class":43},[33,507,508,510,512,514,517,519],{"class":35,"line":95},[33,509,98],{"class":65},[33,511,62],{"class":43},[33,513,72],{"class":71},[33,515,516],{"class":75},"Something went wrong.",[33,518,72],{"class":71},[33,520,110],{"class":43},[14,522,523],{},"This catches almost everything, which can hide useful error details.",[14,525,526],{},"A better version is:",[23,528,530],{"className":25,"code":529,"language":27,"meta":28,"style":28},"try:\n    age = int(\"hello\")\nexcept ValueError:\n    print(\"Please enter a valid number.\")\n",[30,531,532,538,556,564],{"__ignoreMap":28},[33,533,534,536],{"class":35,"line":36},[33,535,40],{"class":39},[33,537,44],{"class":43},[33,539,540,542,544,546,548,550,552,554],{"class":35,"line":47},[33,541,412],{"class":50},[33,543,55],{"class":54},[33,545,59],{"class":58},[33,547,62],{"class":43},[33,549,72],{"class":71},[33,551,423],{"class":75},[33,553,72],{"class":71},[33,555,110],{"class":43},[33,557,558,560,562],{"class":35,"line":84},[33,559,87],{"class":39},[33,561,90],{"class":58},[33,563,44],{"class":43},[33,565,566,568,570,572,575,577],{"class":35,"line":95},[33,567,98],{"class":65},[33,569,62],{"class":43},[33,571,72],{"class":71},[33,573,574],{"class":75},"Please enter a valid number.",[33,576,72],{"class":71},[33,578,110],{"class":43},[14,580,581,582,352],{},"If you want more practice with this pattern, see ",[357,583,585],{"href":584},"\u002Fhow-to\u002Fhow-to-use-try-except-blocks-in-python\u002F","how to use try except blocks in Python",[18,587,214,589,217],{"id":588},"what-else-does",[30,590,116],{},[14,592,593,595,596,598],{},[30,594,116],{}," runs only if the ",[30,597,40],{}," block succeeds.",[14,600,601],{},"That means:",[186,603,604,609],{},[189,605,606,607],{},"no exception happened in ",[30,608,40],{},[189,610,611,612],{},"Python did not jump to ",[30,613,87],{},[14,615,168,616,618],{},[30,617,116],{}," for code that should run only after the risky code worked.",[14,620,251],{},[23,622,624],{"className":25,"code":623,"language":27,"meta":28,"style":28},"try:\n    number = int(\"42\")\nexcept ValueError:\n    print(\"Invalid number\")\nelse:\n    print(\"Conversion worked:\", number)\n",[30,625,626,632,651,659,674,680],{"__ignoreMap":28},[33,627,628,630],{"class":35,"line":36},[33,629,40],{"class":39},[33,631,44],{"class":43},[33,633,634,636,638,640,642,644,647,649],{"class":35,"line":47},[33,635,51],{"class":50},[33,637,55],{"class":54},[33,639,59],{"class":58},[33,641,62],{"class":43},[33,643,72],{"class":71},[33,645,646],{"class":75},"42",[33,648,72],{"class":71},[33,650,110],{"class":43},[33,652,653,655,657],{"class":35,"line":84},[33,654,87],{"class":39},[33,656,90],{"class":58},[33,658,44],{"class":43},[33,660,661,663,665,667,670,672],{"class":35,"line":95},[33,662,98],{"class":65},[33,664,62],{"class":43},[33,666,72],{"class":71},[33,668,669],{"class":75},"Invalid number",[33,671,72],{"class":71},[33,673,110],{"class":43},[33,675,676,678],{"class":35,"line":113},[33,677,116],{"class":39},[33,679,44],{"class":43},[33,681,682,684,686,688,691,693,695,697],{"class":35,"line":121},[33,683,98],{"class":65},[33,685,62],{"class":43},[33,687,72],{"class":71},[33,689,690],{"class":75},"Conversion worked:",[33,692,72],{"class":71},[33,694,135],{"class":43},[33,696,139],{"class":138},[33,698,110],{"class":43},[14,700,321],{},[23,702,704],{"className":25,"code":703,"language":27,"meta":28,"style":28},"Conversion worked: 42\n",[30,705,706],{"__ignoreMap":28},[33,707,708,711,713],{"class":35,"line":36},[33,709,710],{"class":50},"Conversion worked",[33,712,232],{"class":43},[33,714,715],{"class":272}," 42\n",[14,717,718],{},"Why is this useful?",[186,720,721,727,730],{},[189,722,723,724,726],{},"It keeps the ",[30,725,40],{}," block small",[189,728,729],{},"It makes debugging easier",[189,731,732],{},"It clearly separates risky code from success code",[14,734,735,736,738,739,741],{},"Do not use ",[30,737,116],{}," to handle errors. That is what ",[30,740,87],{}," is for.",[18,743,214,745,217],{"id":744},"what-finally-does",[30,746,147],{},[14,748,749,751],{},[30,750,147],{}," runs whether an error happens or not.",[14,753,754],{},"Use it for cleanup work, such as:",[186,756,757,760,763,766],{},[189,758,759],{},"closing files",[189,761,762],{},"releasing resources",[189,764,765],{},"printing a final message",[189,767,768],{},"resetting temporary state",[14,770,251],{},[23,772,774],{"className":25,"code":773,"language":27,"meta":28,"style":28},"try:\n    print(\"Trying something\")\nexcept ValueError:\n    print(\"A value error happened\")\nfinally:\n    print(\"This always runs\")\n",[30,775,776,782,797,805,820,826],{"__ignoreMap":28},[33,777,778,780],{"class":35,"line":36},[33,779,40],{"class":39},[33,781,44],{"class":43},[33,783,784,786,788,790,793,795],{"class":35,"line":47},[33,785,98],{"class":65},[33,787,62],{"class":43},[33,789,72],{"class":71},[33,791,792],{"class":75},"Trying something",[33,794,72],{"class":71},[33,796,110],{"class":43},[33,798,799,801,803],{"class":35,"line":84},[33,800,87],{"class":39},[33,802,90],{"class":58},[33,804,44],{"class":43},[33,806,807,809,811,813,816,818],{"class":35,"line":95},[33,808,98],{"class":65},[33,810,62],{"class":43},[33,812,72],{"class":71},[33,814,815],{"class":75},"A value error happened",[33,817,72],{"class":71},[33,819,110],{"class":43},[33,821,822,824],{"class":35,"line":113},[33,823,147],{"class":39},[33,825,44],{"class":43},[33,827,828,830,832,834,837,839],{"class":35,"line":121},[33,829,98],{"class":65},[33,831,62],{"class":43},[33,833,72],{"class":71},[33,835,836],{"class":75},"This always runs",[33,838,72],{"class":71},[33,840,110],{"class":43},[14,842,321],{},[23,844,846],{"className":25,"code":845,"language":27,"meta":28,"style":28},"Trying something\nThis always runs\n",[30,847,848,853],{"__ignoreMap":28},[33,849,850],{"class":35,"line":36},[33,851,852],{"class":50},"Trying something\n",[33,854,855],{"class":35,"line":47},[33,856,857],{"class":50},"This always runs\n",[14,859,860,861,863,864,867,868,870,871,873],{},"In normal Python execution, ",[30,862,147],{}," still runs even if there is a ",[30,865,866],{},"return"," statement in ",[30,869,40],{}," or ",[30,872,87],{}," in many cases.",[14,875,251],{},[23,877,879],{"className":25,"code":878,"language":27,"meta":28,"style":28},"def test():\n    try:\n        return \"from try\"\n    finally:\n        print(\"Cleaning up\")\n\nprint(test())\n",[30,880,881,894,901,915,922,938,944],{"__ignoreMap":28},[33,882,883,887,891],{"class":35,"line":36},[33,884,886],{"class":885},"sbsja","def",[33,888,890],{"class":889},"sGLFI"," test",[33,892,893],{"class":43},"():\n",[33,895,896,899],{"class":35,"line":47},[33,897,898],{"class":39},"    try",[33,900,44],{"class":43},[33,902,903,906,909,912],{"class":35,"line":84},[33,904,905],{"class":39},"        return",[33,907,908],{"class":71}," \"",[33,910,911],{"class":75},"from try",[33,913,914],{"class":71},"\"\n",[33,916,917,920],{"class":35,"line":95},[33,918,919],{"class":39},"    finally",[33,921,44],{"class":43},[33,923,924,927,929,931,934,936],{"class":35,"line":113},[33,925,926],{"class":65},"        print",[33,928,62],{"class":43},[33,930,72],{"class":71},[33,932,933],{"class":75},"Cleaning up",[33,935,72],{"class":71},[33,937,110],{"class":43},[33,939,940],{"class":35,"line":121},[33,941,943],{"emptyLinePlaceholder":942},true,"\n",[33,945,946,949,951,954],{"class":35,"line":144},[33,947,948],{"class":65},"print",[33,950,62],{"class":43},[33,952,953],{"class":138},"test",[33,955,956],{"class":43},"())\n",[14,958,321],{},[23,960,962],{"className":25,"code":961,"language":27,"meta":28,"style":28},"Cleaning up\nfrom try\n",[30,963,964,969],{"__ignoreMap":28},[33,965,966],{"class":35,"line":36},[33,967,968],{"class":50},"Cleaning up\n",[33,970,971,974],{"class":35,"line":47},[33,972,973],{"class":39},"from",[33,975,976],{"class":39}," try\n",[18,978,980],{"id":979},"execution-order-of-the-blocks","Execution order of the blocks",[14,982,983,984,352],{},"Python always starts with ",[30,985,40],{},[987,988,990],"h3",{"id":989},"if-no-exception-happens","If no exception happens",[14,992,993],{},"The order is:",[995,996,997,1001,1005],"ol",{},[189,998,999],{},[30,1000,40],{},[189,1002,1003],{},[30,1004,116],{},[189,1006,1007],{},[30,1008,147],{},[14,1010,251],{},[23,1012,1014],{"className":25,"code":1013,"language":27,"meta":28,"style":28},"try:\n    print(\"Try block\")\nexcept ValueError:\n    print(\"Except block\")\nelse:\n    print(\"Else block\")\nfinally:\n    print(\"Finally block\")\n",[30,1015,1016,1022,1037,1045,1060,1066,1081,1087],{"__ignoreMap":28},[33,1017,1018,1020],{"class":35,"line":36},[33,1019,40],{"class":39},[33,1021,44],{"class":43},[33,1023,1024,1026,1028,1030,1033,1035],{"class":35,"line":47},[33,1025,98],{"class":65},[33,1027,62],{"class":43},[33,1029,72],{"class":71},[33,1031,1032],{"class":75},"Try block",[33,1034,72],{"class":71},[33,1036,110],{"class":43},[33,1038,1039,1041,1043],{"class":35,"line":84},[33,1040,87],{"class":39},[33,1042,90],{"class":58},[33,1044,44],{"class":43},[33,1046,1047,1049,1051,1053,1056,1058],{"class":35,"line":95},[33,1048,98],{"class":65},[33,1050,62],{"class":43},[33,1052,72],{"class":71},[33,1054,1055],{"class":75},"Except block",[33,1057,72],{"class":71},[33,1059,110],{"class":43},[33,1061,1062,1064],{"class":35,"line":113},[33,1063,116],{"class":39},[33,1065,44],{"class":43},[33,1067,1068,1070,1072,1074,1077,1079],{"class":35,"line":121},[33,1069,98],{"class":65},[33,1071,62],{"class":43},[33,1073,72],{"class":71},[33,1075,1076],{"class":75},"Else block",[33,1078,72],{"class":71},[33,1080,110],{"class":43},[33,1082,1083,1085],{"class":35,"line":144},[33,1084,147],{"class":39},[33,1086,44],{"class":43},[33,1088,1089,1091,1093,1095,1098,1100],{"class":35,"line":152},[33,1090,98],{"class":65},[33,1092,62],{"class":43},[33,1094,72],{"class":71},[33,1096,1097],{"class":75},"Finally block",[33,1099,72],{"class":71},[33,1101,110],{"class":43},[14,1103,321],{},[23,1105,1107],{"className":25,"code":1106,"language":27,"meta":28,"style":28},"Try block\nElse block\nFinally block\n",[30,1108,1109,1114,1119],{"__ignoreMap":28},[33,1110,1111],{"class":35,"line":36},[33,1112,1113],{"class":50},"Try block\n",[33,1115,1116],{"class":35,"line":47},[33,1117,1118],{"class":50},"Else block\n",[33,1120,1121],{"class":35,"line":84},[33,1122,1123],{"class":50},"Finally block\n",[987,1125,1127],{"id":1126},"if-a-matching-exception-happens","If a matching exception happens",[14,1129,993],{},[995,1131,1132,1136,1140],{},[189,1133,1134],{},[30,1135,40],{},[189,1137,1138],{},[30,1139,87],{},[189,1141,1142],{},[30,1143,147],{},[14,1145,251],{},[23,1147,1149],{"className":25,"code":1148,"language":27,"meta":28,"style":28},"try:\n    number = int(\"abc\")\nexcept ValueError:\n    print(\"Except block\")\nelse:\n    print(\"Else block\")\nfinally:\n    print(\"Finally block\")\n",[30,1150,1151,1157,1176,1184,1198,1204,1218,1224],{"__ignoreMap":28},[33,1152,1153,1155],{"class":35,"line":36},[33,1154,40],{"class":39},[33,1156,44],{"class":43},[33,1158,1159,1161,1163,1165,1167,1169,1172,1174],{"class":35,"line":47},[33,1160,51],{"class":50},[33,1162,55],{"class":54},[33,1164,59],{"class":58},[33,1166,62],{"class":43},[33,1168,72],{"class":71},[33,1170,1171],{"class":75},"abc",[33,1173,72],{"class":71},[33,1175,110],{"class":43},[33,1177,1178,1180,1182],{"class":35,"line":84},[33,1179,87],{"class":39},[33,1181,90],{"class":58},[33,1183,44],{"class":43},[33,1185,1186,1188,1190,1192,1194,1196],{"class":35,"line":95},[33,1187,98],{"class":65},[33,1189,62],{"class":43},[33,1191,72],{"class":71},[33,1193,1055],{"class":75},[33,1195,72],{"class":71},[33,1197,110],{"class":43},[33,1199,1200,1202],{"class":35,"line":113},[33,1201,116],{"class":39},[33,1203,44],{"class":43},[33,1205,1206,1208,1210,1212,1214,1216],{"class":35,"line":121},[33,1207,98],{"class":65},[33,1209,62],{"class":43},[33,1211,72],{"class":71},[33,1213,1076],{"class":75},[33,1215,72],{"class":71},[33,1217,110],{"class":43},[33,1219,1220,1222],{"class":35,"line":144},[33,1221,147],{"class":39},[33,1223,44],{"class":43},[33,1225,1226,1228,1230,1232,1234,1236],{"class":35,"line":152},[33,1227,98],{"class":65},[33,1229,62],{"class":43},[33,1231,72],{"class":71},[33,1233,1097],{"class":75},[33,1235,72],{"class":71},[33,1237,110],{"class":43},[14,1239,321],{},[23,1241,1243],{"className":25,"code":1242,"language":27,"meta":28,"style":28},"Except block\nFinally block\n",[30,1244,1245,1250],{"__ignoreMap":28},[33,1246,1247],{"class":35,"line":36},[33,1248,1249],{"class":50},"Except block\n",[33,1251,1252],{"class":35,"line":47},[33,1253,1123],{"class":50},[987,1255,1257],{"id":1256},"if-the-exception-is-not-caught","If the exception is not caught",[14,1259,1260,1261,1263,1264,1266],{},"If no matching ",[30,1262,87],{}," block handles the error, ",[30,1265,147],{}," still runs before the program stops with the error.",[23,1268,1270],{"className":25,"code":1269,"language":27,"meta":28,"style":28},"try:\n    print(10 \u002F 0)\nexcept ValueError:\n    print(\"Wrong type of exception\")\nfinally:\n    print(\"Finally still runs\")\n",[30,1271,1272,1278,1294,1302,1317,1323],{"__ignoreMap":28},[33,1273,1274,1276],{"class":35,"line":36},[33,1275,40],{"class":39},[33,1277,44],{"class":43},[33,1279,1280,1282,1284,1287,1289,1292],{"class":35,"line":47},[33,1281,98],{"class":65},[33,1283,62],{"class":43},[33,1285,1286],{"class":272},"10",[33,1288,276],{"class":54},[33,1290,1291],{"class":272}," 0",[33,1293,110],{"class":43},[33,1295,1296,1298,1300],{"class":35,"line":84},[33,1297,87],{"class":39},[33,1299,90],{"class":58},[33,1301,44],{"class":43},[33,1303,1304,1306,1308,1310,1313,1315],{"class":35,"line":95},[33,1305,98],{"class":65},[33,1307,62],{"class":43},[33,1309,72],{"class":71},[33,1311,1312],{"class":75},"Wrong type of exception",[33,1314,72],{"class":71},[33,1316,110],{"class":43},[33,1318,1319,1321],{"class":35,"line":113},[33,1320,147],{"class":39},[33,1322,44],{"class":43},[33,1324,1325,1327,1329,1331,1334,1336],{"class":35,"line":121},[33,1326,98],{"class":65},[33,1328,62],{"class":43},[33,1330,72],{"class":71},[33,1332,1333],{"class":75},"Finally still runs",[33,1335,72],{"class":71},[33,1337,110],{"class":43},[14,1339,321],{},[23,1341,1343],{"className":25,"code":1342,"language":27,"meta":28,"style":28},"Finally still runs\nTraceback (most recent call last):\n...\nZeroDivisionError: division by zero\n",[30,1344,1345,1350,1363,1369],{"__ignoreMap":28},[33,1346,1347],{"class":35,"line":36},[33,1348,1349],{"class":50},"Finally still runs\n",[33,1351,1352,1355,1357,1360],{"class":35,"line":47},[33,1353,1354],{"class":138},"Traceback ",[33,1356,62],{"class":43},[33,1358,1359],{"class":138},"most recent call last",[33,1361,1362],{"class":43},"):\n",[33,1364,1365],{"class":35,"line":84},[33,1366,1368],{"class":1367},"s_hVV","...\n",[33,1370,1371,1373,1375],{"class":35,"line":95},[33,1372,344],{"class":58},[33,1374,232],{"class":43},[33,1376,1377],{"class":50}," division by zero\n",[18,1379,1381],{"id":1380},"when-to-use-each-block","When to use each block",[14,1383,1384],{},"Use each block for one clear purpose:",[186,1386,1387,1392,1397,1402],{},[189,1388,1389,1391],{},[30,1390,40],{},": risky code",[189,1393,1394,1396],{},[30,1395,87],{},": what to do if it fails",[189,1398,1399,1401],{},[30,1400,116],{},": code to run only on success",[189,1403,1404,1406],{},[30,1405,147],{},": cleanup that must always happen",[14,1408,1409],{},"A good mental model is:",[186,1411,1412,1418,1423,1428],{},[189,1413,1414],{},[1415,1416,1417],"strong",{},"Try it",[189,1419,1420],{},[1415,1421,1422],{},"Handle failure",[189,1424,1425],{},[1415,1426,1427],{},"Continue on success",[189,1429,1430],{},[1415,1431,1432],{},"Clean up at the end",[18,1434,1436],{"id":1435},"beginner-example-with-user-input","Beginner example with user input",[14,1438,1439],{},"This example converts user input to an integer.",[186,1441,1442,1448,1453,1458],{},[189,1443,1444,1447],{},[30,1445,1446],{},"int()"," may fail",[189,1449,1450,1452],{},[30,1451,385],{}," is caught if the input is not a valid whole number",[189,1454,1455,1457],{},[30,1456,116],{}," runs only when conversion succeeds",[189,1459,1460,1462],{},[30,1461,147],{}," always prints the final message",[23,1464,1466],{"className":25,"code":1465,"language":27,"meta":28,"style":28},"try:\n    number = int(input(\"Enter a whole number: \"))\nexcept ValueError:\n    print(\"Please enter a valid whole number.\")\nelse:\n    print(\"You entered:\", number)\nfinally:\n    print(\"Program finished.\")\n",[30,1467,1468,1474,1497,1505,1519,1525,1543,1549],{"__ignoreMap":28},[33,1469,1470,1472],{"class":35,"line":36},[33,1471,40],{"class":39},[33,1473,44],{"class":43},[33,1475,1476,1478,1480,1482,1484,1486,1488,1490,1493,1495],{"class":35,"line":47},[33,1477,51],{"class":50},[33,1479,55],{"class":54},[33,1481,59],{"class":58},[33,1483,62],{"class":43},[33,1485,66],{"class":65},[33,1487,62],{"class":43},[33,1489,72],{"class":71},[33,1491,1492],{"class":75},"Enter a whole number: ",[33,1494,72],{"class":71},[33,1496,81],{"class":43},[33,1498,1499,1501,1503],{"class":35,"line":84},[33,1500,87],{"class":39},[33,1502,90],{"class":58},[33,1504,44],{"class":43},[33,1506,1507,1509,1511,1513,1515,1517],{"class":35,"line":95},[33,1508,98],{"class":65},[33,1510,62],{"class":43},[33,1512,72],{"class":71},[33,1514,105],{"class":75},[33,1516,72],{"class":71},[33,1518,110],{"class":43},[33,1520,1521,1523],{"class":35,"line":113},[33,1522,116],{"class":39},[33,1524,44],{"class":43},[33,1526,1527,1529,1531,1533,1535,1537,1539,1541],{"class":35,"line":121},[33,1528,98],{"class":65},[33,1530,62],{"class":43},[33,1532,72],{"class":71},[33,1534,130],{"class":75},[33,1536,72],{"class":71},[33,1538,135],{"class":43},[33,1540,139],{"class":138},[33,1542,110],{"class":43},[33,1544,1545,1547],{"class":35,"line":144},[33,1546,147],{"class":39},[33,1548,44],{"class":43},[33,1550,1551,1553,1555,1557,1559,1561],{"class":35,"line":152},[33,1552,98],{"class":65},[33,1554,62],{"class":43},[33,1556,72],{"class":71},[33,1558,161],{"class":75},[33,1560,72],{"class":71},[33,1562,110],{"class":43},[14,1564,1565,1566,232],{},"Possible output if the user enters ",[30,1567,1568],{},"25",[23,1570,1572],{"className":25,"code":1571,"language":27,"meta":28,"style":28},"Enter a whole number: 25\nYou entered: 25\nProgram finished.\n",[30,1573,1574,1584,1593],{"__ignoreMap":28},[33,1575,1576,1579,1581],{"class":35,"line":36},[33,1577,1578],{"class":50},"Enter a whole number",[33,1580,232],{"class":43},[33,1582,1583],{"class":272}," 25\n",[33,1585,1586,1589,1591],{"class":35,"line":47},[33,1587,1588],{"class":50},"You entered",[33,1590,232],{"class":43},[33,1592,1583],{"class":272},[33,1594,1595,1598],{"class":35,"line":84},[33,1596,1597],{"class":50},"Program finished",[33,1599,334],{"class":43},[14,1601,1565,1602,232],{},[30,1603,423],{},[23,1605,1607],{"className":25,"code":1606,"language":27,"meta":28,"style":28},"Enter a whole number: hello\nPlease enter a valid whole number.\nProgram finished.\n",[30,1608,1609,1618,1625],{"__ignoreMap":28},[33,1610,1611,1613,1615],{"class":35,"line":36},[33,1612,1578],{"class":50},[33,1614,232],{"class":43},[33,1616,1617],{"class":50}," hello\n",[33,1619,1620,1623],{"class":35,"line":47},[33,1621,1622],{"class":50},"Please enter a valid whole number",[33,1624,334],{"class":43},[33,1626,1627,1629],{"class":35,"line":84},[33,1628,1597],{"class":50},[33,1630,334],{"class":43},[14,1632,1633,1634,352],{},"If you keep seeing this kind of problem, see ",[357,1635,1637],{"href":1636},"\u002Ferrors\u002Fvalueerror-in-python-causes-and-fixes\u002F","ValueError in Python: causes and fixes",[18,1639,1641],{"id":1640},"example-with-file-handling","Example with file handling",[14,1643,1644],{},"This example tries to open and read a file.",[186,1646,1647,1653,1659,1664],{},[189,1648,1649,1652],{},[30,1650,1651],{},"open()"," may fail if the file does not exist",[189,1654,1655,1658],{},[30,1656,1657],{},"except FileNotFoundError"," handles that case",[189,1660,1661,1663],{},[30,1662,116],{}," runs only when the file was opened successfully",[189,1665,1666,1668],{},[30,1667,147],{}," closes the file if it was opened",[23,1670,1672],{"className":25,"code":1671,"language":27,"meta":28,"style":28},"file = None\n\ntry:\n    file = open(\"message.txt\", \"r\")\n    content = file.read()\nexcept FileNotFoundError:\n    print(\"The file was not found.\")\nelse:\n    print(\"File content:\")\n    print(content)\nfinally:\n    if file is not None:\n        file.close()\n        print(\"File closed.\")\n",[30,1673,1674,1687,1691,1697,1727,1745,1754,1769,1775,1791,1803,1810,1829,1842],{"__ignoreMap":28},[33,1675,1676,1680,1683],{"class":35,"line":36},[33,1677,1679],{"class":1678},"sMMDD","file",[33,1681,1682],{"class":54}," =",[33,1684,1686],{"class":1685},"s39Yj"," None\n",[33,1688,1689],{"class":35,"line":47},[33,1690,943],{"emptyLinePlaceholder":942},[33,1692,1693,1695],{"class":35,"line":84},[33,1694,40],{"class":39},[33,1696,44],{"class":43},[33,1698,1699,1702,1704,1707,1709,1711,1714,1716,1718,1720,1723,1725],{"class":35,"line":95},[33,1700,1701],{"class":1678},"    file",[33,1703,1682],{"class":54},[33,1705,1706],{"class":65}," open",[33,1708,62],{"class":43},[33,1710,72],{"class":71},[33,1712,1713],{"class":75},"message.txt",[33,1715,72],{"class":71},[33,1717,135],{"class":43},[33,1719,908],{"class":71},[33,1721,1722],{"class":75},"r",[33,1724,72],{"class":71},[33,1726,110],{"class":43},[33,1728,1729,1732,1734,1737,1739,1742],{"class":35,"line":113},[33,1730,1731],{"class":50},"    content ",[33,1733,55],{"class":54},[33,1735,1736],{"class":1678}," file",[33,1738,352],{"class":43},[33,1740,1741],{"class":138},"read",[33,1743,1744],{"class":43},"()\n",[33,1746,1747,1749,1752],{"class":35,"line":121},[33,1748,87],{"class":39},[33,1750,1751],{"class":58}," FileNotFoundError",[33,1753,44],{"class":43},[33,1755,1756,1758,1760,1762,1765,1767],{"class":35,"line":144},[33,1757,98],{"class":65},[33,1759,62],{"class":43},[33,1761,72],{"class":71},[33,1763,1764],{"class":75},"The file was not found.",[33,1766,72],{"class":71},[33,1768,110],{"class":43},[33,1770,1771,1773],{"class":35,"line":152},[33,1772,116],{"class":39},[33,1774,44],{"class":43},[33,1776,1778,1780,1782,1784,1787,1789],{"class":35,"line":1777},9,[33,1779,98],{"class":65},[33,1781,62],{"class":43},[33,1783,72],{"class":71},[33,1785,1786],{"class":75},"File content:",[33,1788,72],{"class":71},[33,1790,110],{"class":43},[33,1792,1794,1796,1798,1801],{"class":35,"line":1793},10,[33,1795,98],{"class":65},[33,1797,62],{"class":43},[33,1799,1800],{"class":138},"content",[33,1802,110],{"class":43},[33,1804,1806,1808],{"class":35,"line":1805},11,[33,1807,147],{"class":39},[33,1809,44],{"class":43},[33,1811,1813,1816,1818,1821,1824,1827],{"class":35,"line":1812},12,[33,1814,1815],{"class":39},"    if",[33,1817,1736],{"class":1678},[33,1819,1820],{"class":54}," is",[33,1822,1823],{"class":54}," not",[33,1825,1826],{"class":1685}," None",[33,1828,44],{"class":43},[33,1830,1832,1835,1837,1840],{"class":35,"line":1831},13,[33,1833,1834],{"class":1678},"        file",[33,1836,352],{"class":43},[33,1838,1839],{"class":138},"close",[33,1841,1744],{"class":43},[33,1843,1845,1847,1849,1851,1854,1856],{"class":35,"line":1844},14,[33,1846,926],{"class":65},[33,1848,62],{"class":43},[33,1850,72],{"class":71},[33,1852,1853],{"class":75},"File closed.",[33,1855,72],{"class":71},[33,1857,110],{"class":43},[14,1859,1860,1861,1864],{},"Why do we set ",[30,1862,1863],{},"file = None"," first?",[14,1866,1867,1868,1870,1871,1873,1874,1876,1877,1879,1880,352],{},"Because if ",[30,1869,1651],{}," fails, the ",[30,1872,1679],{}," variable would not be created inside ",[30,1875,40],{},". Setting it before the ",[30,1878,40],{}," block makes it safe to check in ",[30,1881,147],{},[14,1883,1884,1885,352],{},"If you are working with file errors, see ",[357,1886,1888],{"href":1887},"\u002Ferrors\u002Ffilenotfounderror-in-python-causes-and-fixes\u002F","FileNotFoundError in Python: causes and fixes",[18,1890,1892],{"id":1891},"best-practices","Best practices",[186,1894,1895,1898,1904,1907,1912],{},[189,1896,1897],{},"Catch the most specific exception you can",[189,1899,1900,1901,1903],{},"Keep the ",[30,1902,40],{}," block as small as possible",[189,1905,1906],{},"Do not hide errors silently",[189,1908,168,1909,1911],{},[30,1910,147],{}," for cleanup, not normal program logic",[189,1913,168,1914,1916],{},[30,1915,116],{}," to separate success code from risky code",[14,1918,1919],{},"Good example:",[23,1921,1923],{"className":25,"code":1922,"language":27,"meta":28,"style":28},"try:\n    number = int(user_input)\nexcept ValueError:\n    print(\"Invalid input\")\nelse:\n    print(\"Valid number:\", number)\n",[30,1924,1925,1931,1946,1954,1969,1975],{"__ignoreMap":28},[33,1926,1927,1929],{"class":35,"line":36},[33,1928,40],{"class":39},[33,1930,44],{"class":43},[33,1932,1933,1935,1937,1939,1941,1944],{"class":35,"line":47},[33,1934,51],{"class":50},[33,1936,55],{"class":54},[33,1938,59],{"class":58},[33,1940,62],{"class":43},[33,1942,1943],{"class":138},"user_input",[33,1945,110],{"class":43},[33,1947,1948,1950,1952],{"class":35,"line":84},[33,1949,87],{"class":39},[33,1951,90],{"class":58},[33,1953,44],{"class":43},[33,1955,1956,1958,1960,1962,1965,1967],{"class":35,"line":95},[33,1957,98],{"class":65},[33,1959,62],{"class":43},[33,1961,72],{"class":71},[33,1963,1964],{"class":75},"Invalid input",[33,1966,72],{"class":71},[33,1968,110],{"class":43},[33,1970,1971,1973],{"class":35,"line":113},[33,1972,116],{"class":39},[33,1974,44],{"class":43},[33,1976,1977,1979,1981,1983,1986,1988,1990,1992],{"class":35,"line":121},[33,1978,98],{"class":65},[33,1980,62],{"class":43},[33,1982,72],{"class":71},[33,1984,1985],{"class":75},"Valid number:",[33,1987,72],{"class":71},[33,1989,135],{"class":43},[33,1991,139],{"class":138},[33,1993,110],{"class":43},[14,1995,1996,1997,1999],{},"This is better than putting everything inside ",[30,1998,40],{},", because it is clearer which line might fail.",[14,2001,2002,2003,352],{},"You can learn more patterns in ",[357,2004,2006],{"href":2005},"\u002Fhow-to\u002Fhow-to-handle-exceptions-in-python\u002F","how to handle exceptions in Python",[18,2008,2010],{"id":2009},"common-beginner-mistakes","Common beginner mistakes",[987,2012,2014,2015],{"id":2013},"putting-too-much-code-inside-try","Putting too much code inside ",[30,2016,40],{},[14,2018,2019],{},"Bad:",[23,2021,2023],{"className":25,"code":2022,"language":27,"meta":28,"style":28},"try:\n    number = int(user_input)\n    print(\"Number entered\")\n    total = number * 10\n    print(\"Total:\", total)\nexcept ValueError:\n    print(\"Invalid input\")\n",[30,2024,2025,2031,2045,2060,2076,2096,2104],{"__ignoreMap":28},[33,2026,2027,2029],{"class":35,"line":36},[33,2028,40],{"class":39},[33,2030,44],{"class":43},[33,2032,2033,2035,2037,2039,2041,2043],{"class":35,"line":47},[33,2034,51],{"class":50},[33,2036,55],{"class":54},[33,2038,59],{"class":58},[33,2040,62],{"class":43},[33,2042,1943],{"class":138},[33,2044,110],{"class":43},[33,2046,2047,2049,2051,2053,2056,2058],{"class":35,"line":84},[33,2048,98],{"class":65},[33,2050,62],{"class":43},[33,2052,72],{"class":71},[33,2054,2055],{"class":75},"Number entered",[33,2057,72],{"class":71},[33,2059,110],{"class":43},[33,2061,2062,2065,2067,2070,2073],{"class":35,"line":95},[33,2063,2064],{"class":50},"    total ",[33,2066,55],{"class":54},[33,2068,2069],{"class":50}," number ",[33,2071,2072],{"class":54},"*",[33,2074,2075],{"class":272}," 10\n",[33,2077,2078,2080,2082,2084,2087,2089,2091,2094],{"class":35,"line":113},[33,2079,98],{"class":65},[33,2081,62],{"class":43},[33,2083,72],{"class":71},[33,2085,2086],{"class":75},"Total:",[33,2088,72],{"class":71},[33,2090,135],{"class":43},[33,2092,2093],{"class":138}," total",[33,2095,110],{"class":43},[33,2097,2098,2100,2102],{"class":35,"line":121},[33,2099,87],{"class":39},[33,2101,90],{"class":58},[33,2103,44],{"class":43},[33,2105,2106,2108,2110,2112,2114,2116],{"class":35,"line":144},[33,2107,98],{"class":65},[33,2109,62],{"class":43},[33,2111,72],{"class":71},[33,2113,1964],{"class":75},[33,2115,72],{"class":71},[33,2117,110],{"class":43},[14,2119,2120],{},"Better:",[23,2122,2124],{"className":25,"code":2123,"language":27,"meta":28,"style":28},"try:\n    number = int(user_input)\nexcept ValueError:\n    print(\"Invalid input\")\nelse:\n    print(\"Number entered\")\n    total = number * 10\n    print(\"Total:\", total)\n",[30,2125,2126,2132,2146,2154,2168,2174,2188,2200],{"__ignoreMap":28},[33,2127,2128,2130],{"class":35,"line":36},[33,2129,40],{"class":39},[33,2131,44],{"class":43},[33,2133,2134,2136,2138,2140,2142,2144],{"class":35,"line":47},[33,2135,51],{"class":50},[33,2137,55],{"class":54},[33,2139,59],{"class":58},[33,2141,62],{"class":43},[33,2143,1943],{"class":138},[33,2145,110],{"class":43},[33,2147,2148,2150,2152],{"class":35,"line":84},[33,2149,87],{"class":39},[33,2151,90],{"class":58},[33,2153,44],{"class":43},[33,2155,2156,2158,2160,2162,2164,2166],{"class":35,"line":95},[33,2157,98],{"class":65},[33,2159,62],{"class":43},[33,2161,72],{"class":71},[33,2163,1964],{"class":75},[33,2165,72],{"class":71},[33,2167,110],{"class":43},[33,2169,2170,2172],{"class":35,"line":113},[33,2171,116],{"class":39},[33,2173,44],{"class":43},[33,2175,2176,2178,2180,2182,2184,2186],{"class":35,"line":121},[33,2177,98],{"class":65},[33,2179,62],{"class":43},[33,2181,72],{"class":71},[33,2183,2055],{"class":75},[33,2185,72],{"class":71},[33,2187,110],{"class":43},[33,2189,2190,2192,2194,2196,2198],{"class":35,"line":144},[33,2191,2064],{"class":50},[33,2193,55],{"class":54},[33,2195,2069],{"class":50},[33,2197,2072],{"class":54},[33,2199,2075],{"class":272},[33,2201,2202,2204,2206,2208,2210,2212,2214,2216],{"class":35,"line":152},[33,2203,98],{"class":65},[33,2205,62],{"class":43},[33,2207,72],{"class":71},[33,2209,2086],{"class":75},[33,2211,72],{"class":71},[33,2213,135],{"class":43},[33,2215,2093],{"class":138},[33,2217,110],{"class":43},[14,2219,2220],{},"This makes it clearer that only the conversion is risky.",[987,2222,2224,2225,2227],{"id":2223},"using-except-without-naming-an-exception-type","Using ",[30,2226,87],{}," without naming an exception type",[14,2229,2230],{},"This can catch unexpected errors and make debugging harder.",[14,2232,2233],{},"Prefer:",[23,2235,2237],{"className":25,"code":2236,"language":27,"meta":28,"style":28},"except ValueError:\n",[30,2238,2239],{"__ignoreMap":28},[33,2240,2241,2243,2245],{"class":35,"line":36},[33,2242,87],{"class":39},[33,2244,90],{"class":58},[33,2246,44],{"class":43},[14,2248,2249],{},"instead of:",[23,2251,2253],{"className":25,"code":2252,"language":27,"meta":28,"style":28},"except:\n",[30,2254,2255],{"__ignoreMap":28},[33,2256,2257,2259],{"class":35,"line":36},[33,2258,87],{"class":39},[33,2260,44],{"class":43},[987,2262,2264,2265,2267,2268],{"id":2263},"confusing-else-with-except","Confusing ",[30,2266,116],{}," with ",[30,2269,87],{},[14,2271,2272],{},"Remember:",[186,2274,2275,2280],{},[189,2276,2277,2279],{},[30,2278,87],{}," runs when an error happens",[189,2281,2282,2284],{},[30,2283,116],{}," runs when no error happens",[987,2286,2224,2288,2290],{"id":2287},"using-finally-when-cleanup-is-not-needed",[30,2289,147],{}," when cleanup is not needed",[14,2292,2293,2294,2296],{},"You do not have to use ",[30,2295,147],{}," every time. Use it when something must always happen at the end.",[987,2298,2300],{"id":2299},"catching-errors-but-not-fixing-the-real-cause","Catching errors but not fixing the real cause",[14,2302,2303],{},"This is not very helpful:",[23,2305,2307],{"className":25,"code":2306,"language":27,"meta":28,"style":28},"try:\n    number = int(text)\nexcept ValueError:\n    pass\n",[30,2308,2309,2315,2330,2338],{"__ignoreMap":28},[33,2310,2311,2313],{"class":35,"line":36},[33,2312,40],{"class":39},[33,2314,44],{"class":43},[33,2316,2317,2319,2321,2323,2325,2328],{"class":35,"line":47},[33,2318,51],{"class":50},[33,2320,55],{"class":54},[33,2322,59],{"class":58},[33,2324,62],{"class":43},[33,2326,2327],{"class":138},"text",[33,2329,110],{"class":43},[33,2331,2332,2334,2336],{"class":35,"line":84},[33,2333,87],{"class":39},[33,2335,90],{"class":58},[33,2337,44],{"class":43},[33,2339,2340],{"class":35,"line":95},[33,2341,2342],{"class":39},"    pass\n",[14,2344,2345],{},"It hides the problem.",[14,2347,2348],{},"At minimum, print a useful message or handle the input another way.",[18,2350,2352],{"id":2351},"common-causes-of-confusion","Common causes of confusion",[14,2354,2355],{},"These are common reasons beginners struggle with exception blocks:",[186,2357,2358,2363,2373,2379,2387],{},[189,2359,2224,2360,2362],{},[30,2361,40],{}," around a large block of unrelated code",[189,2364,2365,2366,2369,2370,2372],{},"Catching ",[30,2367,2368],{},"Exception"," or using bare ",[30,2371,87],{}," too early in learning",[189,2374,2375,2376,2378],{},"Putting code that can also fail inside ",[30,2377,87],{}," without another plan",[189,2380,2381,2382,2384,2385],{},"Trying to use a variable in ",[30,2383,147],{}," that was never created in ",[30,2386,40],{},[189,2388,2389,2390,2392],{},"Forgetting that ",[30,2391,116],{}," runs only when no exception occurs",[18,2394,2396],{"id":2395},"useful-debugging-commands","Useful debugging commands",[14,2398,2399],{},"These commands can help when you are learning exception handling:",[23,2401,2405],{"className":2402,"code":2403,"language":2404,"meta":28,"style":28},"language-bash shiki shiki-themes material-theme-lighter github-light github-dark","python your_script.py\n","bash",[30,2406,2407],{"__ignoreMap":28},[33,2408,2409,2412],{"class":35,"line":36},[33,2410,27],{"class":2411},"sbgvK",[33,2413,2414],{"class":75}," your_script.py\n",[23,2416,2418],{"className":25,"code":2417,"language":27,"meta":28,"style":28},"print(type(value))\nprint(repr(value))\nhelp(ValueError)\nhelp(FileNotFoundError)\n",[30,2419,2420,2436,2451,2462],{"__ignoreMap":28},[33,2421,2422,2424,2426,2429,2431,2434],{"class":35,"line":36},[33,2423,948],{"class":65},[33,2425,62],{"class":43},[33,2427,2428],{"class":58},"type",[33,2430,62],{"class":43},[33,2432,2433],{"class":138},"value",[33,2435,81],{"class":43},[33,2437,2438,2440,2442,2445,2447,2449],{"class":35,"line":47},[33,2439,948],{"class":65},[33,2441,62],{"class":43},[33,2443,2444],{"class":65},"repr",[33,2446,62],{"class":43},[33,2448,2433],{"class":138},[33,2450,81],{"class":43},[33,2452,2453,2456,2458,2460],{"class":35,"line":84},[33,2454,2455],{"class":65},"help",[33,2457,62],{"class":43},[33,2459,385],{"class":58},[33,2461,110],{"class":43},[33,2463,2464,2466,2468,2470],{"class":35,"line":95},[33,2465,2455],{"class":65},[33,2467,62],{"class":43},[33,2469,394],{"class":58},[33,2471,110],{"class":43},[14,2473,2474],{},"These are useful because:",[186,2476,2477,2483,2489,2495],{},[189,2478,2479,2482],{},[30,2480,2481],{},"python your_script.py"," runs your program from the terminal",[189,2484,2485,2488],{},[30,2486,2487],{},"type()"," shows what kind of value you have",[189,2490,2491,2494],{},[30,2492,2493],{},"repr()"," shows a more exact version of a value",[189,2496,2497,2500],{},[30,2498,2499],{},"help()"," shows built-in documentation for an exception type",[18,2502,2504],{"id":2503},"faq","FAQ",[987,2506,2508],{"id":2507},"do-i-need-to-use-all-four-blocks-every-time","Do I need to use all four blocks every time?",[14,2510,2511,2512,2514,2515,352],{},"No. You can use only the blocks you need. Many programs use just ",[30,2513,40],{}," and ",[30,2516,87],{},[987,2518,2520,2521,2514,2523,2525],{"id":2519},"what-is-the-difference-between-except-and-else","What is the difference between ",[30,2522,87],{},[30,2524,116],{},"?",[14,2527,2528,2530,2531,2533],{},[30,2529,87],{}," runs when an error happens. ",[30,2532,116],{}," runs when no error happens.",[987,2535,2537,2538,2540],{"id":2536},"does-finally-always-run","Does ",[30,2539,147],{}," always run?",[14,2542,2543],{},"In normal Python execution, yes. It is meant for cleanup code that should always run.",[987,2545,2547,2548,2525],{"id":2546},"should-i-use-bare-except","Should I use bare ",[30,2549,87],{},[14,2551,2552],{},"Usually no. It can hide useful error details and make debugging harder.",[987,2554,2556,2557,2559],{"id":2555},"can-i-have-more-than-one-except-block","Can I have more than one ",[30,2558,87],{}," block?",[14,2561,2562,2563,2565],{},"Yes. You can catch different exception types with separate ",[30,2564,87],{}," blocks.",[14,2567,251],{},[23,2569,2571],{"className":25,"code":2570,"language":27,"meta":28,"style":28},"try:\n    value = int(input(\"Enter a number: \"))\n    result = 10 \u002F value\nexcept ValueError:\n    print(\"That was not a valid integer.\")\nexcept ZeroDivisionError:\n    print(\"You cannot divide by zero.\")\nelse:\n    print(\"Result:\", result)\nfinally:\n    print(\"Done.\")\n",[30,2572,2573,2579,2602,2615,2623,2638,2646,2660,2666,2686,2692],{"__ignoreMap":28},[33,2574,2575,2577],{"class":35,"line":36},[33,2576,40],{"class":39},[33,2578,44],{"class":43},[33,2580,2581,2584,2586,2588,2590,2592,2594,2596,2598,2600],{"class":35,"line":47},[33,2582,2583],{"class":50},"    value ",[33,2585,55],{"class":54},[33,2587,59],{"class":58},[33,2589,62],{"class":43},[33,2591,66],{"class":65},[33,2593,62],{"class":43},[33,2595,72],{"class":71},[33,2597,76],{"class":75},[33,2599,72],{"class":71},[33,2601,81],{"class":43},[33,2603,2604,2606,2608,2610,2612],{"class":35,"line":84},[33,2605,267],{"class":50},[33,2607,55],{"class":54},[33,2609,273],{"class":272},[33,2611,276],{"class":54},[33,2613,2614],{"class":50}," value\n",[33,2616,2617,2619,2621],{"class":35,"line":95},[33,2618,87],{"class":39},[33,2620,90],{"class":58},[33,2622,44],{"class":43},[33,2624,2625,2627,2629,2631,2634,2636],{"class":35,"line":113},[33,2626,98],{"class":65},[33,2628,62],{"class":43},[33,2630,72],{"class":71},[33,2632,2633],{"class":75},"That was not a valid integer.",[33,2635,72],{"class":71},[33,2637,110],{"class":43},[33,2639,2640,2642,2644],{"class":35,"line":121},[33,2641,87],{"class":39},[33,2643,301],{"class":58},[33,2645,44],{"class":43},[33,2647,2648,2650,2652,2654,2656,2658],{"class":35,"line":144},[33,2649,98],{"class":65},[33,2651,62],{"class":43},[33,2653,72],{"class":71},[33,2655,314],{"class":75},[33,2657,72],{"class":71},[33,2659,110],{"class":43},[33,2661,2662,2664],{"class":35,"line":152},[33,2663,116],{"class":39},[33,2665,44],{"class":43},[33,2667,2668,2670,2672,2674,2677,2679,2681,2684],{"class":35,"line":1777},[33,2669,98],{"class":65},[33,2671,62],{"class":43},[33,2673,72],{"class":71},[33,2675,2676],{"class":75},"Result:",[33,2678,72],{"class":71},[33,2680,135],{"class":43},[33,2682,2683],{"class":138}," result",[33,2685,110],{"class":43},[33,2687,2688,2690],{"class":35,"line":1793},[33,2689,147],{"class":39},[33,2691,44],{"class":43},[33,2693,2694,2696,2698,2700,2703,2705],{"class":35,"line":1805},[33,2695,98],{"class":65},[33,2697,62],{"class":43},[33,2699,72],{"class":71},[33,2701,2702],{"class":75},"Done.",[33,2704,72],{"class":71},[33,2706,110],{"class":43},[18,2708,2710],{"id":2709},"see-also","See also",[186,2712,2713,2717,2722,2727,2733,2737],{},[189,2714,2715],{},[357,2716,360],{"href":359},[189,2718,2719],{},[357,2720,2721],{"href":584},"How to use try except blocks in Python",[189,2723,2724],{},[357,2725,2726],{"href":2005},"How to handle exceptions in Python",[189,2728,2729],{},[357,2730,2732],{"href":2731},"\u002Flearn\u002Fraising-exceptions-in-python\u002F","Raising exceptions in Python",[189,2734,2735],{},[357,2736,1637],{"href":1636},[189,2738,2739],{},[357,2740,1888],{"href":1887},[2742,2743,2744],"style",{},"html pre.shiki code .sVHd0, html code.shiki .sVHd0{--shiki-light:#39ADB5;--shiki-light-font-style:italic;--shiki-default:#D73A49;--shiki-default-font-style:inherit;--shiki-dark:#F97583;--shiki-dark-font-style:inherit}html pre.shiki code .sP7_E, html code.shiki .sP7_E{--shiki-light:#39ADB5;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .su5hD, html code.shiki .su5hD{--shiki-light:#90A4AE;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .smGrS, html code.shiki .smGrS{--shiki-light:#39ADB5;--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .sZMiF, html code.shiki .sZMiF{--shiki-light:#E2931D;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .sptTA, html code.shiki .sptTA{--shiki-light:#6182B8;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .sjJ54, html code.shiki .sjJ54{--shiki-light:#39ADB5;--shiki-default:#032F62;--shiki-dark:#9ECBFF}html pre.shiki code .s_sjI, html code.shiki .s_sjI{--shiki-light:#91B859;--shiki-default:#032F62;--shiki-dark:#9ECBFF}html pre.shiki code .slqww, html code.shiki .slqww{--shiki-light:#6182B8;--shiki-default:#24292E;--shiki-dark:#E1E4E8}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);}html pre.shiki code .srdBf, html code.shiki .srdBf{--shiki-light:#F76D47;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .sbsja, html code.shiki .sbsja{--shiki-light:#9C3EDA;--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .sGLFI, html code.shiki .sGLFI{--shiki-light:#6182B8;--shiki-default:#6F42C1;--shiki-dark:#B392F0}html pre.shiki code .s_hVV, html code.shiki .s_hVV{--shiki-light:#90A4AE;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .sMMDD, html code.shiki .sMMDD{--shiki-light:#90A4AE;--shiki-default:#E36209;--shiki-dark:#FFAB70}html pre.shiki code .s39Yj, html code.shiki .s39Yj{--shiki-light:#39ADB5;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .sbgvK, html code.shiki .sbgvK{--shiki-light:#E2931D;--shiki-default:#6F42C1;--shiki-dark:#B392F0}",{"title":28,"searchDepth":47,"depth":47,"links":2746},[2747,2748,2749,2751,2753,2755,2757,2762,2763,2764,2765,2766,2777,2778,2779,2790],{"id":20,"depth":47,"text":21},{"id":183,"depth":47,"text":184},{"id":213,"depth":47,"text":2750},"What try does",{"id":363,"depth":47,"text":2752},"What except does",{"id":588,"depth":47,"text":2754},"What else does",{"id":744,"depth":47,"text":2756},"What finally does",{"id":979,"depth":47,"text":980,"children":2758},[2759,2760,2761],{"id":989,"depth":84,"text":990},{"id":1126,"depth":84,"text":1127},{"id":1256,"depth":84,"text":1257},{"id":1380,"depth":47,"text":1381},{"id":1435,"depth":47,"text":1436},{"id":1640,"depth":47,"text":1641},{"id":1891,"depth":47,"text":1892},{"id":2009,"depth":47,"text":2010,"children":2767},[2768,2770,2772,2774,2776],{"id":2013,"depth":84,"text":2769},"Putting too much code inside try",{"id":2223,"depth":84,"text":2771},"Using except without naming an exception type",{"id":2263,"depth":84,"text":2773},"Confusing else with except",{"id":2287,"depth":84,"text":2775},"Using finally when cleanup is not needed",{"id":2299,"depth":84,"text":2300},{"id":2351,"depth":47,"text":2352},{"id":2395,"depth":47,"text":2396},{"id":2503,"depth":47,"text":2504,"children":2780},[2781,2782,2784,2786,2788],{"id":2507,"depth":84,"text":2508},{"id":2519,"depth":84,"text":2783},"What is the difference between except and else?",{"id":2536,"depth":84,"text":2785},"Does finally always run?",{"id":2546,"depth":84,"text":2787},"Should I use bare except?",{"id":2555,"depth":84,"text":2789},"Can I have more than one except block?",{"id":2709,"depth":47,"text":2710},"Master using try except else and finally in python in our comprehensive Python beginner guide.","md",{},"\u002Flearn\u002Fusing-try-except-else-and-finally-in-python",{"title":5,"description":2791},"learn\u002Fusing-try-except-else-and-finally-in-python","5abiL9O4faxnyA7B9L3C4BlQ5RP8KTConUQYGEWX10A",1777585495591]