[{"data":1,"prerenderedAt":2204},["ShallowReactive",2],{"doc-\u002Fhow-to\u002Fhow-to-raise-an-exception-in-python":3},{"id":4,"title":5,"body":6,"description":2197,"extension":2198,"meta":2199,"navigation":64,"path":2200,"seo":2201,"stem":2202,"__hash__":2203},"content\u002Fhow-to\u002Fhow-to-raise-an-exception-in-python.md","How to Raise an Exception in Python",{"type":7,"value":8,"toc":2153},"minimark",[9,13,22,25,30,112,118,122,141,148,153,156,167,170,179,186,189,211,214,226,229,280,283,297,300,304,307,310,324,326,377,383,387,390,396,401,450,456,463,469,474,541,547,553,558,635,638,644,649,698,701,705,708,711,722,725,781,784,823,830,834,840,843,1086,1089,1098,1101,1112,1119,1123,1133,1136,1197,1200,1224,1227,1240,1244,1247,1250,1253,1330,1333,1342,1349,1356,1359,1371,1373,1508,1510,1528,1530,1543,1550,1554,1559,1569,1574,1619,1622,1625,1671,1675,1678,1690,1692,1751,1755,1758,1779,1782,1803,1809,1812,1815,1837,1840,1887,1898,1901,1913,1918,1922,1925,1939,1990,1993,2004,2008,2017,2025,2033,2041,2045,2048,2050,2062,2065,2086,2093,2096,2103,2108,2112,2149],[10,11,5],"h1",{"id":12},"how-to-raise-an-exception-in-python",[14,15,16,17,21],"p",{},"Use the ",[18,19,20],"code",{},"raise"," statement when you want your program to stop and show a clear error on purpose.",[14,23,24],{},"This is useful when your code detects invalid data, a missing value, or a rule that should not be broken. On this page, you will learn when to raise an exception, how to choose an exception type, and how to write a helpful error message.",[26,27,29],"h2",{"id":28},"quick-answer","Quick answer",[31,32,37],"pre",{"className":33,"code":34,"language":35,"meta":36,"style":36},"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","",[18,38,39,59,66,86],{"__ignoreMap":36},[40,41,44,48,52,55],"span",{"class":42,"line":43},"line",1,[40,45,47],{"class":46},"su5hD","age ",[40,49,51],{"class":50},"smGrS","=",[40,53,54],{"class":50}," -",[40,56,58],{"class":57},"srdBf","1\n",[40,60,62],{"class":42,"line":61},2,[40,63,65],{"emptyLinePlaceholder":64},true,"\n",[40,67,69,73,76,79,82],{"class":42,"line":68},3,[40,70,72],{"class":71},"sVHd0","if",[40,74,75],{"class":46}," age ",[40,77,78],{"class":50},"\u003C",[40,80,81],{"class":57}," 0",[40,83,85],{"class":84},"sP7_E",":\n",[40,87,89,92,96,99,103,107,109],{"class":42,"line":88},4,[40,90,91],{"class":71},"    raise",[40,93,95],{"class":94},"sZMiF"," ValueError",[40,97,98],{"class":84},"(",[40,100,102],{"class":101},"sjJ54","\"",[40,104,106],{"class":105},"s_sjI","age cannot be negative",[40,108,102],{"class":101},[40,110,111],{"class":84},")\n",[14,113,114,115,117],{},"Use ",[18,116,20],{}," when your program detects invalid data or a rule that should not be broken.",[26,119,121],{"id":120},"what-this-page-covers","What this page covers",[123,124,125,132,135,138],"ul",{},[126,127,128,129,131],"li",{},"Shows how to use the ",[18,130,20],{}," statement",[126,133,134],{},"Explains why you would raise an exception yourself",[126,136,137],{},"Focuses on simple built-in exceptions",[126,139,140],{},"Does not repeat full exception handling basics",[26,142,144,145,147],{"id":143},"what-raise-means","What ",[18,146,20],{}," means",[14,149,150,152],{},[18,151,20],{}," stops normal program flow and creates an exception at the exact place where your code finds a problem.",[14,154,155],{},"Use it when:",[123,157,158,161,164],{},[126,159,160],{},"Input is invalid",[126,162,163],{},"A value breaks a rule",[126,165,166],{},"Your program reaches a state that should not happen",[14,168,169],{},"This helps you catch problems earlier and makes errors clearer.",[14,171,172,173,178],{},"For a beginner-friendly definition, see ",[174,175,177],"a",{"href":176},"\u002Fglossary\u002Fwhat-is-an-exception-in-python","what an exception is in Python",".",[26,180,182,183,185],{"id":181},"basic-raise-syntax","Basic ",[18,184,20],{}," syntax",[14,187,188],{},"The basic pattern is:",[31,190,192],{"className":33,"code":191,"language":35,"meta":36,"style":36},"raise ValueError(\"message\")\n",[18,193,194],{"__ignoreMap":36},[40,195,196,198,200,202,204,207,209],{"class":42,"line":43},[40,197,20],{"class":71},[40,199,95],{"class":94},[40,201,98],{"class":84},[40,203,102],{"class":101},[40,205,206],{"class":105},"message",[40,208,102],{"class":101},[40,210,111],{"class":84},[14,212,213],{},"You write:",[123,215,216,220,223],{},[126,217,218],{},[18,219,20],{},[126,221,222],{},"an exception type",[126,224,225],{},"usually a short message string",[14,227,228],{},"Example:",[31,230,232],{"className":33,"code":231,"language":35,"meta":36,"style":36},"name = \"\"\n\nif name == \"\":\n    raise ValueError(\"name cannot be empty\")\n",[18,233,234,244,248,263],{"__ignoreMap":36},[40,235,236,239,241],{"class":42,"line":43},[40,237,238],{"class":46},"name ",[40,240,51],{"class":50},[40,242,243],{"class":101}," \"\"\n",[40,245,246],{"class":42,"line":61},[40,247,65],{"emptyLinePlaceholder":64},[40,249,250,252,255,258,261],{"class":42,"line":68},[40,251,72],{"class":71},[40,253,254],{"class":46}," name ",[40,256,257],{"class":50},"==",[40,259,260],{"class":101}," \"\"",[40,262,85],{"class":84},[40,264,265,267,269,271,273,276,278],{"class":42,"line":88},[40,266,91],{"class":71},[40,268,95],{"class":94},[40,270,98],{"class":84},[40,272,102],{"class":101},[40,274,275],{"class":105},"name cannot be empty",[40,277,102],{"class":101},[40,279,111],{"class":84},[14,281,282],{},"In this example:",[123,284,285,291],{},[126,286,287,290],{},[18,288,289],{},"ValueError"," is the exception type",[126,292,293,296],{},[18,294,295],{},"\"name cannot be empty\""," is the error message",[14,298,299],{},"Choose an exception type that matches the problem.",[26,301,303],{"id":302},"when-to-raise-an-exception","When to raise an exception",[14,305,306],{},"Raise an exception when your code detects a problem that should stop normal execution.",[14,308,309],{},"Common cases:",[123,311,312,315,318,321],{},[126,313,314],{},"An input value is not allowed",[126,316,317],{},"A required argument is missing or empty",[126,319,320],{},"A function gets the wrong type or wrong value",[126,322,323],{},"A business rule is broken, such as a negative price",[14,325,228],{},[31,327,329],{"className":33,"code":328,"language":35,"meta":36,"style":36},"price = -10\n\nif price \u003C 0:\n    raise ValueError(\"price cannot be negative\")\n",[18,330,331,343,347,360],{"__ignoreMap":36},[40,332,333,336,338,340],{"class":42,"line":43},[40,334,335],{"class":46},"price ",[40,337,51],{"class":50},[40,339,54],{"class":50},[40,341,342],{"class":57},"10\n",[40,344,345],{"class":42,"line":61},[40,346,65],{"emptyLinePlaceholder":64},[40,348,349,351,354,356,358],{"class":42,"line":68},[40,350,72],{"class":71},[40,352,353],{"class":46}," price ",[40,355,78],{"class":50},[40,357,81],{"class":57},[40,359,85],{"class":84},[40,361,362,364,366,368,370,373,375],{"class":42,"line":88},[40,363,91],{"class":71},[40,365,95],{"class":94},[40,367,98],{"class":84},[40,369,102],{"class":101},[40,371,372],{"class":105},"price cannot be negative",[40,374,102],{"class":101},[40,376,111],{"class":84},[14,378,379,380,382],{},"This is better than just printing a warning, because ",[18,381,20],{}," stops the program unless the exception is caught.",[26,384,386],{"id":385},"common-built-in-exceptions-to-raise","Common built-in exceptions to raise",[14,388,389],{},"Here are some common exception types beginners use.",[391,392,394],"h3",{"id":393},"valueerror",[18,395,289],{},[14,397,114,398,400],{},[18,399,289],{}," when the type is correct, but the value is not allowed.",[31,402,404],{"className":33,"code":403,"language":35,"meta":36,"style":36},"age = -5\n\nif age \u003C 0:\n    raise ValueError(\"age must be 0 or greater\")\n",[18,405,406,417,421,433],{"__ignoreMap":36},[40,407,408,410,412,414],{"class":42,"line":43},[40,409,47],{"class":46},[40,411,51],{"class":50},[40,413,54],{"class":50},[40,415,416],{"class":57},"5\n",[40,418,419],{"class":42,"line":61},[40,420,65],{"emptyLinePlaceholder":64},[40,422,423,425,427,429,431],{"class":42,"line":68},[40,424,72],{"class":71},[40,426,75],{"class":46},[40,428,78],{"class":50},[40,430,81],{"class":57},[40,432,85],{"class":84},[40,434,435,437,439,441,443,446,448],{"class":42,"line":88},[40,436,91],{"class":71},[40,438,95],{"class":94},[40,440,98],{"class":84},[40,442,102],{"class":101},[40,444,445],{"class":105},"age must be 0 or greater",[40,447,102],{"class":101},[40,449,111],{"class":84},[14,451,452,455],{},[18,453,454],{},"age"," is still an integer, so the type is fine. The problem is the value.",[14,457,458,459,178],{},"If you want more help with this error type, see ",[174,460,462],{"href":461},"\u002Ferrors\u002Fvalueerror-in-python-causes-and-fixes","ValueError in Python: causes and fixes",[391,464,466],{"id":465},"typeerror",[18,467,468],{},"TypeError",[14,470,114,471,473],{},[18,472,468],{}," when the data type is wrong.",[31,475,477],{"className":33,"code":476,"language":35,"meta":36,"style":36},"age = \"twenty\"\n\nif not isinstance(age, int):\n    raise TypeError(\"age must be an integer\")\n",[18,478,479,494,498,523],{"__ignoreMap":36},[40,480,481,483,485,488,491],{"class":42,"line":43},[40,482,47],{"class":46},[40,484,51],{"class":50},[40,486,487],{"class":101}," \"",[40,489,490],{"class":105},"twenty",[40,492,493],{"class":101},"\"\n",[40,495,496],{"class":42,"line":61},[40,497,65],{"emptyLinePlaceholder":64},[40,499,500,502,505,509,511,514,517,520],{"class":42,"line":68},[40,501,72],{"class":71},[40,503,504],{"class":50}," not",[40,506,508],{"class":507},"sptTA"," isinstance",[40,510,98],{"class":84},[40,512,454],{"class":513},"slqww",[40,515,516],{"class":84},",",[40,518,519],{"class":94}," int",[40,521,522],{"class":84},"):\n",[40,524,525,527,530,532,534,537,539],{"class":42,"line":88},[40,526,91],{"class":71},[40,528,529],{"class":94}," TypeError",[40,531,98],{"class":84},[40,533,102],{"class":101},[40,535,536],{"class":105},"age must be an integer",[40,538,102],{"class":101},[40,540,111],{"class":84},[14,542,543,544,546],{},"Here, ",[18,545,454],{}," is a string instead of an integer.",[391,548,550],{"id":549},"keyerror",[18,551,552],{},"KeyError",[14,554,114,555,557],{},[18,556,552],{}," when a required dictionary key is missing.",[31,559,561],{"className":33,"code":560,"language":35,"meta":36,"style":36},"user = {\"name\": \"Ana\"}\n\nif \"email\" not in user:\n    raise KeyError(\"email\")\n",[18,562,563,593,597,618],{"__ignoreMap":36},[40,564,565,568,570,573,575,578,580,583,585,588,590],{"class":42,"line":43},[40,566,567],{"class":46},"user ",[40,569,51],{"class":50},[40,571,572],{"class":84}," {",[40,574,102],{"class":101},[40,576,577],{"class":105},"name",[40,579,102],{"class":101},[40,581,582],{"class":84},":",[40,584,487],{"class":101},[40,586,587],{"class":105},"Ana",[40,589,102],{"class":101},[40,591,592],{"class":84},"}\n",[40,594,595],{"class":42,"line":61},[40,596,65],{"emptyLinePlaceholder":64},[40,598,599,601,603,606,608,610,613,616],{"class":42,"line":68},[40,600,72],{"class":71},[40,602,487],{"class":101},[40,604,605],{"class":105},"email",[40,607,102],{"class":101},[40,609,504],{"class":50},[40,611,612],{"class":50}," in",[40,614,615],{"class":46}," user",[40,617,85],{"class":84},[40,619,620,622,625,627,629,631,633],{"class":42,"line":88},[40,621,91],{"class":71},[40,623,624],{"class":94}," KeyError",[40,626,98],{"class":84},[40,628,102],{"class":101},[40,630,605],{"class":105},[40,632,102],{"class":101},[40,634,111],{"class":84},[14,636,637],{},"This is useful in custom checks when your code requires a key to exist.",[391,639,641],{"id":640},"runtimeerror",[18,642,643],{},"RuntimeError",[14,645,114,646,648],{},[18,647,643],{}," for a general program state problem when no better built-in exception fits.",[31,650,652],{"className":33,"code":651,"language":35,"meta":36,"style":36},"is_connected = False\n\nif not is_connected:\n    raise RuntimeError(\"database connection is not available\")\n",[18,653,654,665,669,680],{"__ignoreMap":36},[40,655,656,659,661],{"class":42,"line":43},[40,657,658],{"class":46},"is_connected ",[40,660,51],{"class":50},[40,662,664],{"class":663},"s39Yj"," False\n",[40,666,667],{"class":42,"line":61},[40,668,65],{"emptyLinePlaceholder":64},[40,670,671,673,675,678],{"class":42,"line":68},[40,672,72],{"class":71},[40,674,504],{"class":50},[40,676,677],{"class":46}," is_connected",[40,679,85],{"class":84},[40,681,682,684,687,689,691,694,696],{"class":42,"line":88},[40,683,91],{"class":71},[40,685,686],{"class":94}," RuntimeError",[40,688,98],{"class":84},[40,690,102],{"class":101},[40,692,693],{"class":105},"database connection is not available",[40,695,102],{"class":101},[40,697,111],{"class":84},[14,699,700],{},"Try to use a more specific exception type if possible.",[26,702,704],{"id":703},"how-to-write-a-helpful-error-message","How to write a helpful error message",[14,706,707],{},"A good error message should be short and specific.",[14,709,710],{},"Try to include:",[123,712,713,716,719],{},[126,714,715],{},"What was wrong",[126,717,718],{},"What was expected",[126,720,721],{},"The bad value only if it helps debugging",[14,723,724],{},"Good examples:",[31,726,728],{"className":33,"code":727,"language":35,"meta":36,"style":36},"raise ValueError(\"score must be between 0 and 100\")\nraise TypeError(\"name must be a string\")\nraise ValueError(\"quantity cannot be negative\")\n",[18,729,730,747,764],{"__ignoreMap":36},[40,731,732,734,736,738,740,743,745],{"class":42,"line":43},[40,733,20],{"class":71},[40,735,95],{"class":94},[40,737,98],{"class":84},[40,739,102],{"class":101},[40,741,742],{"class":105},"score must be between 0 and 100",[40,744,102],{"class":101},[40,746,111],{"class":84},[40,748,749,751,753,755,757,760,762],{"class":42,"line":61},[40,750,20],{"class":71},[40,752,529],{"class":94},[40,754,98],{"class":84},[40,756,102],{"class":101},[40,758,759],{"class":105},"name must be a string",[40,761,102],{"class":101},[40,763,111],{"class":84},[40,765,766,768,770,772,774,777,779],{"class":42,"line":68},[40,767,20],{"class":71},[40,769,95],{"class":94},[40,771,98],{"class":84},[40,773,102],{"class":101},[40,775,776],{"class":105},"quantity cannot be negative",[40,778,102],{"class":101},[40,780,111],{"class":84},[14,782,783],{},"Less helpful examples:",[31,785,787],{"className":33,"code":786,"language":35,"meta":36,"style":36},"raise ValueError(\"error\")\nraise ValueError(\"invalid\")\n",[18,788,789,806],{"__ignoreMap":36},[40,790,791,793,795,797,799,802,804],{"class":42,"line":43},[40,792,20],{"class":71},[40,794,95],{"class":94},[40,796,98],{"class":84},[40,798,102],{"class":101},[40,800,801],{"class":105},"error",[40,803,102],{"class":101},[40,805,111],{"class":84},[40,807,808,810,812,814,816,819,821],{"class":42,"line":61},[40,809,20],{"class":71},[40,811,95],{"class":94},[40,813,98],{"class":84},[40,815,102],{"class":101},[40,817,818],{"class":105},"invalid",[40,820,102],{"class":101},[40,822,111],{"class":84},[14,824,825,826,829],{},"Messages like ",[18,827,828],{},"\"error\""," do not explain the real problem.",[26,831,833],{"id":832},"raising-exceptions-inside-functions","Raising exceptions inside functions",[14,835,836,837,839],{},"A very common place to use ",[18,838,20],{}," is at the start of a function.",[14,841,842],{},"Validate inputs early before the function does more work.",[31,844,846],{"className":33,"code":845,"language":35,"meta":36,"style":36},"def calculate_total(price, quantity):\n    if not isinstance(price, (int, float)):\n        raise TypeError(\"price must be a number\")\n\n    if not isinstance(quantity, int):\n        raise TypeError(\"quantity must be an integer\")\n\n    if price \u003C 0:\n        raise ValueError(\"price cannot be negative\")\n\n    if quantity \u003C 1:\n        raise ValueError(\"quantity must be at least 1\")\n\n    return price * quantity\n\n\nprint(calculate_total(9.99, 3))\n",[18,847,848,871,900,918,922,942,960,965,978,995,1000,1015,1033,1038,1052,1057,1062],{"__ignoreMap":36},[40,849,850,854,858,860,864,866,869],{"class":42,"line":43},[40,851,853],{"class":852},"sbsja","def",[40,855,857],{"class":856},"sGLFI"," calculate_total",[40,859,98],{"class":84},[40,861,863],{"class":862},"sFwrP","price",[40,865,516],{"class":84},[40,867,868],{"class":862}," quantity",[40,870,522],{"class":84},[40,872,873,876,878,880,882,884,886,889,892,894,897],{"class":42,"line":61},[40,874,875],{"class":71},"    if",[40,877,504],{"class":50},[40,879,508],{"class":507},[40,881,98],{"class":84},[40,883,863],{"class":513},[40,885,516],{"class":84},[40,887,888],{"class":84}," (",[40,890,891],{"class":94},"int",[40,893,516],{"class":84},[40,895,896],{"class":94}," float",[40,898,899],{"class":84},")):\n",[40,901,902,905,907,909,911,914,916],{"class":42,"line":68},[40,903,904],{"class":71},"        raise",[40,906,529],{"class":94},[40,908,98],{"class":84},[40,910,102],{"class":101},[40,912,913],{"class":105},"price must be a number",[40,915,102],{"class":101},[40,917,111],{"class":84},[40,919,920],{"class":42,"line":88},[40,921,65],{"emptyLinePlaceholder":64},[40,923,925,927,929,931,933,936,938,940],{"class":42,"line":924},5,[40,926,875],{"class":71},[40,928,504],{"class":50},[40,930,508],{"class":507},[40,932,98],{"class":84},[40,934,935],{"class":513},"quantity",[40,937,516],{"class":84},[40,939,519],{"class":94},[40,941,522],{"class":84},[40,943,945,947,949,951,953,956,958],{"class":42,"line":944},6,[40,946,904],{"class":71},[40,948,529],{"class":94},[40,950,98],{"class":84},[40,952,102],{"class":101},[40,954,955],{"class":105},"quantity must be an integer",[40,957,102],{"class":101},[40,959,111],{"class":84},[40,961,963],{"class":42,"line":962},7,[40,964,65],{"emptyLinePlaceholder":64},[40,966,968,970,972,974,976],{"class":42,"line":967},8,[40,969,875],{"class":71},[40,971,353],{"class":46},[40,973,78],{"class":50},[40,975,81],{"class":57},[40,977,85],{"class":84},[40,979,981,983,985,987,989,991,993],{"class":42,"line":980},9,[40,982,904],{"class":71},[40,984,95],{"class":94},[40,986,98],{"class":84},[40,988,102],{"class":101},[40,990,372],{"class":105},[40,992,102],{"class":101},[40,994,111],{"class":84},[40,996,998],{"class":42,"line":997},10,[40,999,65],{"emptyLinePlaceholder":64},[40,1001,1003,1005,1008,1010,1013],{"class":42,"line":1002},11,[40,1004,875],{"class":71},[40,1006,1007],{"class":46}," quantity ",[40,1009,78],{"class":50},[40,1011,1012],{"class":57}," 1",[40,1014,85],{"class":84},[40,1016,1018,1020,1022,1024,1026,1029,1031],{"class":42,"line":1017},12,[40,1019,904],{"class":71},[40,1021,95],{"class":94},[40,1023,98],{"class":84},[40,1025,102],{"class":101},[40,1027,1028],{"class":105},"quantity must be at least 1",[40,1030,102],{"class":101},[40,1032,111],{"class":84},[40,1034,1036],{"class":42,"line":1035},13,[40,1037,65],{"emptyLinePlaceholder":64},[40,1039,1041,1044,1046,1049],{"class":42,"line":1040},14,[40,1042,1043],{"class":71},"    return",[40,1045,353],{"class":46},[40,1047,1048],{"class":50},"*",[40,1050,1051],{"class":46}," quantity\n",[40,1053,1055],{"class":42,"line":1054},15,[40,1056,65],{"emptyLinePlaceholder":64},[40,1058,1060],{"class":42,"line":1059},16,[40,1061,65],{"emptyLinePlaceholder":64},[40,1063,1065,1068,1070,1073,1075,1078,1080,1083],{"class":42,"line":1064},17,[40,1066,1067],{"class":507},"print",[40,1069,98],{"class":84},[40,1071,1072],{"class":513},"calculate_total",[40,1074,98],{"class":84},[40,1076,1077],{"class":57},"9.99",[40,1079,516],{"class":84},[40,1081,1082],{"class":57}," 3",[40,1084,1085],{"class":84},"))\n",[14,1087,1088],{},"Expected output:",[31,1090,1092],{"className":33,"code":1091,"language":35,"meta":36,"style":36},"29.97\n",[18,1093,1094],{"__ignoreMap":36},[40,1095,1096],{"class":42,"line":43},[40,1097,1091],{"class":57},[14,1099,1100],{},"Why this helps:",[123,1102,1103,1106,1109],{},[126,1104,1105],{},"Bad input is caught immediately",[126,1107,1108],{},"The function is easier to debug",[126,1110,1111],{},"The caller gets a clear error message",[14,1113,1114,1115,178],{},"If you are also learning how to catch these errors, read ",[174,1116,1118],{"href":1117},"\u002Fhow-to\u002Fhow-to-handle-exceptions-in-python\u002F","how to handle exceptions in Python",[26,1120,1122],{"id":1121},"re-raising-an-exception","Re-raising an exception",[14,1124,1125,1126,1129,1130,1132],{},"Inside an ",[18,1127,1128],{},"except"," block, you can use ",[18,1131,20],{}," by itself to raise the same exception again.",[14,1134,1135],{},"This is called re-raising.",[31,1137,1139],{"className":33,"code":1138,"language":35,"meta":36,"style":36},"try:\n    number = int(\"hello\")\nexcept ValueError:\n    print(\"Could not convert the text to an integer\")\n    raise\n",[18,1140,1141,1148,1168,1176,1192],{"__ignoreMap":36},[40,1142,1143,1146],{"class":42,"line":43},[40,1144,1145],{"class":71},"try",[40,1147,85],{"class":84},[40,1149,1150,1153,1155,1157,1159,1161,1164,1166],{"class":42,"line":61},[40,1151,1152],{"class":46},"    number ",[40,1154,51],{"class":50},[40,1156,519],{"class":94},[40,1158,98],{"class":84},[40,1160,102],{"class":101},[40,1162,1163],{"class":105},"hello",[40,1165,102],{"class":101},[40,1167,111],{"class":84},[40,1169,1170,1172,1174],{"class":42,"line":68},[40,1171,1128],{"class":71},[40,1173,95],{"class":94},[40,1175,85],{"class":84},[40,1177,1178,1181,1183,1185,1188,1190],{"class":42,"line":88},[40,1179,1180],{"class":507},"    print",[40,1182,98],{"class":84},[40,1184,102],{"class":101},[40,1186,1187],{"class":105},"Could not convert the text to an integer",[40,1189,102],{"class":101},[40,1191,111],{"class":84},[40,1193,1194],{"class":42,"line":924},[40,1195,1196],{"class":71},"    raise\n",[14,1198,1199],{},"What happens here:",[123,1201,1202,1210,1216,1219],{},[126,1203,1204,1207,1208],{},[18,1205,1206],{},"int(\"hello\")"," causes a ",[18,1209,289],{},[126,1211,1212,1213,1215],{},"The ",[18,1214,1128],{}," block runs",[126,1217,1218],{},"The message is printed",[126,1220,1221,1223],{},[18,1222,20],{}," sends the same exception upward again",[14,1225,1226],{},"Use this when you want to inspect or log the error first, but still keep the original problem visible.",[14,1228,1229,1230,1232,1233,1235,1236,178],{},"To learn more about ",[18,1231,1145],{}," and ",[18,1234,1128],{},", see ",[174,1237,1239],{"href":1238},"\u002Fhow-to\u002Fhow-to-use-try-except-blocks-in-python\u002F","how to use try-except blocks in Python",[26,1241,1243],{"id":1242},"custom-exceptions","Custom exceptions",[14,1245,1246],{},"You can create your own exception class for program-specific rules.",[14,1248,1249],{},"For beginners, built-in exceptions are usually enough. Start there first.",[14,1251,1252],{},"Simple example:",[31,1254,1256],{"className":33,"code":1255,"language":35,"meta":36,"style":36},"class InvalidAgeError(Exception):\n    pass\n\n\nage = -2\n\nif age \u003C 0:\n    raise InvalidAgeError(\"age cannot be negative\")\n",[18,1257,1258,1274,1279,1283,1287,1298,1302,1314],{"__ignoreMap":36},[40,1259,1260,1263,1267,1269,1272],{"class":42,"line":43},[40,1261,1262],{"class":852},"class",[40,1264,1266],{"class":1265},"sbgvK"," InvalidAgeError",[40,1268,98],{"class":84},[40,1270,1271],{"class":94},"Exception",[40,1273,522],{"class":84},[40,1275,1276],{"class":42,"line":61},[40,1277,1278],{"class":71},"    pass\n",[40,1280,1281],{"class":42,"line":68},[40,1282,65],{"emptyLinePlaceholder":64},[40,1284,1285],{"class":42,"line":88},[40,1286,65],{"emptyLinePlaceholder":64},[40,1288,1289,1291,1293,1295],{"class":42,"line":924},[40,1290,47],{"class":46},[40,1292,51],{"class":50},[40,1294,54],{"class":50},[40,1296,1297],{"class":57},"2\n",[40,1299,1300],{"class":42,"line":944},[40,1301,65],{"emptyLinePlaceholder":64},[40,1303,1304,1306,1308,1310,1312],{"class":42,"line":962},[40,1305,72],{"class":71},[40,1307,75],{"class":46},[40,1309,78],{"class":50},[40,1311,81],{"class":57},[40,1313,85],{"class":84},[40,1315,1316,1318,1320,1322,1324,1326,1328],{"class":42,"line":967},[40,1317,91],{"class":71},[40,1319,1266],{"class":513},[40,1321,98],{"class":84},[40,1323,102],{"class":101},[40,1325,106],{"class":105},[40,1327,102],{"class":101},[40,1329,111],{"class":84},[14,1331,1332],{},"Custom exceptions are helpful when a built-in name is too vague.",[14,1334,1335,1336,1338,1339,1341],{},"But in many beginner programs, ",[18,1337,289],{}," or ",[18,1340,468],{}," is clearer and simpler.",[14,1343,1344,1345,178],{},"For a broader explanation, see ",[174,1346,1348],{"href":1347},"\u002Flearn\u002Fraising-exceptions-in-python","raising exceptions in Python",[26,1350,1352,1353],{"id":1351},"how-this-differs-from-try-except","How this differs from ",[18,1354,1355],{},"try-except",[14,1357,1358],{},"These two ideas are related, but they do different jobs:",[123,1360,1361,1366],{},[126,1362,1363,1365],{},[18,1364,20],{}," creates an exception",[126,1367,1368,1370],{},[18,1369,1355],{}," catches and handles an exception",[14,1372,228],{},[31,1374,1376],{"className":33,"code":1375,"language":35,"meta":36,"style":36},"def set_age(age):\n    if age \u003C 0:\n        raise ValueError(\"age cannot be negative\")\n    return age\n\n\ntry:\n    result = set_age(-1)\n    print(result)\nexcept ValueError as error:\n    print(f\"Invalid input: {error}\")\n",[18,1377,1378,1391,1403,1419,1426,1430,1434,1440,1459,1470,1484],{"__ignoreMap":36},[40,1379,1380,1382,1385,1387,1389],{"class":42,"line":43},[40,1381,853],{"class":852},[40,1383,1384],{"class":856}," set_age",[40,1386,98],{"class":84},[40,1388,454],{"class":862},[40,1390,522],{"class":84},[40,1392,1393,1395,1397,1399,1401],{"class":42,"line":61},[40,1394,875],{"class":71},[40,1396,75],{"class":46},[40,1398,78],{"class":50},[40,1400,81],{"class":57},[40,1402,85],{"class":84},[40,1404,1405,1407,1409,1411,1413,1415,1417],{"class":42,"line":68},[40,1406,904],{"class":71},[40,1408,95],{"class":94},[40,1410,98],{"class":84},[40,1412,102],{"class":101},[40,1414,106],{"class":105},[40,1416,102],{"class":101},[40,1418,111],{"class":84},[40,1420,1421,1423],{"class":42,"line":88},[40,1422,1043],{"class":71},[40,1424,1425],{"class":46}," age\n",[40,1427,1428],{"class":42,"line":924},[40,1429,65],{"emptyLinePlaceholder":64},[40,1431,1432],{"class":42,"line":944},[40,1433,65],{"emptyLinePlaceholder":64},[40,1435,1436,1438],{"class":42,"line":962},[40,1437,1145],{"class":71},[40,1439,85],{"class":84},[40,1441,1442,1445,1447,1449,1451,1454,1457],{"class":42,"line":967},[40,1443,1444],{"class":46},"    result ",[40,1446,51],{"class":50},[40,1448,1384],{"class":513},[40,1450,98],{"class":84},[40,1452,1453],{"class":50},"-",[40,1455,1456],{"class":57},"1",[40,1458,111],{"class":84},[40,1460,1461,1463,1465,1468],{"class":42,"line":980},[40,1462,1180],{"class":507},[40,1464,98],{"class":84},[40,1466,1467],{"class":513},"result",[40,1469,111],{"class":84},[40,1471,1472,1474,1476,1479,1482],{"class":42,"line":997},[40,1473,1128],{"class":71},[40,1475,95],{"class":94},[40,1477,1478],{"class":71}," as",[40,1480,1481],{"class":46}," error",[40,1483,85],{"class":84},[40,1485,1486,1488,1490,1493,1496,1499,1501,1504,1506],{"class":42,"line":1002},[40,1487,1180],{"class":507},[40,1489,98],{"class":84},[40,1491,1492],{"class":852},"f",[40,1494,1495],{"class":105},"\"Invalid input: ",[40,1497,1498],{"class":57},"{",[40,1500,801],{"class":513},[40,1502,1503],{"class":57},"}",[40,1505,102],{"class":105},[40,1507,111],{"class":84},[14,1509,1088],{},[31,1511,1513],{"className":33,"code":1512,"language":35,"meta":36,"style":36},"Invalid input: age cannot be negative\n",[18,1514,1515],{"__ignoreMap":36},[40,1516,1517,1520,1523,1525],{"class":42,"line":43},[40,1518,1519],{"class":46},"Invalid ",[40,1521,1522],{"class":507},"input",[40,1524,582],{"class":84},[40,1526,1527],{"class":46}," age cannot be negative\n",[14,1529,282],{},[123,1531,1532,1538],{},[126,1533,1534,1537],{},[18,1535,1536],{},"set_age()"," raises the exception",[126,1539,1540,1542],{},[18,1541,1355],{}," catches it and prints a message",[14,1544,1545,1546,178],{},"If you want to learn the full pattern, read ",[174,1547,1549],{"href":1548},"\u002Flearn\u002Fusing-try-except-else-and-finally-in-python","using try-except-else-and-finally in Python",[26,1551,1553],{"id":1552},"common-mistakes","Common mistakes",[14,1555,1556,1557,178],{},"These are common problems beginners run into when using ",[18,1558,20],{},[391,1560,1562,1563,1566,1567],{"id":1561},"using-print-instead-of-raise","Using ",[18,1564,1565],{},"print()"," instead of ",[18,1568,20],{},[14,1570,1571,1573],{},[18,1572,1565],{}," only shows text. It does not stop the program.",[31,1575,1577],{"className":33,"code":1576,"language":35,"meta":36,"style":36},"age = -1\n\nif age \u003C 0:\n    print(\"age cannot be negative\")\n",[18,1578,1579,1589,1593,1605],{"__ignoreMap":36},[40,1580,1581,1583,1585,1587],{"class":42,"line":43},[40,1582,47],{"class":46},[40,1584,51],{"class":50},[40,1586,54],{"class":50},[40,1588,58],{"class":57},[40,1590,1591],{"class":42,"line":61},[40,1592,65],{"emptyLinePlaceholder":64},[40,1594,1595,1597,1599,1601,1603],{"class":42,"line":68},[40,1596,72],{"class":71},[40,1598,75],{"class":46},[40,1600,78],{"class":50},[40,1602,81],{"class":57},[40,1604,85],{"class":84},[40,1606,1607,1609,1611,1613,1615,1617],{"class":42,"line":88},[40,1608,1180],{"class":507},[40,1610,98],{"class":84},[40,1612,102],{"class":101},[40,1614,106],{"class":105},[40,1616,102],{"class":101},[40,1618,111],{"class":84},[14,1620,1621],{},"This prints a message, but the program keeps going.",[14,1623,1624],{},"If the problem should stop execution, use:",[31,1626,1627],{"className":33,"code":34,"language":35,"meta":36,"style":36},[18,1628,1629,1639,1643,1655],{"__ignoreMap":36},[40,1630,1631,1633,1635,1637],{"class":42,"line":43},[40,1632,47],{"class":46},[40,1634,51],{"class":50},[40,1636,54],{"class":50},[40,1638,58],{"class":57},[40,1640,1641],{"class":42,"line":61},[40,1642,65],{"emptyLinePlaceholder":64},[40,1644,1645,1647,1649,1651,1653],{"class":42,"line":68},[40,1646,72],{"class":71},[40,1648,75],{"class":46},[40,1650,78],{"class":50},[40,1652,81],{"class":57},[40,1654,85],{"class":84},[40,1656,1657,1659,1661,1663,1665,1667,1669],{"class":42,"line":88},[40,1658,91],{"class":71},[40,1660,95],{"class":94},[40,1662,98],{"class":84},[40,1664,102],{"class":101},[40,1666,106],{"class":105},[40,1668,102],{"class":101},[40,1670,111],{"class":84},[391,1672,1674],{"id":1673},"raising-the-wrong-exception-type","Raising the wrong exception type",[14,1676,1677],{},"Pick an exception type that matches the real problem.",[123,1679,1680,1685],{},[126,1681,1682,1683],{},"Wrong type → ",[18,1684,468],{},[126,1686,1687,1688],{},"Wrong value → ",[18,1689,289],{},[14,1691,228],{},[31,1693,1695],{"className":33,"code":1694,"language":35,"meta":36,"style":36},"value = \"100\"\n\nif not isinstance(value, int):\n    raise TypeError(\"value must be an integer\")\n",[18,1696,1697,1711,1715,1734],{"__ignoreMap":36},[40,1698,1699,1702,1704,1706,1709],{"class":42,"line":43},[40,1700,1701],{"class":46},"value ",[40,1703,51],{"class":50},[40,1705,487],{"class":101},[40,1707,1708],{"class":105},"100",[40,1710,493],{"class":101},[40,1712,1713],{"class":42,"line":61},[40,1714,65],{"emptyLinePlaceholder":64},[40,1716,1717,1719,1721,1723,1725,1728,1730,1732],{"class":42,"line":68},[40,1718,72],{"class":71},[40,1720,504],{"class":50},[40,1722,508],{"class":507},[40,1724,98],{"class":84},[40,1726,1727],{"class":513},"value",[40,1729,516],{"class":84},[40,1731,519],{"class":94},[40,1733,522],{"class":84},[40,1735,1736,1738,1740,1742,1744,1747,1749],{"class":42,"line":88},[40,1737,91],{"class":71},[40,1739,529],{"class":94},[40,1741,98],{"class":84},[40,1743,102],{"class":101},[40,1745,1746],{"class":105},"value must be an integer",[40,1748,102],{"class":101},[40,1750,111],{"class":84},[391,1752,1754],{"id":1753},"writing-vague-messages","Writing vague messages",[14,1756,1757],{},"Avoid messages like:",[31,1759,1761],{"className":33,"code":1760,"language":35,"meta":36,"style":36},"raise ValueError(\"invalid\")\n",[18,1762,1763],{"__ignoreMap":36},[40,1764,1765,1767,1769,1771,1773,1775,1777],{"class":42,"line":43},[40,1766,20],{"class":71},[40,1768,95],{"class":94},[40,1770,98],{"class":84},[40,1772,102],{"class":101},[40,1774,818],{"class":105},[40,1776,102],{"class":101},[40,1778,111],{"class":84},[14,1780,1781],{},"Better:",[31,1783,1785],{"className":33,"code":1784,"language":35,"meta":36,"style":36},"raise ValueError(\"quantity must be at least 1\")\n",[18,1786,1787],{"__ignoreMap":36},[40,1788,1789,1791,1793,1795,1797,1799,1801],{"class":42,"line":43},[40,1790,20],{"class":71},[40,1792,95],{"class":94},[40,1794,98],{"class":84},[40,1796,102],{"class":101},[40,1798,1028],{"class":105},[40,1800,102],{"class":101},[40,1802,111],{"class":84},[391,1804,1562,1806,1808],{"id":1805},"using-raise-without-a-real-validation-check",[18,1807,20],{}," without a real validation check",[14,1810,1811],{},"Only raise an exception when your code has actually found a problem.",[14,1813,1814],{},"Bad example:",[31,1816,1818],{"className":33,"code":1817,"language":35,"meta":36,"style":36},"raise ValueError(\"something went wrong\")\n",[18,1819,1820],{"__ignoreMap":36},[40,1821,1822,1824,1826,1828,1830,1833,1835],{"class":42,"line":43},[40,1823,20],{"class":71},[40,1825,95],{"class":94},[40,1827,98],{"class":84},[40,1829,102],{"class":101},[40,1831,1832],{"class":105},"something went wrong",[40,1834,102],{"class":101},[40,1836,111],{"class":84},[14,1838,1839],{},"Better example:",[31,1841,1843],{"className":33,"code":1842,"language":35,"meta":36,"style":36},"quantity = 0\n\nif quantity \u003C 1:\n    raise ValueError(\"quantity must be at least 1\")\n",[18,1844,1845,1855,1859,1871],{"__ignoreMap":36},[40,1846,1847,1850,1852],{"class":42,"line":43},[40,1848,1849],{"class":46},"quantity ",[40,1851,51],{"class":50},[40,1853,1854],{"class":57}," 0\n",[40,1856,1857],{"class":42,"line":61},[40,1858,65],{"emptyLinePlaceholder":64},[40,1860,1861,1863,1865,1867,1869],{"class":42,"line":68},[40,1862,72],{"class":71},[40,1864,1007],{"class":46},[40,1866,78],{"class":50},[40,1868,1012],{"class":57},[40,1870,85],{"class":84},[40,1872,1873,1875,1877,1879,1881,1883,1885],{"class":42,"line":88},[40,1874,91],{"class":71},[40,1876,95],{"class":94},[40,1878,98],{"class":84},[40,1880,102],{"class":101},[40,1882,1028],{"class":105},[40,1884,102],{"class":101},[40,1886,111],{"class":84},[391,1888,1890,1891,1894,1895],{"id":1889},"confusing-raise-valueerror-with-return-false","Confusing ",[18,1892,1893],{},"raise ValueError(...)"," with ",[18,1896,1897],{},"return False",[14,1899,1900],{},"These do different things.",[123,1902,1903,1908],{},[126,1904,1905,1907],{},[18,1906,1897],{}," sends back a normal value",[126,1909,1910,1912],{},[18,1911,1893],{}," creates an error and stops normal flow",[14,1914,114,1915,1917],{},[18,1916,20],{}," when the input or state is invalid and the caller should know about the error.",[26,1919,1921],{"id":1920},"useful-commands-for-debugging","Useful commands for debugging",[14,1923,1924],{},"When you are testing exception code, these commands can help:",[31,1926,1930],{"className":1927,"code":1928,"language":1929,"meta":36,"style":36},"language-bash shiki shiki-themes material-theme-lighter github-light github-dark","python your_script.py\n","bash",[18,1931,1932],{"__ignoreMap":36},[40,1933,1934,1936],{"class":42,"line":43},[40,1935,35],{"class":1265},[40,1937,1938],{"class":105}," your_script.py\n",[31,1940,1942],{"className":33,"code":1941,"language":35,"meta":36,"style":36},"print(type(value))\nprint(value)\nhelp(ValueError)\nhelp(TypeError)\n",[18,1943,1944,1959,1969,1980],{"__ignoreMap":36},[40,1945,1946,1948,1950,1953,1955,1957],{"class":42,"line":43},[40,1947,1067],{"class":507},[40,1949,98],{"class":84},[40,1951,1952],{"class":94},"type",[40,1954,98],{"class":84},[40,1956,1727],{"class":513},[40,1958,1085],{"class":84},[40,1960,1961,1963,1965,1967],{"class":42,"line":61},[40,1962,1067],{"class":507},[40,1964,98],{"class":84},[40,1966,1727],{"class":513},[40,1968,111],{"class":84},[40,1970,1971,1974,1976,1978],{"class":42,"line":68},[40,1972,1973],{"class":507},"help",[40,1975,98],{"class":84},[40,1977,289],{"class":94},[40,1979,111],{"class":84},[40,1981,1982,1984,1986,1988],{"class":42,"line":88},[40,1983,1973],{"class":507},[40,1985,98],{"class":84},[40,1987,468],{"class":94},[40,1989,111],{"class":84},[14,1991,1992],{},"These can help you check:",[123,1994,1995,1998,2001],{},[126,1996,1997],{},"what type a value really has",[126,1999,2000],{},"what the bad value is",[126,2002,2003],{},"what a built-in exception means",[26,2005,2007],{"id":2006},"faq","FAQ",[391,2009,2011,2012,1232,2014,2016],{"id":2010},"what-is-the-difference-between-raise-and-print","What is the difference between ",[18,2013,20],{},[18,2015,1067],{},"?",[14,2018,2019,2021,2022,2024],{},[18,2020,1067],{}," only shows text. ",[18,2023,20],{}," creates an actual exception and stops the normal flow unless it is caught.",[391,2026,2028,2029,1338,2031,2016],{"id":2027},"should-i-use-valueerror-or-typeerror","Should I use ",[18,2030,289],{},[18,2032,468],{},[14,2034,114,2035,2037,2038,2040],{},[18,2036,468],{}," when the data type is wrong. Use ",[18,2039,289],{}," when the type is correct but the value is not allowed.",[391,2042,2044],{"id":2043},"can-i-raise-an-exception-without-a-message","Can I raise an exception without a message?",[14,2046,2047],{},"Yes, but a clear message is usually better for debugging.",[14,2049,228],{},[31,2051,2053],{"className":33,"code":2052,"language":35,"meta":36,"style":36},"raise ValueError\n",[18,2054,2055],{"__ignoreMap":36},[40,2056,2057,2059],{"class":42,"line":43},[40,2058,20],{"class":71},[40,2060,2061],{"class":94}," ValueError\n",[14,2063,2064],{},"Usually this is more helpful:",[31,2066,2068],{"className":33,"code":2067,"language":35,"meta":36,"style":36},"raise ValueError(\"age cannot be negative\")\n",[18,2069,2070],{"__ignoreMap":36},[40,2071,2072,2074,2076,2078,2080,2082,2084],{"class":42,"line":43},[40,2073,20],{"class":71},[40,2075,95],{"class":94},[40,2077,98],{"class":84},[40,2079,102],{"class":101},[40,2081,106],{"class":105},[40,2083,102],{"class":101},[40,2085,111],{"class":84},[391,2087,2089,2090,2092],{"id":2088},"can-i-use-raise-inside-a-function","Can I use ",[18,2091,20],{}," inside a function?",[14,2094,2095],{},"Yes. This is a common place to validate arguments and stop bad input early.",[391,2097,2099,2100,2102],{"id":2098},"what-does-raise-by-itself-do","What does ",[18,2101,20],{}," by itself do?",[14,2104,1125,2105,2107],{},[18,2106,1128],{}," block, it raises the current exception again.",[26,2109,2111],{"id":2110},"see-also","See also",[123,2113,2114,2119,2124,2129,2134,2138,2144],{},[126,2115,2116],{},[174,2117,2118],{"href":1347},"Raising exceptions in Python",[126,2120,2121],{},[174,2122,2123],{"href":1548},"Using try-except-else-and-finally in Python",[126,2125,2126],{},[174,2127,2128],{"href":1117},"How to handle exceptions in Python",[126,2130,2131],{},[174,2132,2133],{"href":1238},"How to use try-except blocks in Python",[126,2135,2136],{},[174,2137,462],{"href":461},[126,2139,2140],{},[174,2141,2143],{"href":2142},"\u002Ferrors\u002Ftypeerror-missing-required-positional-argument-fix","TypeError: missing required positional argument fix",[126,2145,2146],{},[174,2147,2148],{"href":176},"What is an exception in Python?",[2150,2151,2152],"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 .sptTA, html code.shiki .sptTA{--shiki-light:#6182B8;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .slqww, html code.shiki .slqww{--shiki-light:#6182B8;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .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":36,"searchDepth":61,"depth":61,"links":2154},[2155,2156,2157,2159,2161,2162,2168,2169,2170,2171,2172,2174,2184,2185,2196],{"id":28,"depth":61,"text":29},{"id":120,"depth":61,"text":121},{"id":143,"depth":61,"text":2158},"What raise means",{"id":181,"depth":61,"text":2160},"Basic raise syntax",{"id":302,"depth":61,"text":303},{"id":385,"depth":61,"text":386,"children":2163},[2164,2165,2166,2167],{"id":393,"depth":68,"text":289},{"id":465,"depth":68,"text":468},{"id":549,"depth":68,"text":552},{"id":640,"depth":68,"text":643},{"id":703,"depth":61,"text":704},{"id":832,"depth":61,"text":833},{"id":1121,"depth":61,"text":1122},{"id":1242,"depth":61,"text":1243},{"id":1351,"depth":61,"text":2173},"How this differs from try-except",{"id":1552,"depth":61,"text":1553,"children":2175},[2176,2178,2179,2180,2182],{"id":1561,"depth":68,"text":2177},"Using print() instead of raise",{"id":1673,"depth":68,"text":1674},{"id":1753,"depth":68,"text":1754},{"id":1805,"depth":68,"text":2181},"Using raise without a real validation check",{"id":1889,"depth":68,"text":2183},"Confusing raise ValueError(...) with return False",{"id":1920,"depth":61,"text":1921},{"id":2006,"depth":61,"text":2007,"children":2186},[2187,2189,2191,2192,2194],{"id":2010,"depth":68,"text":2188},"What is the difference between raise and print?",{"id":2027,"depth":68,"text":2190},"Should I use ValueError or TypeError?",{"id":2043,"depth":68,"text":2044},{"id":2088,"depth":68,"text":2193},"Can I use raise inside a function?",{"id":2098,"depth":68,"text":2195},"What does raise by itself do?",{"id":2110,"depth":61,"text":2111},"Master how to raise an exception in python in our comprehensive Python beginner guide.","md",{},"\u002Fhow-to\u002Fhow-to-raise-an-exception-in-python",{"title":5,"description":2197},"how-to\u002Fhow-to-raise-an-exception-in-python","o2jodQXuZ1vHG45i3LpBDOSP9f34zp81HT5bXlcuAMY",1777585510272]