[{"data":1,"prerenderedAt":1660},["ShallowReactive",2],{"doc-\u002Fhow-to\u002Fhow-to-use-try-except-blocks-in-python":3},{"id":4,"title":5,"body":6,"description":1653,"extension":1654,"meta":1655,"navigation":1341,"path":1656,"seo":1657,"stem":1658,"__hash__":1659},"content\u002Fhow-to\u002Fhow-to-use-try-except-blocks-in-python.md","How to Use try-except Blocks in Python",{"type":7,"value":8,"toc":1623},"minimark",[9,13,22,27,161,170,174,198,202,208,232,235,275,280,339,343,368,371,384,393,397,400,414,498,502,531,535,542,565,569,590,601,605,608,611,626,632,744,748,766,772,783,787,792,795,809,812,818,821,837,841,846,875,879,947,950,953,964,967,1057,1061,1064,1198,1208,1211,1215,1221,1238,1241,1305,1309,1312,1400,1403,1416,1419,1524,1534,1538,1542,1545,1549,1557,1561,1567,1571,1574,1578,1606,1619],[10,11,5],"h1",{"id":12},"how-to-use-try-except-blocks-in-python",[14,15,16,17,21],"p",{},"Learn how to catch errors in Python with ",[18,19,20],"code",{},"try-except"," blocks so your program does not stop unexpectedly. This page focuses on the basic pattern, when to use it, and beginner-friendly examples.",[23,24,26],"h2",{"id":25},"quick-example","Quick example",[28,29,34],"pre",{"className":30,"code":31,"language":32,"meta":33,"style":33},"language-python shiki shiki-themes material-theme-lighter github-light github-dark","try:\n    number = int(input(\"Enter a number: \"))\n    print(10 \u002F number)\nexcept ValueError:\n    print(\"Please enter a valid whole number.\")\nexcept ZeroDivisionError:\n    print(\"You cannot divide by zero.\")\n","python","",[18,35,36,49,86,108,119,135,145],{"__ignoreMap":33},[37,38,41,45],"span",{"class":39,"line":40},"line",1,[37,42,44],{"class":43},"sVHd0","try",[37,46,48],{"class":47},"sP7_E",":\n",[37,50,52,56,60,64,67,71,73,77,81,83],{"class":39,"line":51},2,[37,53,55],{"class":54},"su5hD","    number ",[37,57,59],{"class":58},"smGrS","=",[37,61,63],{"class":62},"sZMiF"," int",[37,65,66],{"class":47},"(",[37,68,70],{"class":69},"sptTA","input",[37,72,66],{"class":47},[37,74,76],{"class":75},"sjJ54","\"",[37,78,80],{"class":79},"s_sjI","Enter a number: ",[37,82,76],{"class":75},[37,84,85],{"class":47},"))\n",[37,87,89,92,94,98,101,105],{"class":39,"line":88},3,[37,90,91],{"class":69},"    print",[37,93,66],{"class":47},[37,95,97],{"class":96},"srdBf","10",[37,99,100],{"class":58}," \u002F",[37,102,104],{"class":103},"slqww"," number",[37,106,107],{"class":47},")\n",[37,109,111,114,117],{"class":39,"line":110},4,[37,112,113],{"class":43},"except",[37,115,116],{"class":62}," ValueError",[37,118,48],{"class":47},[37,120,122,124,126,128,131,133],{"class":39,"line":121},5,[37,123,91],{"class":69},[37,125,66],{"class":47},[37,127,76],{"class":75},[37,129,130],{"class":79},"Please enter a valid whole number.",[37,132,76],{"class":75},[37,134,107],{"class":47},[37,136,138,140,143],{"class":39,"line":137},6,[37,139,113],{"class":43},[37,141,142],{"class":62}," ZeroDivisionError",[37,144,48],{"class":47},[37,146,148,150,152,154,157,159],{"class":39,"line":147},7,[37,149,91],{"class":69},[37,151,66],{"class":47},[37,153,76],{"class":75},[37,155,156],{"class":79},"You cannot divide by zero.",[37,158,76],{"class":75},[37,160,107],{"class":47},[14,162,163,164,166,167,169],{},"Use ",[18,165,44],{}," for code that may fail, and ",[18,168,113],{}," for the specific errors you want to handle.",[23,171,173],{"id":172},"what-this-page-helps-you-do","What this page helps you do",[175,176,177,184,187,195],"ul",{},[178,179,180,181,183],"li",{},"Use a ",[18,182,20],{}," block to handle errors safely",[178,185,186],{},"Catch common beginner errors without crashing the program",[178,188,189,190,192,193],{},"Understand the difference between ",[18,191,44],{}," and ",[18,194,113],{},[178,196,197],{},"See when to catch one error or multiple errors",[23,199,201],{"id":200},"basic-try-except-structure","Basic try-except structure",[14,203,204,205,207],{},"A ",[18,206,20],{}," block lets you run code that might fail.",[175,209,210,216,221,227],{},[178,211,212,213,215],{},"Put risky code inside the ",[18,214,44],{}," block",[178,217,218,219,215],{},"Put error-handling code inside the ",[18,220,113],{},[178,222,223,224,226],{},"If no error happens, the ",[18,225,113],{}," block is skipped",[178,228,229,230,215],{},"If an error happens, Python jumps to the matching ",[18,231,113],{},[14,233,234],{},"Here is the basic pattern:",[28,236,238],{"className":30,"code":237,"language":32,"meta":33,"style":33},"try:\n    # code that may raise an error\n    pass\nexcept SomeError:\n    # code that runs if that error happens\n    pass\n",[18,239,240,246,252,257,266,271],{"__ignoreMap":33},[37,241,242,244],{"class":39,"line":40},[37,243,44],{"class":43},[37,245,48],{"class":47},[37,247,248],{"class":39,"line":51},[37,249,251],{"class":250},"sutJx","    # code that may raise an error\n",[37,253,254],{"class":39,"line":88},[37,255,256],{"class":43},"    pass\n",[37,258,259,261,264],{"class":39,"line":110},[37,260,113],{"class":43},[37,262,263],{"class":54}," SomeError",[37,265,48],{"class":47},[37,267,268],{"class":39,"line":121},[37,269,270],{"class":250},"    # code that runs if that error happens\n",[37,272,273],{"class":39,"line":137},[37,274,256],{"class":43},[276,277,279],"h3",{"id":278},"example","Example",[28,281,283],{"className":30,"code":282,"language":32,"meta":33,"style":33},"try:\n    result = 10 \u002F 0\n    print(result)\nexcept ZeroDivisionError:\n    print(\"You cannot divide by zero.\")\n",[18,284,285,291,306,317,325],{"__ignoreMap":33},[37,286,287,289],{"class":39,"line":40},[37,288,44],{"class":43},[37,290,48],{"class":47},[37,292,293,296,298,301,303],{"class":39,"line":51},[37,294,295],{"class":54},"    result ",[37,297,59],{"class":58},[37,299,300],{"class":96}," 10",[37,302,100],{"class":58},[37,304,305],{"class":96}," 0\n",[37,307,308,310,312,315],{"class":39,"line":88},[37,309,91],{"class":69},[37,311,66],{"class":47},[37,313,314],{"class":103},"result",[37,316,107],{"class":47},[37,318,319,321,323],{"class":39,"line":110},[37,320,113],{"class":43},[37,322,142],{"class":62},[37,324,48],{"class":47},[37,326,327,329,331,333,335,337],{"class":39,"line":121},[37,328,91],{"class":69},[37,330,66],{"class":47},[37,332,76],{"class":75},[37,334,156],{"class":79},[37,336,76],{"class":75},[37,338,107],{"class":47},[276,340,342],{"id":341},"what-happens-here","What happens here",[175,344,345,351,357,362],{},[178,346,347,348],{},"Python tries to run ",[18,349,350],{},"10 \u002F 0",[178,352,353,354],{},"That raises a ",[18,355,356],{},"ZeroDivisionError",[178,358,359,360,215],{},"Python skips the rest of the ",[18,361,44],{},[178,363,364,365,367],{},"Python runs the matching ",[18,366,113],{}," block instead",[14,369,370],{},"Output:",[28,372,374],{"className":30,"code":373,"language":32,"meta":33,"style":33},"You cannot divide by zero.\n",[18,375,376],{"__ignoreMap":33},[37,377,378,381],{"class":39,"line":40},[37,379,380],{"class":54},"You cannot divide by zero",[37,382,383],{"class":47},".\n",[14,385,386,387,392],{},"If you are new to exceptions, see ",[388,389,391],"a",{"href":390},"\u002Flearn\u002Fpython-errors-and-exceptions-explained","Python errors and exceptions explained",".",[23,394,396],{"id":395},"simple-example-with-user-input","Simple example with user input",[14,398,399],{},"A common beginner problem is converting input to a number.",[14,401,402,403,406,407,410,411,392],{},"The ",[18,404,405],{},"input()"," function always returns text. If the text is not a valid integer, ",[18,408,409],{},"int()"," raises a ",[18,412,413],{},"ValueError",[28,415,417],{"className":30,"code":416,"language":32,"meta":33,"style":33},"try:\n    age = int(input(\"Enter your age: \"))\n    print(\"Next year you will be\", age + 1)\nexcept ValueError:\n    print(\"Please enter a valid whole number.\")\n",[18,418,419,425,449,476,484],{"__ignoreMap":33},[37,420,421,423],{"class":39,"line":40},[37,422,44],{"class":43},[37,424,48],{"class":47},[37,426,427,430,432,434,436,438,440,442,445,447],{"class":39,"line":51},[37,428,429],{"class":54},"    age ",[37,431,59],{"class":58},[37,433,63],{"class":62},[37,435,66],{"class":47},[37,437,70],{"class":69},[37,439,66],{"class":47},[37,441,76],{"class":75},[37,443,444],{"class":79},"Enter your age: ",[37,446,76],{"class":75},[37,448,85],{"class":47},[37,450,451,453,455,457,460,462,465,468,471,474],{"class":39,"line":88},[37,452,91],{"class":69},[37,454,66],{"class":47},[37,456,76],{"class":75},[37,458,459],{"class":79},"Next year you will be",[37,461,76],{"class":75},[37,463,464],{"class":47},",",[37,466,467],{"class":103}," age ",[37,469,470],{"class":58},"+",[37,472,473],{"class":96}," 1",[37,475,107],{"class":47},[37,477,478,480,482],{"class":39,"line":110},[37,479,113],{"class":43},[37,481,116],{"class":62},[37,483,48],{"class":47},[37,485,486,488,490,492,494,496],{"class":39,"line":121},[37,487,91],{"class":69},[37,489,66],{"class":47},[37,491,76],{"class":75},[37,493,130],{"class":79},[37,495,76],{"class":75},[37,497,107],{"class":47},[276,499,501],{"id":500},"why-this-works","Why this works",[175,503,504,510,516,525],{},[178,505,506,509],{},[18,507,508],{},"input(\"Enter your age: \")"," gets text from the user",[178,511,512,515],{},[18,513,514],{},"int(...)"," tries to turn that text into an integer",[178,517,518,519,522,523],{},"If the user enters something like ",[18,520,521],{},"abc",", Python raises ",[18,524,413],{},[178,526,402,527,530],{},[18,528,529],{},"except ValueError"," block shows a helpful message",[276,532,534],{"id":533},"example-runs","Example runs",[14,536,537,538,541],{},"If the user enters ",[18,539,540],{},"25",":",[28,543,545],{"className":30,"code":544,"language":32,"meta":33,"style":33},"Enter your age: 25\nNext year you will be 26\n",[18,546,547,557],{"__ignoreMap":33},[37,548,549,552,554],{"class":39,"line":40},[37,550,551],{"class":54},"Enter your age",[37,553,541],{"class":47},[37,555,556],{"class":96}," 25\n",[37,558,559,562],{"class":39,"line":51},[37,560,561],{"class":54},"Next year you will be ",[37,563,564],{"class":96},"26\n",[14,566,537,567,541],{},[18,568,521],{},[28,570,572],{"className":30,"code":571,"language":32,"meta":33,"style":33},"Enter your age: abc\nPlease enter a valid whole number.\n",[18,573,574,583],{"__ignoreMap":33},[37,575,576,578,580],{"class":39,"line":40},[37,577,551],{"class":54},[37,579,541],{"class":47},[37,581,582],{"class":54}," abc\n",[37,584,585,588],{"class":39,"line":51},[37,586,587],{"class":54},"Please enter a valid whole number",[37,589,383],{"class":47},[14,591,592,593,192,597,392],{},"If you want to learn more about converting input, see ",[388,594,596],{"href":595},"\u002Fhow-to\u002Fhow-to-convert-user-input-to-numbers-in-python\u002F","how to convert user input to numbers in Python",[388,598,600],{"href":599},"\u002Freference\u002Fpython-int-function-explained","the Python int() function explained",[23,602,604],{"id":603},"catching-more-than-one-error","Catching more than one error",[14,606,607],{},"Sometimes more than one kind of error can happen.",[14,609,610],{},"For example:",[175,612,613,621],{},[178,614,615,618,619],{},[18,616,617],{},"int(input(...))"," can raise ",[18,620,413],{},[178,622,623,624],{},"dividing by that number can raise ",[18,625,356],{},[14,627,628,629,631],{},"Use separate ",[18,630,113],{}," blocks when you want different messages or actions.",[28,633,635],{"className":30,"code":634,"language":32,"meta":33,"style":33},"try:\n    number = int(input(\"Enter a number: \"))\n    result = 10 \u002F number\n    print(\"Result:\", result)\nexcept ValueError:\n    print(\"That was not a valid whole number.\")\nexcept ZeroDivisionError:\n    print(\"You cannot divide by zero.\")\n",[18,636,637,643,665,678,698,706,721,729],{"__ignoreMap":33},[37,638,639,641],{"class":39,"line":40},[37,640,44],{"class":43},[37,642,48],{"class":47},[37,644,645,647,649,651,653,655,657,659,661,663],{"class":39,"line":51},[37,646,55],{"class":54},[37,648,59],{"class":58},[37,650,63],{"class":62},[37,652,66],{"class":47},[37,654,70],{"class":69},[37,656,66],{"class":47},[37,658,76],{"class":75},[37,660,80],{"class":79},[37,662,76],{"class":75},[37,664,85],{"class":47},[37,666,667,669,671,673,675],{"class":39,"line":88},[37,668,295],{"class":54},[37,670,59],{"class":58},[37,672,300],{"class":96},[37,674,100],{"class":58},[37,676,677],{"class":54}," number\n",[37,679,680,682,684,686,689,691,693,696],{"class":39,"line":110},[37,681,91],{"class":69},[37,683,66],{"class":47},[37,685,76],{"class":75},[37,687,688],{"class":79},"Result:",[37,690,76],{"class":75},[37,692,464],{"class":47},[37,694,695],{"class":103}," result",[37,697,107],{"class":47},[37,699,700,702,704],{"class":39,"line":121},[37,701,113],{"class":43},[37,703,116],{"class":62},[37,705,48],{"class":47},[37,707,708,710,712,714,717,719],{"class":39,"line":137},[37,709,91],{"class":69},[37,711,66],{"class":47},[37,713,76],{"class":75},[37,715,716],{"class":79},"That was not a valid whole number.",[37,718,76],{"class":75},[37,720,107],{"class":47},[37,722,723,725,727],{"class":39,"line":147},[37,724,113],{"class":43},[37,726,142],{"class":62},[37,728,48],{"class":47},[37,730,732,734,736,738,740,742],{"class":39,"line":731},8,[37,733,91],{"class":69},[37,735,66],{"class":47},[37,737,76],{"class":75},[37,739,156],{"class":79},[37,741,76],{"class":75},[37,743,107],{"class":47},[276,745,747],{"id":746},"why-separate-blocks-are-useful","Why separate blocks are useful",[175,749,750,755,763],{},[178,751,752,754],{},[18,753,413],{}," means the input could not be converted",[178,756,757,759,760],{},[18,758,356],{}," means the number was ",[18,761,762],{},"0",[178,764,765],{},"Each error gets a clear message",[14,767,768,769,771],{},"This is better than using a broad ",[18,770,113],{}," because you know exactly what went wrong.",[14,773,774,775,192,779,392],{},"If you want more examples, see ",[388,776,778],{"href":777},"\u002Fhow-to\u002Fhow-to-catch-multiple-exceptions-in-python\u002F","how to catch multiple exceptions in Python",[388,780,782],{"href":781},"\u002Ferrors\u002Fzerodivisionerror-division-by-zero-fix","how to fix ZeroDivisionError: division by zero",[23,784,786],{"id":785},"when-to-use-try-except","When to use try-except",[14,788,163,789,791],{},[18,790,20],{}," when you expect a known operation might fail.",[14,793,794],{},"Good examples:",[175,796,797,800,803,806],{},[178,798,799],{},"Reading user input",[178,801,802],{},"Opening files",[178,804,805],{},"Converting text to numbers",[178,807,808],{},"Doing calculations that may divide by zero",[14,810,811],{},"Use it around code you expect might raise a known exception.",[14,813,814,815,817],{},"Avoid wrapping large parts of your program without a clear reason. If too much code is inside one ",[18,816,44],{}," block, it becomes harder to tell which line failed.",[14,819,820],{},"A good rule:",[175,822,823,826,832],{},[178,824,825],{},"Catch only errors you can respond to usefully",[178,827,828,829,831],{},"Keep the ",[18,830,44],{}," block as small as possible",[178,833,834,835],{},"Prefer specific exceptions over a plain ",[18,836,113],{},[23,838,840],{"id":839},"common-beginner-mistakes","Common beginner mistakes",[14,842,843,844,541],{},"Here are some common causes of problems with ",[18,845,20],{},[175,847,848,853,858,865,870],{},[178,849,850,851],{},"Trying to convert invalid text with ",[18,852,409],{},[178,854,855,856,215],{},"Dividing by zero inside the ",[18,857,44],{},[178,859,860,861,864],{},"Catching ",[18,862,863],{},"Exception"," too broadly and hiding the real bug",[178,866,867,868,215],{},"Placing unrelated code inside one large ",[18,869,44],{},[178,871,872,873],{},"Using the wrong exception type in ",[18,874,113],{},[276,876,878],{"id":877},"mistake-using-a-plain-except-block","Mistake: using a plain except block",[28,880,882],{"className":30,"code":881,"language":32,"meta":33,"style":33},"try:\n    number = int(input(\"Enter a number: \"))\n    print(10 \u002F number)\nexcept:\n    print(\"Something went wrong.\")\n",[18,883,884,890,912,926,932],{"__ignoreMap":33},[37,885,886,888],{"class":39,"line":40},[37,887,44],{"class":43},[37,889,48],{"class":47},[37,891,892,894,896,898,900,902,904,906,908,910],{"class":39,"line":51},[37,893,55],{"class":54},[37,895,59],{"class":58},[37,897,63],{"class":62},[37,899,66],{"class":47},[37,901,70],{"class":69},[37,903,66],{"class":47},[37,905,76],{"class":75},[37,907,80],{"class":79},[37,909,76],{"class":75},[37,911,85],{"class":47},[37,913,914,916,918,920,922,924],{"class":39,"line":88},[37,915,91],{"class":69},[37,917,66],{"class":47},[37,919,97],{"class":96},[37,921,100],{"class":58},[37,923,104],{"class":103},[37,925,107],{"class":47},[37,927,928,930],{"class":39,"line":110},[37,929,113],{"class":43},[37,931,48],{"class":47},[37,933,934,936,938,940,943,945],{"class":39,"line":121},[37,935,91],{"class":69},[37,937,66],{"class":47},[37,939,76],{"class":75},[37,941,942],{"class":79},"Something went wrong.",[37,944,76],{"class":75},[37,946,107],{"class":47},[14,948,949],{},"This works, but it is usually not a good idea for beginners.",[14,951,952],{},"Why?",[175,954,955,958,961],{},[178,956,957],{},"It hides the real error type",[178,959,960],{},"It makes debugging harder",[178,962,963],{},"It may catch unexpected bugs you did not mean to handle",[14,965,966],{},"Prefer this:",[28,968,969],{"className":30,"code":31,"language":32,"meta":33,"style":33},[18,970,971,977,999,1013,1021,1035,1043],{"__ignoreMap":33},[37,972,973,975],{"class":39,"line":40},[37,974,44],{"class":43},[37,976,48],{"class":47},[37,978,979,981,983,985,987,989,991,993,995,997],{"class":39,"line":51},[37,980,55],{"class":54},[37,982,59],{"class":58},[37,984,63],{"class":62},[37,986,66],{"class":47},[37,988,70],{"class":69},[37,990,66],{"class":47},[37,992,76],{"class":75},[37,994,80],{"class":79},[37,996,76],{"class":75},[37,998,85],{"class":47},[37,1000,1001,1003,1005,1007,1009,1011],{"class":39,"line":88},[37,1002,91],{"class":69},[37,1004,66],{"class":47},[37,1006,97],{"class":96},[37,1008,100],{"class":58},[37,1010,104],{"class":103},[37,1012,107],{"class":47},[37,1014,1015,1017,1019],{"class":39,"line":110},[37,1016,113],{"class":43},[37,1018,116],{"class":62},[37,1020,48],{"class":47},[37,1022,1023,1025,1027,1029,1031,1033],{"class":39,"line":121},[37,1024,91],{"class":69},[37,1026,66],{"class":47},[37,1028,76],{"class":75},[37,1030,130],{"class":79},[37,1032,76],{"class":75},[37,1034,107],{"class":47},[37,1036,1037,1039,1041],{"class":39,"line":137},[37,1038,113],{"class":43},[37,1040,142],{"class":62},[37,1042,48],{"class":47},[37,1044,1045,1047,1049,1051,1053,1055],{"class":39,"line":147},[37,1046,91],{"class":69},[37,1048,66],{"class":47},[37,1050,76],{"class":75},[37,1052,156],{"class":79},[37,1054,76],{"class":75},[37,1056,107],{"class":47},[276,1058,1060],{"id":1059},"mistake-putting-too-much-code-inside-try","Mistake: putting too much code inside try",[14,1062,1063],{},"Less helpful:",[28,1065,1067],{"className":30,"code":1066,"language":32,"meta":33,"style":33},"try:\n    number = int(input(\"Enter a number: \"))\n    result = 10 \u002F number\n    print(\"Result:\", result)\n    print(\"The length is\", len(result))\nexcept ValueError:\n    print(\"Invalid number.\")\nexcept ZeroDivisionError:\n    print(\"Cannot divide by zero.\")\n",[18,1068,1069,1075,1097,1109,1127,1151,1159,1174,1182],{"__ignoreMap":33},[37,1070,1071,1073],{"class":39,"line":40},[37,1072,44],{"class":43},[37,1074,48],{"class":47},[37,1076,1077,1079,1081,1083,1085,1087,1089,1091,1093,1095],{"class":39,"line":51},[37,1078,55],{"class":54},[37,1080,59],{"class":58},[37,1082,63],{"class":62},[37,1084,66],{"class":47},[37,1086,70],{"class":69},[37,1088,66],{"class":47},[37,1090,76],{"class":75},[37,1092,80],{"class":79},[37,1094,76],{"class":75},[37,1096,85],{"class":47},[37,1098,1099,1101,1103,1105,1107],{"class":39,"line":88},[37,1100,295],{"class":54},[37,1102,59],{"class":58},[37,1104,300],{"class":96},[37,1106,100],{"class":58},[37,1108,677],{"class":54},[37,1110,1111,1113,1115,1117,1119,1121,1123,1125],{"class":39,"line":110},[37,1112,91],{"class":69},[37,1114,66],{"class":47},[37,1116,76],{"class":75},[37,1118,688],{"class":79},[37,1120,76],{"class":75},[37,1122,464],{"class":47},[37,1124,695],{"class":103},[37,1126,107],{"class":47},[37,1128,1129,1131,1133,1135,1138,1140,1142,1145,1147,1149],{"class":39,"line":121},[37,1130,91],{"class":69},[37,1132,66],{"class":47},[37,1134,76],{"class":75},[37,1136,1137],{"class":79},"The length is",[37,1139,76],{"class":75},[37,1141,464],{"class":47},[37,1143,1144],{"class":69}," len",[37,1146,66],{"class":47},[37,1148,314],{"class":103},[37,1150,85],{"class":47},[37,1152,1153,1155,1157],{"class":39,"line":137},[37,1154,113],{"class":43},[37,1156,116],{"class":62},[37,1158,48],{"class":47},[37,1160,1161,1163,1165,1167,1170,1172],{"class":39,"line":147},[37,1162,91],{"class":69},[37,1164,66],{"class":47},[37,1166,76],{"class":75},[37,1168,1169],{"class":79},"Invalid number.",[37,1171,76],{"class":75},[37,1173,107],{"class":47},[37,1175,1176,1178,1180],{"class":39,"line":731},[37,1177,113],{"class":43},[37,1179,142],{"class":62},[37,1181,48],{"class":47},[37,1183,1185,1187,1189,1191,1194,1196],{"class":39,"line":1184},9,[37,1186,91],{"class":69},[37,1188,66],{"class":47},[37,1190,76],{"class":75},[37,1192,1193],{"class":79},"Cannot divide by zero.",[37,1195,76],{"class":75},[37,1197,107],{"class":47},[14,1199,1200,1201,1204,1205,1207],{},"The problem is that ",[18,1202,1203],{},"len(result)"," is also inside the ",[18,1206,44],{}," block. If that line fails, your error handling may not match the real problem clearly.",[14,1209,1210],{},"Keep the risky part focused.",[23,1212,1214],{"id":1213},"how-to-debug-a-try-except-block","How to debug a try-except block",[14,1216,1217,1218,1220],{},"If your ",[18,1219,20],{}," block is not behaving as expected, use these steps:",[175,1222,1223,1226,1232,1235],{},[178,1224,1225],{},"Read the exception name carefully",[178,1227,1228,1229,1231],{},"Temporarily remove the ",[18,1230,113],{}," block to see the full traceback",[178,1233,1234],{},"Print values before the line that fails",[178,1236,1237],{},"Check that you are catching the correct exception type",[14,1239,1240],{},"Useful commands and checks:",[28,1242,1244],{"className":30,"code":1243,"language":32,"meta":33,"style":33},"python script.py\nprint(type(value))\nprint(value)\nhelp(ValueError)\nhelp(ZeroDivisionError)\n",[18,1245,1246,1257,1274,1284,1295],{"__ignoreMap":33},[37,1247,1248,1251,1253],{"class":39,"line":40},[37,1249,1250],{"class":54},"python script",[37,1252,392],{"class":47},[37,1254,1256],{"class":1255},"skxfh","py\n",[37,1258,1259,1262,1264,1267,1269,1272],{"class":39,"line":51},[37,1260,1261],{"class":69},"print",[37,1263,66],{"class":47},[37,1265,1266],{"class":62},"type",[37,1268,66],{"class":47},[37,1270,1271],{"class":103},"value",[37,1273,85],{"class":47},[37,1275,1276,1278,1280,1282],{"class":39,"line":88},[37,1277,1261],{"class":69},[37,1279,66],{"class":47},[37,1281,1271],{"class":103},[37,1283,107],{"class":47},[37,1285,1286,1289,1291,1293],{"class":39,"line":110},[37,1287,1288],{"class":69},"help",[37,1290,66],{"class":47},[37,1292,413],{"class":62},[37,1294,107],{"class":47},[37,1296,1297,1299,1301,1303],{"class":39,"line":121},[37,1298,1288],{"class":69},[37,1300,66],{"class":47},[37,1302,356],{"class":62},[37,1304,107],{"class":47},[276,1306,1308],{"id":1307},"example-debugging-idea","Example debugging idea",[14,1310,1311],{},"If this code does not work:",[28,1313,1315],{"className":30,"code":1314,"language":32,"meta":33,"style":33},"value = input(\"Enter a number: \")\n\ntry:\n    number = int(value)\n    print(10 \u002F number)\nexcept ValueError:\n    print(\"Invalid input.\")\n",[18,1316,1317,1337,1343,1349,1363,1377,1385],{"__ignoreMap":33},[37,1318,1319,1322,1324,1327,1329,1331,1333,1335],{"class":39,"line":40},[37,1320,1321],{"class":54},"value ",[37,1323,59],{"class":58},[37,1325,1326],{"class":69}," input",[37,1328,66],{"class":47},[37,1330,76],{"class":75},[37,1332,80],{"class":79},[37,1334,76],{"class":75},[37,1336,107],{"class":47},[37,1338,1339],{"class":39,"line":51},[37,1340,1342],{"emptyLinePlaceholder":1341},true,"\n",[37,1344,1345,1347],{"class":39,"line":88},[37,1346,44],{"class":43},[37,1348,48],{"class":47},[37,1350,1351,1353,1355,1357,1359,1361],{"class":39,"line":110},[37,1352,55],{"class":54},[37,1354,59],{"class":58},[37,1356,63],{"class":62},[37,1358,66],{"class":47},[37,1360,1271],{"class":103},[37,1362,107],{"class":47},[37,1364,1365,1367,1369,1371,1373,1375],{"class":39,"line":121},[37,1366,91],{"class":69},[37,1368,66],{"class":47},[37,1370,97],{"class":96},[37,1372,100],{"class":58},[37,1374,104],{"class":103},[37,1376,107],{"class":47},[37,1378,1379,1381,1383],{"class":39,"line":137},[37,1380,113],{"class":43},[37,1382,116],{"class":62},[37,1384,48],{"class":47},[37,1386,1387,1389,1391,1393,1396,1398],{"class":39,"line":147},[37,1388,91],{"class":69},[37,1390,66],{"class":47},[37,1392,76],{"class":75},[37,1394,1395],{"class":79},"Invalid input.",[37,1397,76],{"class":75},[37,1399,107],{"class":47},[14,1401,1402],{},"You should ask:",[175,1404,1405,1411],{},[178,1406,1407,1408,1410],{},"Could the value be ",[18,1409,762],{},"?",[178,1412,1413,1414,1410],{},"If so, should you also catch ",[18,1415,356],{},[14,1417,1418],{},"A corrected version:",[28,1420,1422],{"className":30,"code":1421,"language":32,"meta":33,"style":33},"value = input(\"Enter a number: \")\n\ntry:\n    number = int(value)\n    print(10 \u002F number)\nexcept ValueError:\n    print(\"Invalid input.\")\nexcept ZeroDivisionError:\n    print(\"You cannot divide by zero.\")\n",[18,1423,1424,1442,1446,1452,1466,1480,1488,1502,1510],{"__ignoreMap":33},[37,1425,1426,1428,1430,1432,1434,1436,1438,1440],{"class":39,"line":40},[37,1427,1321],{"class":54},[37,1429,59],{"class":58},[37,1431,1326],{"class":69},[37,1433,66],{"class":47},[37,1435,76],{"class":75},[37,1437,80],{"class":79},[37,1439,76],{"class":75},[37,1441,107],{"class":47},[37,1443,1444],{"class":39,"line":51},[37,1445,1342],{"emptyLinePlaceholder":1341},[37,1447,1448,1450],{"class":39,"line":88},[37,1449,44],{"class":43},[37,1451,48],{"class":47},[37,1453,1454,1456,1458,1460,1462,1464],{"class":39,"line":110},[37,1455,55],{"class":54},[37,1457,59],{"class":58},[37,1459,63],{"class":62},[37,1461,66],{"class":47},[37,1463,1271],{"class":103},[37,1465,107],{"class":47},[37,1467,1468,1470,1472,1474,1476,1478],{"class":39,"line":121},[37,1469,91],{"class":69},[37,1471,66],{"class":47},[37,1473,97],{"class":96},[37,1475,100],{"class":58},[37,1477,104],{"class":103},[37,1479,107],{"class":47},[37,1481,1482,1484,1486],{"class":39,"line":137},[37,1483,113],{"class":43},[37,1485,116],{"class":62},[37,1487,48],{"class":47},[37,1489,1490,1492,1494,1496,1498,1500],{"class":39,"line":147},[37,1491,91],{"class":69},[37,1493,66],{"class":47},[37,1495,76],{"class":75},[37,1497,1395],{"class":79},[37,1499,76],{"class":75},[37,1501,107],{"class":47},[37,1503,1504,1506,1508],{"class":39,"line":731},[37,1505,113],{"class":43},[37,1507,142],{"class":62},[37,1509,48],{"class":47},[37,1511,1512,1514,1516,1518,1520,1522],{"class":39,"line":1184},[37,1513,91],{"class":69},[37,1515,66],{"class":47},[37,1517,76],{"class":75},[37,1519,156],{"class":79},[37,1521,76],{"class":75},[37,1523,107],{"class":47},[14,1525,1526,1527,392],{},"If you are seeing a specific conversion error, see ",[388,1528,1530,1531],{"href":1529},"\u002Ferrors\u002Fvalueerror-invalid-literal-for-int-with-base-10-fix","how to fix ",[18,1532,1533],{},"ValueError: invalid literal for int() with base 10",[23,1535,1537],{"id":1536},"faq","FAQ",[276,1539,1541],{"id":1540},"what-does-try-except-do-in-python","What does try-except do in Python?",[14,1543,1544],{},"It lets your program catch an error and handle it with a message or another action instead of stopping immediately.",[276,1546,1548],{"id":1547},"should-i-use-a-plain-except-block","Should I use a plain except block?",[14,1550,1551,1552,1554,1555,392],{},"Usually no. It is better to catch specific exceptions like ",[18,1553,413],{}," or ",[18,1556,356],{},[276,1558,1560],{"id":1559},"can-i-catch-multiple-exceptions","Can I catch multiple exceptions?",[14,1562,1563,1564,1566],{},"Yes. You can use multiple ",[18,1565,113],{}," blocks or catch more than one exception type when the handling is the same.",[276,1568,1570],{"id":1569},"does-try-except-prevent-all-crashes","Does try-except prevent all crashes?",[14,1572,1573],{},"No. It only handles the exceptions you catch, and it does not fix logic mistakes automatically.",[23,1575,1577],{"id":1576},"see-also","See also",[175,1579,1580,1584,1590,1596,1601],{},[178,1581,1582],{},[388,1583,391],{"href":390},[178,1585,1586],{},[388,1587,1589],{"href":1588},"\u002Flearn\u002Fusing-try-except-else-and-finally-in-python","Using try, except, else, and finally in Python",[178,1591,1592],{},[388,1593,1595],{"href":1594},"\u002Fhow-to\u002Fhow-to-handle-exceptions-in-python\u002F","How to handle exceptions in Python",[178,1597,1598],{},[388,1599,1600],{"href":595},"How to convert user input to numbers in Python",[178,1602,1603],{},[388,1604,1605],{"href":599},"Python int() function explained",[14,1607,1608,1609,1611,1612,192,1615,1618],{},"Practice idea: wrap one small input example or file example in a ",[18,1610,20],{}," block first. Then move on to ",[18,1613,1614],{},"else",[18,1616,1617],{},"finally"," when you want more control over what happens after success or failure.",[1620,1621,1622],"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 .srdBf, html code.shiki .srdBf{--shiki-light:#F76D47;--shiki-default:#005CC5;--shiki-dark:#79B8FF}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 .sutJx, html code.shiki .sutJx{--shiki-light:#90A4AE;--shiki-light-font-style:italic;--shiki-default:#6A737D;--shiki-default-font-style:inherit;--shiki-dark:#6A737D;--shiki-dark-font-style:inherit}html pre.shiki code .skxfh, html code.shiki .skxfh{--shiki-light:#E53935;--shiki-default:#24292E;--shiki-dark:#E1E4E8}",{"title":33,"searchDepth":51,"depth":51,"links":1624},[1625,1626,1627,1631,1635,1638,1639,1643,1646,1652],{"id":25,"depth":51,"text":26},{"id":172,"depth":51,"text":173},{"id":200,"depth":51,"text":201,"children":1628},[1629,1630],{"id":278,"depth":88,"text":279},{"id":341,"depth":88,"text":342},{"id":395,"depth":51,"text":396,"children":1632},[1633,1634],{"id":500,"depth":88,"text":501},{"id":533,"depth":88,"text":534},{"id":603,"depth":51,"text":604,"children":1636},[1637],{"id":746,"depth":88,"text":747},{"id":785,"depth":51,"text":786},{"id":839,"depth":51,"text":840,"children":1640},[1641,1642],{"id":877,"depth":88,"text":878},{"id":1059,"depth":88,"text":1060},{"id":1213,"depth":51,"text":1214,"children":1644},[1645],{"id":1307,"depth":88,"text":1308},{"id":1536,"depth":51,"text":1537,"children":1647},[1648,1649,1650,1651],{"id":1540,"depth":88,"text":1541},{"id":1547,"depth":88,"text":1548},{"id":1559,"depth":88,"text":1560},{"id":1569,"depth":88,"text":1570},{"id":1576,"depth":51,"text":1577},"Master how to use try except blocks in python in our comprehensive Python beginner guide.","md",{},"\u002Fhow-to\u002Fhow-to-use-try-except-blocks-in-python",{"title":5,"description":1653},"how-to\u002Fhow-to-use-try-except-blocks-in-python","vS8aYuSO_spJswHEKE7VNuu99Yw9LS-PezvGVkQSa4s",1777585495832]