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