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