[{"data":1,"prerenderedAt":1978},["ShallowReactive",2],{"doc-\u002Fhow-to\u002Fhow-to-catch-multiple-exceptions-in-python":3},{"id":4,"title":5,"body":6,"description":1971,"extension":1972,"meta":1973,"navigation":308,"path":1974,"seo":1975,"stem":1976,"__hash__":1977},"content\u002Fhow-to\u002Fhow-to-catch-multiple-exceptions-in-python.md","How to Catch Multiple Exceptions in Python",{"type":7,"value":8,"toc":1936},"minimark",[9,13,17,20,37,44,47,70,75,197,202,206,228,232,235,238,252,261,265,271,277,282,386,390,419,425,519,523,528,531,542,545,656,659,667,671,677,680,691,694,820,823,830,834,840,843,846,922,925,946,949,953,956,959,1034,1037,1111,1122,1133,1137,1141,1144,1202,1205,1260,1263,1270,1273,1325,1328,1331,1383,1390,1394,1397,1400,1475,1478,1569,1576,1580,1583,1586,1600,1603,1607,1610,1628,1641,1645,1648,1746,1749,1800,1803,1814,1818,1822,1827,1846,1850,1853,1856,1881,1885,1888,1891,1895,1898,1902,1932],[10,11,5],"h1",{"id":12},"how-to-catch-multiple-exceptions-in-python",[14,15,16],"p",{},"Sometimes the same piece of Python code can fail in more than one way.",[14,18,19],{},"For example:",[21,22,23,31],"ul",{},[24,25,26,27],"li",{},"converting text to a number can raise a ",[28,29,30],"code",{},"ValueError",[24,32,33,34],{},"dividing by that number can raise a ",[28,35,36],{},"ZeroDivisionError",[14,38,39,40,43],{},"In cases like this, you can catch multiple exceptions with one ",[28,41,42],{},"except"," block.",[14,45,46],{},"This page shows you:",[21,48,49,56,59,64],{},[24,50,51,52,55],{},"how to catch more than one exception in a ",[28,53,54],{},"try"," block",[24,57,58],{},"the correct syntax for grouped exceptions",[24,60,61,62,55],{},"when to use one ",[28,63,42],{},[24,65,66,67,69],{},"when separate ",[28,68,42],{}," blocks are better",[71,72,74],"h2",{"id":73},"quick-fix","Quick fix",[76,77,82],"pre",{"className":78,"code":79,"language":80,"meta":81,"style":81},"language-python shiki shiki-themes material-theme-lighter github-light github-dark","try:\n    number = int(user_input)\n    result = 10 \u002F number\nexcept (ValueError, ZeroDivisionError) as e:\n    print(f\"Error: {e}\")\n","python","",[28,83,84,96,121,139,166],{"__ignoreMap":81},[85,86,89,92],"span",{"class":87,"line":88},"line",1,[85,90,54],{"class":91},"sVHd0",[85,93,95],{"class":94},"sP7_E",":\n",[85,97,99,103,107,111,114,118],{"class":87,"line":98},2,[85,100,102],{"class":101},"su5hD","    number ",[85,104,106],{"class":105},"smGrS","=",[85,108,110],{"class":109},"sZMiF"," int",[85,112,113],{"class":94},"(",[85,115,117],{"class":116},"slqww","user_input",[85,119,120],{"class":94},")\n",[85,122,124,127,129,133,136],{"class":87,"line":123},3,[85,125,126],{"class":101},"    result ",[85,128,106],{"class":105},[85,130,132],{"class":131},"srdBf"," 10",[85,134,135],{"class":105}," \u002F",[85,137,138],{"class":101}," number\n",[85,140,142,144,147,149,152,155,158,161,164],{"class":87,"line":141},4,[85,143,42],{"class":91},[85,145,146],{"class":94}," (",[85,148,30],{"class":109},[85,150,151],{"class":94},",",[85,153,154],{"class":109}," ZeroDivisionError",[85,156,157],{"class":94},")",[85,159,160],{"class":91}," as",[85,162,163],{"class":101}," e",[85,165,95],{"class":94},[85,167,169,173,175,179,183,186,189,192,195],{"class":87,"line":168},5,[85,170,172],{"class":171},"sptTA","    print",[85,174,113],{"class":94},[85,176,178],{"class":177},"sbsja","f",[85,180,182],{"class":181},"s_sjI","\"Error: ",[85,184,185],{"class":131},"{",[85,187,188],{"class":116},"e",[85,190,191],{"class":131},"}",[85,193,194],{"class":181},"\"",[85,196,120],{"class":94},[14,198,199,200,43],{},"Use parentheses to catch multiple exception types in one ",[28,201,42],{},[71,203,205],{"id":204},"what-this-page-helps-you-do","What this page helps you do",[21,207,208,213,219,225],{},[24,209,210,211,55],{},"Catch more than one exception in a ",[28,212,54],{},[24,214,215,216,218],{},"Use one ",[28,217,42],{}," block for multiple error types",[24,220,221,222,224],{},"Decide when to use separate ",[28,223,42],{}," blocks instead",[24,226,227],{},"Avoid common beginner syntax mistakes",[71,229,231],{"id":230},"when-catching-multiple-exceptions-is-useful","When catching multiple exceptions is useful",[14,233,234],{},"Catching multiple exceptions is useful when one block of code can fail in different expected ways.",[14,236,237],{},"Common examples:",[21,239,240,243,246,249],{},[24,241,242],{},"A single line of code can fail in different ways",[24,244,245],{},"User input often causes more than one kind of error",[24,247,248],{},"File handling and type conversion commonly need this pattern",[24,250,251],{},"It helps keep error handling short and readable",[14,253,254,255,260],{},"For a general introduction to exception handling, see ",[256,257,259],"a",{"href":258},"\u002Fhow-to\u002Fhow-to-use-try-except-blocks-in-python","how to use try and except blocks in Python",".",[71,262,264],{"id":263},"basic-syntax","Basic syntax",[14,266,267,268,270],{},"Use ",[28,269,54],{}," for code that might fail.",[14,272,267,273,276],{},[28,274,275],{},"except (ErrorType1, ErrorType2):"," to catch several exceptions in one block.",[278,279,281],"h3",{"id":280},"example","Example",[76,283,285],{"className":78,"code":284,"language":80,"meta":81,"style":81},"user_input = \"0\"\n\ntry:\n    number = int(user_input)\n    result = 10 \u002F number\n    print(result)\nexcept (ValueError, ZeroDivisionError):\n    print(\"Please enter a valid non-zero number.\")\n",[28,286,287,304,310,316,330,342,354,370],{"__ignoreMap":81},[85,288,289,292,294,298,301],{"class":87,"line":88},[85,290,291],{"class":101},"user_input ",[85,293,106],{"class":105},[85,295,297],{"class":296},"sjJ54"," \"",[85,299,300],{"class":181},"0",[85,302,303],{"class":296},"\"\n",[85,305,306],{"class":87,"line":98},[85,307,309],{"emptyLinePlaceholder":308},true,"\n",[85,311,312,314],{"class":87,"line":123},[85,313,54],{"class":91},[85,315,95],{"class":94},[85,317,318,320,322,324,326,328],{"class":87,"line":141},[85,319,102],{"class":101},[85,321,106],{"class":105},[85,323,110],{"class":109},[85,325,113],{"class":94},[85,327,117],{"class":116},[85,329,120],{"class":94},[85,331,332,334,336,338,340],{"class":87,"line":168},[85,333,126],{"class":101},[85,335,106],{"class":105},[85,337,132],{"class":131},[85,339,135],{"class":105},[85,341,138],{"class":101},[85,343,345,347,349,352],{"class":87,"line":344},6,[85,346,172],{"class":171},[85,348,113],{"class":94},[85,350,351],{"class":116},"result",[85,353,120],{"class":94},[85,355,357,359,361,363,365,367],{"class":87,"line":356},7,[85,358,42],{"class":91},[85,360,146],{"class":94},[85,362,30],{"class":109},[85,364,151],{"class":94},[85,366,154],{"class":109},[85,368,369],{"class":94},"):\n",[85,371,373,375,377,379,382,384],{"class":87,"line":372},8,[85,374,172],{"class":171},[85,376,113],{"class":94},[85,378,194],{"class":296},[85,380,381],{"class":181},"Please enter a valid non-zero number.",[85,383,194],{"class":296},[85,385,120],{"class":94},[278,387,389],{"id":388},"how-it-works","How it works",[21,391,392,398,406,413],{},[24,393,394,397],{},[28,395,396],{},"try:"," contains the risky code",[24,399,400,403,404],{},[28,401,402],{},"int(user_input)"," may raise ",[28,405,30],{},[24,407,408,403,411],{},[28,409,410],{},"10 \u002F number",[28,412,36],{},[24,414,415,416,418],{},"the ",[28,417,42],{}," block catches either error",[14,420,421,422,260],{},"If you want to see the actual error message, use ",[28,423,424],{},"as e",[76,426,428],{"className":78,"code":427,"language":80,"meta":81,"style":81},"user_input = \"hello\"\n\ntry:\n    number = int(user_input)\n    result = 10 \u002F number\nexcept (ValueError, ZeroDivisionError) as e:\n    print(f\"Error: {e}\")\n",[28,429,430,443,447,453,467,479,499],{"__ignoreMap":81},[85,431,432,434,436,438,441],{"class":87,"line":88},[85,433,291],{"class":101},[85,435,106],{"class":105},[85,437,297],{"class":296},[85,439,440],{"class":181},"hello",[85,442,303],{"class":296},[85,444,445],{"class":87,"line":98},[85,446,309],{"emptyLinePlaceholder":308},[85,448,449,451],{"class":87,"line":123},[85,450,54],{"class":91},[85,452,95],{"class":94},[85,454,455,457,459,461,463,465],{"class":87,"line":141},[85,456,102],{"class":101},[85,458,106],{"class":105},[85,460,110],{"class":109},[85,462,113],{"class":94},[85,464,117],{"class":116},[85,466,120],{"class":94},[85,468,469,471,473,475,477],{"class":87,"line":168},[85,470,126],{"class":101},[85,472,106],{"class":105},[85,474,132],{"class":131},[85,476,135],{"class":105},[85,478,138],{"class":101},[85,480,481,483,485,487,489,491,493,495,497],{"class":87,"line":344},[85,482,42],{"class":91},[85,484,146],{"class":94},[85,486,30],{"class":109},[85,488,151],{"class":94},[85,490,154],{"class":109},[85,492,157],{"class":94},[85,494,160],{"class":91},[85,496,163],{"class":101},[85,498,95],{"class":94},[85,500,501,503,505,507,509,511,513,515,517],{"class":87,"line":356},[85,502,172],{"class":171},[85,504,113],{"class":94},[85,506,178],{"class":177},[85,508,182],{"class":181},[85,510,185],{"class":131},[85,512,188],{"class":116},[85,514,191],{"class":131},[85,516,194],{"class":181},[85,518,120],{"class":94},[71,520,522],{"id":521},"single-except-block-for-multiple-exceptions","Single except block for multiple exceptions",[14,524,215,525,527],{},[28,526,42],{}," block when the response should be the same for all the errors you expect.",[14,529,530],{},"This is best when:",[21,532,533,536,539],{},[24,534,535],{},"the fix is the same",[24,537,538],{},"the message to the user is the same",[24,540,541],{},"you want to avoid repeating code",[278,543,281],{"id":544},"example-1",[76,546,548],{"className":78,"code":547,"language":80,"meta":81,"style":81},"user_input = input(\"Enter a number: \")\n\ntry:\n    number = int(user_input)\n    result = 100 \u002F number\n    print(\"Result:\", result)\nexcept (ValueError, ZeroDivisionError):\n    print(\"Please enter a valid number that is not zero.\")\n",[28,549,550,570,574,580,594,607,627,641],{"__ignoreMap":81},[85,551,552,554,556,559,561,563,566,568],{"class":87,"line":88},[85,553,291],{"class":101},[85,555,106],{"class":105},[85,557,558],{"class":171}," input",[85,560,113],{"class":94},[85,562,194],{"class":296},[85,564,565],{"class":181},"Enter a number: ",[85,567,194],{"class":296},[85,569,120],{"class":94},[85,571,572],{"class":87,"line":98},[85,573,309],{"emptyLinePlaceholder":308},[85,575,576,578],{"class":87,"line":123},[85,577,54],{"class":91},[85,579,95],{"class":94},[85,581,582,584,586,588,590,592],{"class":87,"line":141},[85,583,102],{"class":101},[85,585,106],{"class":105},[85,587,110],{"class":109},[85,589,113],{"class":94},[85,591,117],{"class":116},[85,593,120],{"class":94},[85,595,596,598,600,603,605],{"class":87,"line":168},[85,597,126],{"class":101},[85,599,106],{"class":105},[85,601,602],{"class":131}," 100",[85,604,135],{"class":105},[85,606,138],{"class":101},[85,608,609,611,613,615,618,620,622,625],{"class":87,"line":344},[85,610,172],{"class":171},[85,612,113],{"class":94},[85,614,194],{"class":296},[85,616,617],{"class":181},"Result:",[85,619,194],{"class":296},[85,621,151],{"class":94},[85,623,624],{"class":116}," result",[85,626,120],{"class":94},[85,628,629,631,633,635,637,639],{"class":87,"line":356},[85,630,42],{"class":91},[85,632,146],{"class":94},[85,634,30],{"class":109},[85,636,151],{"class":94},[85,638,154],{"class":109},[85,640,369],{"class":94},[85,642,643,645,647,649,652,654],{"class":87,"line":372},[85,644,172],{"class":171},[85,646,113],{"class":94},[85,648,194],{"class":296},[85,650,651],{"class":181},"Please enter a valid number that is not zero.",[85,653,194],{"class":296},[85,655,120],{"class":94},[14,657,658],{},"This works well because both errors lead to the same advice:",[21,660,661,664],{},[24,662,663],{},"if the input is not a number, the user should enter a valid number",[24,665,666],{},"if the input is zero, the user should enter a non-zero number",[71,668,670],{"id":669},"using-separate-except-blocks","Using separate except blocks",[14,672,673,674,676],{},"Use separate ",[28,675,42],{}," blocks when each error needs a different message or different handling.",[14,678,679],{},"This is often clearer when:",[21,681,682,685,688],{},[24,683,684],{},"the solution depends on the error type",[24,686,687],{},"you want more helpful feedback",[24,689,690],{},"you are debugging and need to know exactly what failed",[278,692,281],{"id":693},"example-2",[76,695,697],{"className":78,"code":696,"language":80,"meta":81,"style":81},"user_input = input(\"Enter a number: \")\n\ntry:\n    number = int(user_input)\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",[28,698,699,717,721,727,741,753,771,780,795,804],{"__ignoreMap":81},[85,700,701,703,705,707,709,711,713,715],{"class":87,"line":88},[85,702,291],{"class":101},[85,704,106],{"class":105},[85,706,558],{"class":171},[85,708,113],{"class":94},[85,710,194],{"class":296},[85,712,565],{"class":181},[85,714,194],{"class":296},[85,716,120],{"class":94},[85,718,719],{"class":87,"line":98},[85,720,309],{"emptyLinePlaceholder":308},[85,722,723,725],{"class":87,"line":123},[85,724,54],{"class":91},[85,726,95],{"class":94},[85,728,729,731,733,735,737,739],{"class":87,"line":141},[85,730,102],{"class":101},[85,732,106],{"class":105},[85,734,110],{"class":109},[85,736,113],{"class":94},[85,738,117],{"class":116},[85,740,120],{"class":94},[85,742,743,745,747,749,751],{"class":87,"line":168},[85,744,126],{"class":101},[85,746,106],{"class":105},[85,748,602],{"class":131},[85,750,135],{"class":105},[85,752,138],{"class":101},[85,754,755,757,759,761,763,765,767,769],{"class":87,"line":344},[85,756,172],{"class":171},[85,758,113],{"class":94},[85,760,194],{"class":296},[85,762,617],{"class":181},[85,764,194],{"class":296},[85,766,151],{"class":94},[85,768,624],{"class":116},[85,770,120],{"class":94},[85,772,773,775,778],{"class":87,"line":356},[85,774,42],{"class":91},[85,776,777],{"class":109}," ValueError",[85,779,95],{"class":94},[85,781,782,784,786,788,791,793],{"class":87,"line":372},[85,783,172],{"class":171},[85,785,113],{"class":94},[85,787,194],{"class":296},[85,789,790],{"class":181},"That was not a valid number.",[85,792,194],{"class":296},[85,794,120],{"class":94},[85,796,798,800,802],{"class":87,"line":797},9,[85,799,42],{"class":91},[85,801,154],{"class":109},[85,803,95],{"class":94},[85,805,807,809,811,813,816,818],{"class":87,"line":806},10,[85,808,172],{"class":171},[85,810,113],{"class":94},[85,812,194],{"class":296},[85,814,815],{"class":181},"You cannot divide by zero.",[85,817,194],{"class":296},[85,819,120],{"class":94},[14,821,822],{},"This version is better if you want specific feedback.",[14,824,825,826,260],{},"If you are still learning the full structure of exception handling, see ",[256,827,829],{"href":828},"\u002Flearn\u002Fusing-try-except-else-and-finally-in-python","using try, except, else, and finally in Python",[71,831,833],{"id":832},"how-python-checks-except-blocks","How Python checks except blocks",[14,835,836,837,839],{},"Python checks ",[28,838,42],{}," blocks from top to bottom.",[14,841,842],{},"The first matching block runs.",[278,844,281],{"id":845},"example-3",[76,847,849],{"className":78,"code":848,"language":80,"meta":81,"style":81},"try:\n    number = int(\"hello\")\nexcept ValueError:\n    print(\"This is a ValueError.\")\nexcept Exception:\n    print(\"This catches many other errors.\")\n",[28,850,851,857,875,883,898,907],{"__ignoreMap":81},[85,852,853,855],{"class":87,"line":88},[85,854,54],{"class":91},[85,856,95],{"class":94},[85,858,859,861,863,865,867,869,871,873],{"class":87,"line":98},[85,860,102],{"class":101},[85,862,106],{"class":105},[85,864,110],{"class":109},[85,866,113],{"class":94},[85,868,194],{"class":296},[85,870,440],{"class":181},[85,872,194],{"class":296},[85,874,120],{"class":94},[85,876,877,879,881],{"class":87,"line":123},[85,878,42],{"class":91},[85,880,777],{"class":109},[85,882,95],{"class":94},[85,884,885,887,889,891,894,896],{"class":87,"line":141},[85,886,172],{"class":171},[85,888,113],{"class":94},[85,890,194],{"class":296},[85,892,893],{"class":181},"This is a ValueError.",[85,895,194],{"class":296},[85,897,120],{"class":94},[85,899,900,902,905],{"class":87,"line":168},[85,901,42],{"class":91},[85,903,904],{"class":109}," Exception",[85,906,95],{"class":94},[85,908,909,911,913,915,918,920],{"class":87,"line":344},[85,910,172],{"class":171},[85,912,113],{"class":94},[85,914,194],{"class":296},[85,916,917],{"class":181},"This catches many other errors.",[85,919,194],{"class":296},[85,921,120],{"class":94},[14,923,924],{},"Output:",[76,926,928],{"className":78,"code":927,"language":80,"meta":81,"style":81},"This is a ValueError.\n",[28,929,930],{"__ignoreMap":81},[85,931,932,935,938,941,943],{"class":87,"line":88},[85,933,934],{"class":101},"This ",[85,936,937],{"class":105},"is",[85,939,940],{"class":101}," a ",[85,942,30],{"class":109},[85,944,945],{"class":94},".\n",[14,947,948],{},"Python stops at the first match.",[278,950,952],{"id":951},"important-rule","Important rule",[14,954,955],{},"Put more specific exceptions before broader ones.",[14,957,958],{},"Good:",[76,960,962],{"className":78,"code":961,"language":80,"meta":81,"style":81},"try:\n    number = int(\"hello\")\nexcept ValueError:\n    print(\"Invalid number\")\nexcept Exception:\n    print(\"Something else went wrong\")\n",[28,963,964,970,988,996,1011,1019],{"__ignoreMap":81},[85,965,966,968],{"class":87,"line":88},[85,967,54],{"class":91},[85,969,95],{"class":94},[85,971,972,974,976,978,980,982,984,986],{"class":87,"line":98},[85,973,102],{"class":101},[85,975,106],{"class":105},[85,977,110],{"class":109},[85,979,113],{"class":94},[85,981,194],{"class":296},[85,983,440],{"class":181},[85,985,194],{"class":296},[85,987,120],{"class":94},[85,989,990,992,994],{"class":87,"line":123},[85,991,42],{"class":91},[85,993,777],{"class":109},[85,995,95],{"class":94},[85,997,998,1000,1002,1004,1007,1009],{"class":87,"line":141},[85,999,172],{"class":171},[85,1001,113],{"class":94},[85,1003,194],{"class":296},[85,1005,1006],{"class":181},"Invalid number",[85,1008,194],{"class":296},[85,1010,120],{"class":94},[85,1012,1013,1015,1017],{"class":87,"line":168},[85,1014,42],{"class":91},[85,1016,904],{"class":109},[85,1018,95],{"class":94},[85,1020,1021,1023,1025,1027,1030,1032],{"class":87,"line":344},[85,1022,172],{"class":171},[85,1024,113],{"class":94},[85,1026,194],{"class":296},[85,1028,1029],{"class":181},"Something else went wrong",[85,1031,194],{"class":296},[85,1033,120],{"class":94},[14,1035,1036],{},"Less helpful:",[76,1038,1040],{"className":78,"code":1039,"language":80,"meta":81,"style":81},"try:\n    number = int(\"hello\")\nexcept Exception:\n    print(\"Something went wrong\")\nexcept ValueError:\n    print(\"Invalid number\")\n",[28,1041,1042,1048,1066,1074,1089,1097],{"__ignoreMap":81},[85,1043,1044,1046],{"class":87,"line":88},[85,1045,54],{"class":91},[85,1047,95],{"class":94},[85,1049,1050,1052,1054,1056,1058,1060,1062,1064],{"class":87,"line":98},[85,1051,102],{"class":101},[85,1053,106],{"class":105},[85,1055,110],{"class":109},[85,1057,113],{"class":94},[85,1059,194],{"class":296},[85,1061,440],{"class":181},[85,1063,194],{"class":296},[85,1065,120],{"class":94},[85,1067,1068,1070,1072],{"class":87,"line":123},[85,1069,42],{"class":91},[85,1071,904],{"class":109},[85,1073,95],{"class":94},[85,1075,1076,1078,1080,1082,1085,1087],{"class":87,"line":141},[85,1077,172],{"class":171},[85,1079,113],{"class":94},[85,1081,194],{"class":296},[85,1083,1084],{"class":181},"Something went wrong",[85,1086,194],{"class":296},[85,1088,120],{"class":94},[85,1090,1091,1093,1095],{"class":87,"line":168},[85,1092,42],{"class":91},[85,1094,777],{"class":109},[85,1096,95],{"class":94},[85,1098,1099,1101,1103,1105,1107,1109],{"class":87,"line":344},[85,1100,172],{"class":171},[85,1102,113],{"class":94},[85,1104,194],{"class":296},[85,1106,1006],{"class":181},[85,1108,194],{"class":296},[85,1110,120],{"class":94},[14,1112,1113,1114,1117,1118,1121],{},"In the second example, ",[28,1115,1116],{},"except ValueError"," will never run because ",[28,1119,1120],{},"Exception"," catches it first.",[14,1123,1124,1125,1128,1129,260],{},"A broad ",[28,1126,1127],{},"except Exception"," can hide useful details if you place it too early. Beginners should usually catch specific exceptions first. You can learn more in ",[256,1130,1132],{"href":1131},"\u002Fhow-to\u002Fhow-to-handle-exceptions-in-python","how to handle exceptions in Python",[71,1134,1136],{"id":1135},"common-beginner-mistakes","Common beginner mistakes",[278,1138,1140],{"id":1139},"forgetting-parentheses","Forgetting parentheses",[14,1142,1143],{},"This is correct:",[76,1145,1147],{"className":78,"code":1146,"language":80,"meta":81,"style":81},"try:\n    number = int(\"hello\")\nexcept (ValueError, ZeroDivisionError):\n    print(\"Handled error\")\n",[28,1148,1149,1155,1173,1187],{"__ignoreMap":81},[85,1150,1151,1153],{"class":87,"line":88},[85,1152,54],{"class":91},[85,1154,95],{"class":94},[85,1156,1157,1159,1161,1163,1165,1167,1169,1171],{"class":87,"line":98},[85,1158,102],{"class":101},[85,1160,106],{"class":105},[85,1162,110],{"class":109},[85,1164,113],{"class":94},[85,1166,194],{"class":296},[85,1168,440],{"class":181},[85,1170,194],{"class":296},[85,1172,120],{"class":94},[85,1174,1175,1177,1179,1181,1183,1185],{"class":87,"line":123},[85,1176,42],{"class":91},[85,1178,146],{"class":94},[85,1180,30],{"class":109},[85,1182,151],{"class":94},[85,1184,154],{"class":109},[85,1186,369],{"class":94},[85,1188,1189,1191,1193,1195,1198,1200],{"class":87,"line":141},[85,1190,172],{"class":171},[85,1192,113],{"class":94},[85,1194,194],{"class":296},[85,1196,1197],{"class":181},"Handled error",[85,1199,194],{"class":296},[85,1201,120],{"class":94},[14,1203,1204],{},"This is not correct in modern Python:",[76,1206,1208],{"className":78,"code":1207,"language":80,"meta":81,"style":81},"try:\n    number = int(\"hello\")\nexcept ValueError, ZeroDivisionError:\n    print(\"Handled error\")\n",[28,1209,1210,1216,1234,1246],{"__ignoreMap":81},[85,1211,1212,1214],{"class":87,"line":88},[85,1213,54],{"class":91},[85,1215,95],{"class":94},[85,1217,1218,1220,1222,1224,1226,1228,1230,1232],{"class":87,"line":98},[85,1219,102],{"class":101},[85,1221,106],{"class":105},[85,1223,110],{"class":109},[85,1225,113],{"class":94},[85,1227,194],{"class":296},[85,1229,440],{"class":181},[85,1231,194],{"class":296},[85,1233,120],{"class":94},[85,1235,1236,1238,1240,1242,1244],{"class":87,"line":123},[85,1237,42],{"class":91},[85,1239,777],{"class":109},[85,1241,151],{"class":94},[85,1243,154],{"class":109},[85,1245,95],{"class":94},[85,1247,1248,1250,1252,1254,1256,1258],{"class":87,"line":141},[85,1249,172],{"class":171},[85,1251,113],{"class":94},[85,1253,194],{"class":296},[85,1255,1197],{"class":181},[85,1257,194],{"class":296},[85,1259,120],{"class":94},[14,1261,1262],{},"When you catch multiple exception types in one block, put them inside parentheses.",[278,1264,1266,1267,1269],{"id":1265},"catching-exception-too-early","Catching ",[28,1268,1120],{}," too early",[14,1271,1272],{},"This catches too much:",[76,1274,1276],{"className":78,"code":1275,"language":80,"meta":81,"style":81},"try:\n    number = int(\"hello\")\nexcept Exception:\n    print(\"Error\")\n",[28,1277,1278,1284,1302,1310],{"__ignoreMap":81},[85,1279,1280,1282],{"class":87,"line":88},[85,1281,54],{"class":91},[85,1283,95],{"class":94},[85,1285,1286,1288,1290,1292,1294,1296,1298,1300],{"class":87,"line":98},[85,1287,102],{"class":101},[85,1289,106],{"class":105},[85,1291,110],{"class":109},[85,1293,113],{"class":94},[85,1295,194],{"class":296},[85,1297,440],{"class":181},[85,1299,194],{"class":296},[85,1301,120],{"class":94},[85,1303,1304,1306,1308],{"class":87,"line":123},[85,1305,42],{"class":91},[85,1307,904],{"class":109},[85,1309,95],{"class":94},[85,1311,1312,1314,1316,1318,1321,1323],{"class":87,"line":141},[85,1313,172],{"class":171},[85,1315,113],{"class":94},[85,1317,194],{"class":296},[85,1319,1320],{"class":181},"Error",[85,1322,194],{"class":296},[85,1324,120],{"class":94},[14,1326,1327],{},"It works, but it hides which error you expected.",[14,1329,1330],{},"This is usually better:",[76,1332,1334],{"className":78,"code":1333,"language":80,"meta":81,"style":81},"try:\n    number = int(\"hello\")\nexcept ValueError:\n    print(\"Please enter a valid number.\")\n",[28,1335,1336,1342,1360,1368],{"__ignoreMap":81},[85,1337,1338,1340],{"class":87,"line":88},[85,1339,54],{"class":91},[85,1341,95],{"class":94},[85,1343,1344,1346,1348,1350,1352,1354,1356,1358],{"class":87,"line":98},[85,1345,102],{"class":101},[85,1347,106],{"class":105},[85,1349,110],{"class":109},[85,1351,113],{"class":94},[85,1353,194],{"class":296},[85,1355,440],{"class":181},[85,1357,194],{"class":296},[85,1359,120],{"class":94},[85,1361,1362,1364,1366],{"class":87,"line":123},[85,1363,42],{"class":91},[85,1365,777],{"class":109},[85,1367,95],{"class":94},[85,1369,1370,1372,1374,1376,1379,1381],{"class":87,"line":141},[85,1371,172],{"class":171},[85,1373,113],{"class":94},[85,1375,194],{"class":296},[85,1377,1378],{"class":181},"Please enter a valid number.",[85,1380,194],{"class":296},[85,1382,120],{"class":94},[14,1384,1385,1386,260],{},"If you want to understand this error better, see ",[256,1387,1389],{"href":1388},"\u002Ferrors\u002Fvalueerror-in-python-causes-and-fixes","ValueError in Python: causes and fixes",[278,1391,1393],{"id":1392},"grouping-exceptions-that-need-different-handling","Grouping exceptions that need different handling",[14,1395,1396],{},"Do not group exceptions just because you can.",[14,1398,1399],{},"Bad fit:",[76,1401,1403],{"className":78,"code":1402,"language":80,"meta":81,"style":81},"try:\n    number = int(input(\"Enter a number: \"))\n    result = 10 \u002F number\nexcept (ValueError, ZeroDivisionError):\n    print(\"Error\")\n",[28,1404,1405,1411,1435,1447,1461],{"__ignoreMap":81},[85,1406,1407,1409],{"class":87,"line":88},[85,1408,54],{"class":91},[85,1410,95],{"class":94},[85,1412,1413,1415,1417,1419,1421,1424,1426,1428,1430,1432],{"class":87,"line":98},[85,1414,102],{"class":101},[85,1416,106],{"class":105},[85,1418,110],{"class":109},[85,1420,113],{"class":94},[85,1422,1423],{"class":171},"input",[85,1425,113],{"class":94},[85,1427,194],{"class":296},[85,1429,565],{"class":181},[85,1431,194],{"class":296},[85,1433,1434],{"class":94},"))\n",[85,1436,1437,1439,1441,1443,1445],{"class":87,"line":123},[85,1438,126],{"class":101},[85,1440,106],{"class":105},[85,1442,132],{"class":131},[85,1444,135],{"class":105},[85,1446,138],{"class":101},[85,1448,1449,1451,1453,1455,1457,1459],{"class":87,"line":141},[85,1450,42],{"class":91},[85,1452,146],{"class":94},[85,1454,30],{"class":109},[85,1456,151],{"class":94},[85,1458,154],{"class":109},[85,1460,369],{"class":94},[85,1462,1463,1465,1467,1469,1471,1473],{"class":87,"line":168},[85,1464,172],{"class":171},[85,1466,113],{"class":94},[85,1468,194],{"class":296},[85,1470,1320],{"class":181},[85,1472,194],{"class":296},[85,1474,120],{"class":94},[14,1476,1477],{},"Better:",[76,1479,1481],{"className":78,"code":1480,"language":80,"meta":81,"style":81},"try:\n    number = int(input(\"Enter a number: \"))\n    result = 10 \u002F number\nexcept ValueError:\n    print(\"Please enter digits only.\")\nexcept ZeroDivisionError:\n    print(\"Zero is not allowed.\")\n",[28,1482,1483,1489,1511,1523,1531,1546,1554],{"__ignoreMap":81},[85,1484,1485,1487],{"class":87,"line":88},[85,1486,54],{"class":91},[85,1488,95],{"class":94},[85,1490,1491,1493,1495,1497,1499,1501,1503,1505,1507,1509],{"class":87,"line":98},[85,1492,102],{"class":101},[85,1494,106],{"class":105},[85,1496,110],{"class":109},[85,1498,113],{"class":94},[85,1500,1423],{"class":171},[85,1502,113],{"class":94},[85,1504,194],{"class":296},[85,1506,565],{"class":181},[85,1508,194],{"class":296},[85,1510,1434],{"class":94},[85,1512,1513,1515,1517,1519,1521],{"class":87,"line":123},[85,1514,126],{"class":101},[85,1516,106],{"class":105},[85,1518,132],{"class":131},[85,1520,135],{"class":105},[85,1522,138],{"class":101},[85,1524,1525,1527,1529],{"class":87,"line":141},[85,1526,42],{"class":91},[85,1528,777],{"class":109},[85,1530,95],{"class":94},[85,1532,1533,1535,1537,1539,1542,1544],{"class":87,"line":168},[85,1534,172],{"class":171},[85,1536,113],{"class":94},[85,1538,194],{"class":296},[85,1540,1541],{"class":181},"Please enter digits only.",[85,1543,194],{"class":296},[85,1545,120],{"class":94},[85,1547,1548,1550,1552],{"class":87,"line":344},[85,1549,42],{"class":91},[85,1551,154],{"class":109},[85,1553,95],{"class":94},[85,1555,1556,1558,1560,1562,1565,1567],{"class":87,"line":356},[85,1557,172],{"class":171},[85,1559,113],{"class":94},[85,1561,194],{"class":296},[85,1563,1564],{"class":181},"Zero is not allowed.",[85,1566,194],{"class":296},[85,1568,120],{"class":94},[14,1570,1571,1572,260],{},"If you need help with division errors, see ",[256,1573,1575],{"href":1574},"\u002Ferrors\u002Fzerodivisionerror-division-by-zero-fix","ZeroDivisionError: division by zero fix",[71,1577,1579],{"id":1578},"when-not-to-catch-multiple-exceptions-together","When not to catch multiple exceptions together",[14,1581,1582],{},"Do not group exceptions if that makes your code less clear.",[14,1584,1585],{},"Avoid grouping errors together when:",[21,1587,1588,1591,1594,1597],{},[24,1589,1590],{},"the user needs different guidance for each one",[24,1592,1593],{},"you might hide a real programming mistake",[24,1595,1596],{},"simple input validation would solve the problem first",[24,1598,1599],{},"you are catching errors you do not understand",[14,1601,1602],{},"For example, if you know a value should be checked before division, input validation may be clearer than relying only on exceptions.",[71,1604,1606],{"id":1605},"common-causes","Common causes",[14,1608,1609],{},"You will often need this pattern in situations like these:",[21,1611,1612,1619,1622,1625],{},[24,1613,1614,1615,1618],{},"Converting user input with ",[28,1616,1617],{},"int()"," when the input is not a number",[24,1620,1621],{},"Dividing by zero after converting input",[24,1623,1624],{},"Opening files when the file may not exist or access may fail",[24,1626,1627],{},"Calling code that can raise more than one expected exception",[14,1629,1630,1631,1633,1634,260],{},"If ",[28,1632,1617],{}," is part of your code, you may also want to read ",[256,1635,1637,1638,1640],{"href":1636},"\u002Freference\u002Fpython-int-function-explained","the Python ",[28,1639,1617],{}," function explained",[71,1642,1644],{"id":1643},"debugging-tips","Debugging tips",[14,1646,1647],{},"If you are not sure which exception happened, inspect the error object.",[76,1649,1651],{"className":78,"code":1650,"language":80,"meta":81,"style":81},"user_input = \"hello\"\n\ntry:\n    number = int(user_input)\n    result = 10 \u002F number\nexcept (ValueError, ZeroDivisionError) as e:\n    print(type(e))\n    print(e)\n",[28,1652,1653,1665,1669,1675,1689,1701,1721,1736],{"__ignoreMap":81},[85,1654,1655,1657,1659,1661,1663],{"class":87,"line":88},[85,1656,291],{"class":101},[85,1658,106],{"class":105},[85,1660,297],{"class":296},[85,1662,440],{"class":181},[85,1664,303],{"class":296},[85,1666,1667],{"class":87,"line":98},[85,1668,309],{"emptyLinePlaceholder":308},[85,1670,1671,1673],{"class":87,"line":123},[85,1672,54],{"class":91},[85,1674,95],{"class":94},[85,1676,1677,1679,1681,1683,1685,1687],{"class":87,"line":141},[85,1678,102],{"class":101},[85,1680,106],{"class":105},[85,1682,110],{"class":109},[85,1684,113],{"class":94},[85,1686,117],{"class":116},[85,1688,120],{"class":94},[85,1690,1691,1693,1695,1697,1699],{"class":87,"line":168},[85,1692,126],{"class":101},[85,1694,106],{"class":105},[85,1696,132],{"class":131},[85,1698,135],{"class":105},[85,1700,138],{"class":101},[85,1702,1703,1705,1707,1709,1711,1713,1715,1717,1719],{"class":87,"line":344},[85,1704,42],{"class":91},[85,1706,146],{"class":94},[85,1708,30],{"class":109},[85,1710,151],{"class":94},[85,1712,154],{"class":109},[85,1714,157],{"class":94},[85,1716,160],{"class":91},[85,1718,163],{"class":101},[85,1720,95],{"class":94},[85,1722,1723,1725,1727,1730,1732,1734],{"class":87,"line":356},[85,1724,172],{"class":171},[85,1726,113],{"class":94},[85,1728,1729],{"class":109},"type",[85,1731,113],{"class":94},[85,1733,188],{"class":116},[85,1735,1434],{"class":94},[85,1737,1738,1740,1742,1744],{"class":87,"line":372},[85,1739,172],{"class":171},[85,1741,113],{"class":94},[85,1743,188],{"class":116},[85,1745,120],{"class":94},[14,1747,1748],{},"Useful debugging commands:",[76,1750,1752],{"className":78,"code":1751,"language":80,"meta":81,"style":81},"print(type(e))\nprint(e)\nhelp(ValueError)\nhelp(ZeroDivisionError)\n",[28,1753,1754,1769,1779,1790],{"__ignoreMap":81},[85,1755,1756,1759,1761,1763,1765,1767],{"class":87,"line":88},[85,1757,1758],{"class":171},"print",[85,1760,113],{"class":94},[85,1762,1729],{"class":109},[85,1764,113],{"class":94},[85,1766,188],{"class":116},[85,1768,1434],{"class":94},[85,1770,1771,1773,1775,1777],{"class":87,"line":98},[85,1772,1758],{"class":171},[85,1774,113],{"class":94},[85,1776,188],{"class":116},[85,1778,120],{"class":94},[85,1780,1781,1784,1786,1788],{"class":87,"line":123},[85,1782,1783],{"class":171},"help",[85,1785,113],{"class":94},[85,1787,30],{"class":109},[85,1789,120],{"class":94},[85,1791,1792,1794,1796,1798],{"class":87,"line":141},[85,1793,1783],{"class":171},[85,1795,113],{"class":94},[85,1797,36],{"class":109},[85,1799,120],{"class":94},[14,1801,1802],{},"These help you learn:",[21,1804,1805,1808,1811],{},[24,1806,1807],{},"the exact exception type",[24,1809,1810],{},"the error message",[24,1812,1813],{},"what the exception means",[71,1815,1817],{"id":1816},"faq","FAQ",[278,1819,1821],{"id":1820},"how-do-you-catch-two-exceptions-in-python","How do you catch two exceptions in Python?",[14,1823,215,1824,1826],{},[28,1825,42],{}," block with parentheses:",[76,1828,1830],{"className":78,"code":1829,"language":80,"meta":81,"style":81},"except (ValueError, ZeroDivisionError):\n",[28,1831,1832],{"__ignoreMap":81},[85,1833,1834,1836,1838,1840,1842,1844],{"class":87,"line":88},[85,1835,42],{"class":91},[85,1837,146],{"class":94},[85,1839,30],{"class":109},[85,1841,151],{"class":94},[85,1843,154],{"class":109},[85,1845,369],{"class":94},[278,1847,1849],{"id":1848},"can-i-use-as-e-when-catching-multiple-exceptions","Can I use as e when catching multiple exceptions?",[14,1851,1852],{},"Yes.",[14,1854,1855],{},"Example:",[76,1857,1859],{"className":78,"code":1858,"language":80,"meta":81,"style":81},"except (ValueError, ZeroDivisionError) as e:\n",[28,1860,1861],{"__ignoreMap":81},[85,1862,1863,1865,1867,1869,1871,1873,1875,1877,1879],{"class":87,"line":88},[85,1864,42],{"class":91},[85,1866,146],{"class":94},[85,1868,30],{"class":109},[85,1870,151],{"class":94},[85,1872,154],{"class":109},[85,1874,157],{"class":94},[85,1876,160],{"class":91},[85,1878,163],{"class":101},[85,1880,95],{"class":94},[278,1882,1884],{"id":1883},"should-i-use-one-except-block-or-separate-ones","Should I use one except block or separate ones?",[14,1886,1887],{},"Use one block if the response is the same.",[14,1889,1890],{},"Use separate blocks if each error needs a different message or fix.",[278,1892,1894],{"id":1893},"can-i-catch-all-errors-with-exception","Can I catch all errors with Exception?",[14,1896,1897],{},"Yes, but beginners should prefer specific exceptions first so errors are easier to understand and debug.",[71,1899,1901],{"id":1900},"see-also","See also",[21,1903,1904,1909,1914,1919,1923,1927],{},[24,1905,1906],{},[256,1907,1908],{"href":258},"How to use try and except blocks in Python",[24,1910,1911],{},[256,1912,1913],{"href":828},"Using try, except, else, and finally in Python",[24,1915,1916],{},[256,1917,1918],{"href":1131},"How to handle exceptions in Python",[24,1920,1921],{},[256,1922,1389],{"href":1388},[24,1924,1925],{},[256,1926,1575],{"href":1574},[24,1928,1929],{},[256,1930,1931],{"href":1636},"Python int() function explained",[1933,1934,1935],"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 .slqww, html code.shiki .slqww{--shiki-light:#6182B8;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .srdBf, html code.shiki .srdBf{--shiki-light:#F76D47;--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 .sbsja, html code.shiki .sbsja{--shiki-light:#9C3EDA;--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .s_sjI, html code.shiki .s_sjI{--shiki-light:#91B859;--shiki-default:#032F62;--shiki-dark:#9ECBFF}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 .sjJ54, html code.shiki .sjJ54{--shiki-light:#39ADB5;--shiki-default:#032F62;--shiki-dark:#9ECBFF}",{"title":81,"searchDepth":98,"depth":98,"links":1937},[1938,1939,1940,1941,1945,1948,1951,1955,1961,1962,1963,1964,1970],{"id":73,"depth":98,"text":74},{"id":204,"depth":98,"text":205},{"id":230,"depth":98,"text":231},{"id":263,"depth":98,"text":264,"children":1942},[1943,1944],{"id":280,"depth":123,"text":281},{"id":388,"depth":123,"text":389},{"id":521,"depth":98,"text":522,"children":1946},[1947],{"id":544,"depth":123,"text":281},{"id":669,"depth":98,"text":670,"children":1949},[1950],{"id":693,"depth":123,"text":281},{"id":832,"depth":98,"text":833,"children":1952},[1953,1954],{"id":845,"depth":123,"text":281},{"id":951,"depth":123,"text":952},{"id":1135,"depth":98,"text":1136,"children":1956},[1957,1958,1960],{"id":1139,"depth":123,"text":1140},{"id":1265,"depth":123,"text":1959},"Catching Exception too early",{"id":1392,"depth":123,"text":1393},{"id":1578,"depth":98,"text":1579},{"id":1605,"depth":98,"text":1606},{"id":1643,"depth":98,"text":1644},{"id":1816,"depth":98,"text":1817,"children":1965},[1966,1967,1968,1969],{"id":1820,"depth":123,"text":1821},{"id":1848,"depth":123,"text":1849},{"id":1883,"depth":123,"text":1884},{"id":1893,"depth":123,"text":1894},{"id":1900,"depth":98,"text":1901},"Master how to catch multiple exceptions in python in our comprehensive Python beginner guide.","md",{},"\u002Fhow-to\u002Fhow-to-catch-multiple-exceptions-in-python",{"title":5,"description":1971},"how-to\u002Fhow-to-catch-multiple-exceptions-in-python","-Dkl-IW-v_jveI1_k6B5D1R6GomZNzpe5sCsLSx1igs",1777585508690]