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