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