[{"data":1,"prerenderedAt":1330},["ShallowReactive",2],{"doc-\u002Ferrors\u002Fkeyboardinterrupt-exception-in-python-explained":3},{"id":4,"title":5,"body":6,"description":1323,"extension":1324,"meta":1325,"navigation":296,"path":1326,"seo":1327,"stem":1328,"__hash__":1329},"content\u002Ferrors\u002Fkeyboardinterrupt-exception-in-python-explained.md","KeyboardInterrupt Exception in Python Explained",{"type":7,"value":8,"toc":1299},"minimark",[9,13,30,40,45,52,142,145,149,154,157,178,187,191,197,222,229,233,236,264,267,275,278,340,343,351,354,371,378,382,387,398,401,406,412,415,419,425,481,488,492,495,498,526,529,612,615,619,625,735,738,742,745,804,807,811,814,817,834,841,926,929,947,953,961,967,971,977,1012,1015,1052,1055,1075,1082,1086,1091,1094,1116,1126,1133,1137,1140,1157,1161,1165,1168,1172,1181,1239,1243,1253,1257,1260,1264,1295],[10,11,5],"h1",{"id":12},"keyboardinterrupt-exception-in-python-explained",[14,15,16,20,21,25,26,29],"p",{},[17,18,19],"code",{},"KeyboardInterrupt"," is a Python exception that happens when ",[22,23,24],"strong",{},"you stop a running program from the keyboard",". In most cases, this means you pressed ",[22,27,28],{},"Ctrl+C"," in the terminal.",[14,31,32,33,35,36,39],{},"This page explains what ",[17,34,19],{}," means, when it appears, and how to handle it safely. It is usually ",[22,37,38],{},"not a bug in your code",". Instead, it means the program was interrupted by the user.",[41,42,44],"h2",{"id":43},"quick-fix","Quick fix",[14,46,47,48,51],{},"If you want your program to stop cleanly when the user presses Ctrl+C, wrap the code in a ",[17,49,50],{},"try-except"," block:",[53,54,59],"pre",{"className":55,"code":56,"language":57,"meta":58,"style":58},"language-python shiki shiki-themes material-theme-lighter github-light github-dark","try:\n    while True:\n        print(\"Running...\")\nexcept KeyboardInterrupt:\n    print(\"\\nProgram stopped by user\")\n","python","",[17,60,61,74,86,109,121],{"__ignoreMap":58},[62,63,66,70],"span",{"class":64,"line":65},"line",1,[62,67,69],{"class":68},"sVHd0","try",[62,71,73],{"class":72},"sP7_E",":\n",[62,75,77,80,84],{"class":64,"line":76},2,[62,78,79],{"class":68},"    while",[62,81,83],{"class":82},"s39Yj"," True",[62,85,73],{"class":72},[62,87,89,93,96,100,104,106],{"class":64,"line":88},3,[62,90,92],{"class":91},"sptTA","        print",[62,94,95],{"class":72},"(",[62,97,99],{"class":98},"sjJ54","\"",[62,101,103],{"class":102},"s_sjI","Running...",[62,105,99],{"class":98},[62,107,108],{"class":72},")\n",[62,110,112,115,119],{"class":64,"line":111},4,[62,113,114],{"class":68},"except",[62,116,118],{"class":117},"sZMiF"," KeyboardInterrupt",[62,120,73],{"class":72},[62,122,124,127,129,131,135,138,140],{"class":64,"line":123},5,[62,125,126],{"class":91},"    print",[62,128,95],{"class":72},[62,130,99],{"class":98},[62,132,134],{"class":133},"s_hVV","\\n",[62,136,137],{"class":102},"Program stopped by user",[62,139,99],{"class":98},[62,141,108],{"class":72},[14,143,144],{},"Use this when you want to avoid a long traceback and show a friendly exit message instead.",[41,146,148],{"id":147},"what-keyboardinterrupt-means","What KeyboardInterrupt means",[14,150,151,153],{},[17,152,19],{}," is a built-in exception in Python.",[14,155,156],{},"It means:",[158,159,160,164,169,175],"ul",{},[161,162,163],"li",{},"The program was interrupted from the keyboard",[161,165,166,167],{},"This usually happens when you press ",[22,168,28],{},[161,170,171,172],{},"It is ",[22,173,174],{},"not usually caused by a coding mistake",[161,176,177],{},"It often appears when a program is stuck in a loop, waiting for something, or running for a long time",[14,179,180,181,186],{},"If you are new to exceptions, see ",[182,183,185],"a",{"href":184},"\u002Flearn\u002Fpython-errors-and-exceptions-explained","Python errors and exceptions explained",".",[41,188,190],{"id":189},"when-this-exception-happens","When this exception happens",[14,192,193,194,196],{},"You may see ",[17,195,19],{}," in situations like these:",[158,198,199,206,209,212,219],{},[161,200,201,202,205],{},"During an infinite ",[17,203,204],{},"while"," loop",[161,207,208],{},"While waiting for user input",[161,210,211],{},"During a long-running task",[161,213,214,215,218],{},"While ",[17,216,217],{},"time.sleep()"," is pausing the program",[161,220,221],{},"When a script is processing a lot of data and you stop it manually",[14,223,224,225,186],{},"For example, a script may keep running until you interrupt it because it has no normal stop condition. This is common with ",[182,226,228],{"href":227},"\u002Flearn\u002Fpython-while-loops-explained","Python while loops",[41,230,232],{"id":231},"example-that-causes-keyboardinterrupt","Example that causes KeyboardInterrupt",[14,234,235],{},"Here is a simple program that keeps running forever:",[53,237,239],{"className":55,"code":238,"language":57,"meta":58,"style":58},"while True:\n    print(\"Still running...\")\n",[17,240,241,249],{"__ignoreMap":58},[62,242,243,245,247],{"class":64,"line":65},[62,244,204],{"class":68},[62,246,83],{"class":82},[62,248,73],{"class":72},[62,250,251,253,255,257,260,262],{"class":64,"line":76},[62,252,126],{"class":91},[62,254,95],{"class":72},[62,256,99],{"class":98},[62,258,259],{"class":102},"Still running...",[62,261,99],{"class":98},[62,263,108],{"class":72},[14,265,266],{},"This loop has no end. If you run it in a terminal, it will continue printing until you stop it.",[14,268,269,270,272,273,186],{},"A common way to stop it is to press ",[22,271,28],{},". When you do, Python may show a traceback ending with ",[17,274,19],{},[14,276,277],{},"A more realistic example uses a small delay:",[53,279,281],{"className":55,"code":280,"language":57,"meta":58,"style":58},"import time\n\nwhile True:\n    print(\"Working...\")\n    time.sleep(1)\n",[17,282,283,292,298,306,321],{"__ignoreMap":58},[62,284,285,288],{"class":64,"line":65},[62,286,287],{"class":68},"import",[62,289,291],{"class":290},"su5hD"," time\n",[62,293,294],{"class":64,"line":76},[62,295,297],{"emptyLinePlaceholder":296},true,"\n",[62,299,300,302,304],{"class":64,"line":88},[62,301,204],{"class":68},[62,303,83],{"class":82},[62,305,73],{"class":72},[62,307,308,310,312,314,317,319],{"class":64,"line":111},[62,309,126],{"class":91},[62,311,95],{"class":72},[62,313,99],{"class":98},[62,315,316],{"class":102},"Working...",[62,318,99],{"class":98},[62,320,108],{"class":72},[62,322,323,326,328,332,334,338],{"class":64,"line":123},[62,324,325],{"class":290},"    time",[62,327,186],{"class":72},[62,329,331],{"class":330},"slqww","sleep",[62,333,95],{"class":72},[62,335,337],{"class":336},"srdBf","1",[62,339,108],{"class":72},[14,341,342],{},"Possible output before interruption:",[53,344,349],{"className":345,"code":347,"language":348,"meta":58},[346],"language-text","Working...\nWorking...\nWorking...\n^CTraceback (most recent call last):\n  ...\nKeyboardInterrupt\n","text",[17,350,347],{"__ignoreMap":58},[14,352,353],{},"In this example:",[158,355,356,362,368],{},[161,357,358,361],{},[17,359,360],{},"while True"," creates an infinite loop",[161,363,364,367],{},[17,365,366],{},"time.sleep(1)"," pauses for 1 second each time",[161,369,370],{},"Pressing Ctrl+C interrupts the program while it is running or sleeping",[14,372,373,374,186],{},"If you want to understand that delay function better, see ",[182,375,377],{"href":376},"\u002Fstandard-library\u002Ftime.sleep-function-explained","time.sleep() explained",[41,379,381],{"id":380},"how-to-fix-or-handle-it","How to fix or handle it",[14,383,384,386],{},[17,385,19],{}," usually does not need a “fix” in the same way as a normal error. Instead, you decide whether the program should:",[158,388,389,392,395],{},[161,390,391],{},"Stop immediately",[161,393,394],{},"Stop cleanly with a message",[161,396,397],{},"Save data before exiting",[14,399,400],{},"Here are the main ways to handle it.",[402,403,405],"h3",{"id":404},"_1-press-ctrlc-only-when-you-want-to-stop-the-program","1. Press Ctrl+C only when you want to stop the program",[14,407,408,409,411],{},"If you manually interrupted the script, then ",[17,410,19],{}," is expected.",[14,413,414],{},"In that case, nothing may be wrong. You simply stopped the program on purpose.",[402,416,418],{"id":417},"_2-catch-keyboardinterrupt-for-a-clean-exit","2. Catch KeyboardInterrupt for a clean exit",[14,420,421,422,424],{},"Use ",[17,423,50],{}," if you want to show a friendly message instead of a traceback.",[53,426,427],{"className":55,"code":56,"language":57,"meta":58,"style":58},[17,428,429,435,443,457,465],{"__ignoreMap":58},[62,430,431,433],{"class":64,"line":65},[62,432,69],{"class":68},[62,434,73],{"class":72},[62,436,437,439,441],{"class":64,"line":76},[62,438,79],{"class":68},[62,440,83],{"class":82},[62,442,73],{"class":72},[62,444,445,447,449,451,453,455],{"class":64,"line":88},[62,446,92],{"class":91},[62,448,95],{"class":72},[62,450,99],{"class":98},[62,452,103],{"class":102},[62,454,99],{"class":98},[62,456,108],{"class":72},[62,458,459,461,463],{"class":64,"line":111},[62,460,114],{"class":68},[62,462,118],{"class":117},[62,464,73],{"class":72},[62,466,467,469,471,473,475,477,479],{"class":64,"line":123},[62,468,126],{"class":91},[62,470,95],{"class":72},[62,472,99],{"class":98},[62,474,134],{"class":133},[62,476,137],{"class":102},[62,478,99],{"class":98},[62,480,108],{"class":72},[14,482,483,484,186],{},"This is a simple use of exception handling. If you need more help with the syntax, see ",[182,485,487],{"href":486},"\u002Flearn\u002Fusing-try-except-else-and-finally-in-python","using try, except, else, and finally in Python",[402,489,491],{"id":490},"_3-use-a-proper-loop-condition-when-possible","3. Use a proper loop condition when possible",[14,493,494],{},"Infinite loops are sometimes useful, but many programs should stop normally.",[14,496,497],{},"Instead of this:",[53,499,501],{"className":55,"code":500,"language":57,"meta":58,"style":58},"while True:\n    print(\"Checking...\")\n",[17,502,503,511],{"__ignoreMap":58},[62,504,505,507,509],{"class":64,"line":65},[62,506,204],{"class":68},[62,508,83],{"class":82},[62,510,73],{"class":72},[62,512,513,515,517,519,522,524],{"class":64,"line":76},[62,514,126],{"class":91},[62,516,95],{"class":72},[62,518,99],{"class":98},[62,520,521],{"class":102},"Checking...",[62,523,99],{"class":98},[62,525,108],{"class":72},[14,527,528],{},"You can often use a condition:",[53,530,532],{"className":55,"code":531,"language":57,"meta":58,"style":58},"count = 0\n\nwhile count \u003C 3:\n    print(\"Checking...\")\n    count += 1\n\nprint(\"Done\")\n",[17,533,534,546,550,565,579,590,595],{"__ignoreMap":58},[62,535,536,539,543],{"class":64,"line":65},[62,537,538],{"class":290},"count ",[62,540,542],{"class":541},"smGrS","=",[62,544,545],{"class":336}," 0\n",[62,547,548],{"class":64,"line":76},[62,549,297],{"emptyLinePlaceholder":296},[62,551,552,554,557,560,563],{"class":64,"line":88},[62,553,204],{"class":68},[62,555,556],{"class":290}," count ",[62,558,559],{"class":541},"\u003C",[62,561,562],{"class":336}," 3",[62,564,73],{"class":72},[62,566,567,569,571,573,575,577],{"class":64,"line":111},[62,568,126],{"class":91},[62,570,95],{"class":72},[62,572,99],{"class":98},[62,574,521],{"class":102},[62,576,99],{"class":98},[62,578,108],{"class":72},[62,580,581,584,587],{"class":64,"line":123},[62,582,583],{"class":290},"    count ",[62,585,586],{"class":541},"+=",[62,588,589],{"class":336}," 1\n",[62,591,593],{"class":64,"line":592},6,[62,594,297],{"emptyLinePlaceholder":296},[62,596,598,601,603,605,608,610],{"class":64,"line":597},7,[62,599,600],{"class":91},"print",[62,602,95],{"class":72},[62,604,99],{"class":98},[62,606,607],{"class":102},"Done",[62,609,99],{"class":98},[62,611,108],{"class":72},[14,613,614],{},"This version ends by itself and does not need manual interruption.",[402,616,618],{"id":617},"_4-add-a-break-condition","4. Add a break condition",[14,620,621,622,624],{},"If you do need ",[17,623,360],{},", add a way to exit the loop.",[53,626,628],{"className":55,"code":627,"language":57,"meta":58,"style":58},"while True:\n    text = input(\"Type quit to stop: \")\n\n    if text == \"quit\":\n        break\n\n    print(\"You typed:\", text)\n\nprint(\"Program ended normally\")\n",[17,629,630,638,659,663,684,689,693,714,719],{"__ignoreMap":58},[62,631,632,634,636],{"class":64,"line":65},[62,633,204],{"class":68},[62,635,83],{"class":82},[62,637,73],{"class":72},[62,639,640,643,645,648,650,652,655,657],{"class":64,"line":76},[62,641,642],{"class":290},"    text ",[62,644,542],{"class":541},[62,646,647],{"class":91}," input",[62,649,95],{"class":72},[62,651,99],{"class":98},[62,653,654],{"class":102},"Type quit to stop: ",[62,656,99],{"class":98},[62,658,108],{"class":72},[62,660,661],{"class":64,"line":88},[62,662,297],{"emptyLinePlaceholder":296},[62,664,665,668,671,674,677,680,682],{"class":64,"line":111},[62,666,667],{"class":68},"    if",[62,669,670],{"class":290}," text ",[62,672,673],{"class":541},"==",[62,675,676],{"class":98}," \"",[62,678,679],{"class":102},"quit",[62,681,99],{"class":98},[62,683,73],{"class":72},[62,685,686],{"class":64,"line":123},[62,687,688],{"class":68},"        break\n",[62,690,691],{"class":64,"line":592},[62,692,297],{"emptyLinePlaceholder":296},[62,694,695,697,699,701,704,706,709,712],{"class":64,"line":597},[62,696,126],{"class":91},[62,698,95],{"class":72},[62,700,99],{"class":98},[62,702,703],{"class":102},"You typed:",[62,705,99],{"class":98},[62,707,708],{"class":72},",",[62,710,711],{"class":330}," text",[62,713,108],{"class":72},[62,715,717],{"class":64,"line":716},8,[62,718,297],{"emptyLinePlaceholder":296},[62,720,722,724,726,728,731,733],{"class":64,"line":721},9,[62,723,600],{"class":91},[62,725,95],{"class":72},[62,727,99],{"class":98},[62,729,730],{"class":102},"Program ended normally",[62,732,99],{"class":98},[62,734,108],{"class":72},[14,736,737],{},"This lets the user stop the program without pressing Ctrl+C.",[402,739,741],{"id":740},"_5-save-work-before-exiting-if-needed","5. Save work before exiting if needed",[14,743,744],{},"If your program writes to a file, stores data, or keeps a network connection open, you may want to clean up before exit.",[53,746,748],{"className":55,"code":747,"language":57,"meta":58,"style":58},"try:\n    while True:\n        print(\"Program is running...\")\nexcept KeyboardInterrupt:\n    print(\"\\nSaving progress before exit...\")\n",[17,749,750,756,764,779,787],{"__ignoreMap":58},[62,751,752,754],{"class":64,"line":65},[62,753,69],{"class":68},[62,755,73],{"class":72},[62,757,758,760,762],{"class":64,"line":76},[62,759,79],{"class":68},[62,761,83],{"class":82},[62,763,73],{"class":72},[62,765,766,768,770,772,775,777],{"class":64,"line":88},[62,767,92],{"class":91},[62,769,95],{"class":72},[62,771,99],{"class":98},[62,773,774],{"class":102},"Program is running...",[62,776,99],{"class":98},[62,778,108],{"class":72},[62,780,781,783,785],{"class":64,"line":111},[62,782,114],{"class":68},[62,784,118],{"class":117},[62,786,73],{"class":72},[62,788,789,791,793,795,797,800,802],{"class":64,"line":123},[62,790,126],{"class":91},[62,792,95],{"class":72},[62,794,99],{"class":98},[62,796,134],{"class":133},[62,798,799],{"class":102},"Saving progress before exit...",[62,801,99],{"class":98},[62,803,108],{"class":72},[14,805,806],{},"For some programs, a clean shutdown is important.",[41,808,810],{"id":809},"how-to-exit-cleanly","How to exit cleanly",[14,812,813],{},"A clean exit means your program stops in a controlled way.",[14,815,816],{},"This is helpful when you want to:",[158,818,819,822,825,828,831],{},[161,820,821],{},"Print a friendly message",[161,823,824],{},"Close files",[161,826,827],{},"Close connections",[161,829,830],{},"Save progress",[161,832,833],{},"Always run cleanup code",[14,835,836,837,840],{},"Here is a simple example with ",[17,838,839],{},"finally",":",[53,842,844],{"className":55,"code":843,"language":57,"meta":58,"style":58},"try:\n    print(\"Program started\")\n    while True:\n        pass\nexcept KeyboardInterrupt:\n    print(\"\\nInterrupted by user\")\nfinally:\n    print(\"Cleanup code runs before exit\")\n",[17,845,846,852,867,875,880,888,905,911],{"__ignoreMap":58},[62,847,848,850],{"class":64,"line":65},[62,849,69],{"class":68},[62,851,73],{"class":72},[62,853,854,856,858,860,863,865],{"class":64,"line":76},[62,855,126],{"class":91},[62,857,95],{"class":72},[62,859,99],{"class":98},[62,861,862],{"class":102},"Program started",[62,864,99],{"class":98},[62,866,108],{"class":72},[62,868,869,871,873],{"class":64,"line":88},[62,870,79],{"class":68},[62,872,83],{"class":82},[62,874,73],{"class":72},[62,876,877],{"class":64,"line":111},[62,878,879],{"class":68},"        pass\n",[62,881,882,884,886],{"class":64,"line":123},[62,883,114],{"class":68},[62,885,118],{"class":117},[62,887,73],{"class":72},[62,889,890,892,894,896,898,901,903],{"class":64,"line":592},[62,891,126],{"class":91},[62,893,95],{"class":72},[62,895,99],{"class":98},[62,897,134],{"class":133},[62,899,900],{"class":102},"Interrupted by user",[62,902,99],{"class":98},[62,904,108],{"class":72},[62,906,907,909],{"class":64,"line":597},[62,908,839],{"class":68},[62,910,73],{"class":72},[62,912,913,915,917,919,922,924],{"class":64,"line":716},[62,914,126],{"class":91},[62,916,95],{"class":72},[62,918,99],{"class":98},[62,920,921],{"class":102},"Cleanup code runs before exit",[62,923,99],{"class":98},[62,925,108],{"class":72},[14,927,928],{},"What this does:",[158,930,931,936,942],{},[161,932,933,935],{},[17,934,69],{}," runs the main code",[161,937,938,941],{},[17,939,940],{},"except KeyboardInterrupt"," handles Ctrl+C",[161,943,944,946],{},[17,945,839],{}," runs whether an interruption happens or not",[14,948,949,950,952],{},"If something must always happen before the program ends, ",[17,951,839],{}," is a good choice.",[14,954,955,956,960],{},"In some programs, you may also choose to exit intentionally with ",[182,957,959],{"href":958},"\u002Fstandard-library\u002Fsys.exit-function-explained","sys.exit()",", but that is different from a user pressing Ctrl+C.",[14,962,963,964,966],{},"Also, avoid catching ",[17,965,19],{}," and doing nothing unless you have a good reason. Hiding the interruption can make your program harder to stop and harder to debug.",[41,968,970],{"id":969},"debugging-steps","Debugging steps",[14,972,973,974,976],{},"If you see ",[17,975,19],{}," and are not sure why, try these steps:",[158,978,979,985,988,991,998,1005],{},[161,980,981,982],{},"Check whether ",[22,983,984],{},"you pressed Ctrl+C",[161,986,987],{},"Look for infinite loops",[161,989,990],{},"Check for slow code that seems stuck",[161,992,993,994,997],{},"Look for ",[17,995,996],{},"input()"," calls waiting for user input",[161,999,1000,1001,1004],{},"Check for long ",[17,1002,1003],{},"sleep()"," delays",[161,1006,1007,1008,1011],{},"Add ",[17,1009,1010],{},"print()"," statements to see where the program stops",[14,1013,1014],{},"Helpful commands:",[53,1016,1020],{"className":1017,"code":1018,"language":1019,"meta":58,"style":58},"language-bash shiki shiki-themes material-theme-lighter github-light github-dark","python script.py\npython -u script.py\npython -m pdb script.py\n","bash",[17,1021,1022,1030,1040],{"__ignoreMap":58},[62,1023,1024,1027],{"class":64,"line":65},[62,1025,57],{"class":1026},"sbgvK",[62,1028,1029],{"class":102}," script.py\n",[62,1031,1032,1034,1038],{"class":64,"line":76},[62,1033,57],{"class":1026},[62,1035,1037],{"class":1036},"stzsN"," -u",[62,1039,1029],{"class":102},[62,1041,1042,1044,1047,1050],{"class":64,"line":88},[62,1043,57],{"class":1026},[62,1045,1046],{"class":1036}," -m",[62,1048,1049],{"class":102}," pdb",[62,1051,1029],{"class":102},[14,1053,1054],{},"What they do:",[158,1056,1057,1063,1069],{},[161,1058,1059,1062],{},[17,1060,1061],{},"python script.py"," runs the script normally",[161,1064,1065,1068],{},[17,1066,1067],{},"python -u script.py"," runs Python in unbuffered mode, so output appears immediately",[161,1070,1071,1074],{},[17,1072,1073],{},"python -m pdb script.py"," starts the Python debugger",[14,1076,1077,1078,186],{},"If you want more help tracking problems step by step, see the ",[182,1079,1081],{"href":1080},"\u002Fhow-to\u002Fhow-to-debug-python-code-beginner-guide","beginner guide to debugging Python code",[41,1083,1085],{"id":1084},"keyboardinterrupt-vs-other-exceptions","KeyboardInterrupt vs other exceptions",[14,1087,1088,1090],{},[17,1089,19],{}," is different from many common Python exceptions.",[14,1092,1093],{},"For example:",[158,1095,1096,1104,1110],{},[161,1097,1098,1100,1101],{},[17,1099,19],{}," usually happens because the ",[22,1102,1103],{},"user interrupted the program",[161,1105,1106,1109],{},[17,1107,1108],{},"ValueError"," happens when a value has the wrong form",[161,1111,1112,1115],{},[17,1113,1114],{},"RuntimeError"," happens when a program fails during execution for a different reason",[14,1117,1118,1119,1121,1122,1125],{},"So ",[17,1120,19],{}," is usually ",[22,1123,1124],{},"about stopping a running script",", not about invalid code.",[14,1127,1128,1129,186],{},"If you are comparing exception types, you may also want to read about ",[182,1130,1132],{"href":1131},"\u002Ferrors\u002Fruntimeerror-in-python-causes-and-fixes","RuntimeError in Python",[41,1134,1136],{"id":1135},"common-causes","Common causes",[14,1138,1139],{},"The most common causes are:",[158,1141,1142,1145,1148,1154],{},[161,1143,1144],{},"Pressing Ctrl+C while a script is running",[161,1146,1147],{},"Manually stopping an infinite loop",[161,1149,1150,1151,1153],{},"Interrupting a long ",[17,1152,217],{}," call",[161,1155,1156],{},"Stopping a script that is waiting for input or network activity",[41,1158,1160],{"id":1159},"faq","FAQ",[402,1162,1164],{"id":1163},"is-keyboardinterrupt-an-error-in-my-code","Is KeyboardInterrupt an error in my code?",[14,1166,1167],{},"Usually no. It normally means the program was stopped by the user with Ctrl+C.",[402,1169,1171],{"id":1170},"how-do-i-stop-keyboardinterrupt-from-showing-a-traceback","How do I stop KeyboardInterrupt from showing a traceback?",[14,1173,1174,1175,1177,1178,1180],{},"Catch ",[17,1176,19],{}," with ",[17,1179,50],{}," and print your own message before exiting.",[53,1182,1184],{"className":55,"code":1183,"language":57,"meta":58,"style":58},"try:\n    while True:\n        print(\"Running...\")\nexcept KeyboardInterrupt:\n    print(\"\\nStopped cleanly\")\n",[17,1185,1186,1192,1200,1214,1222],{"__ignoreMap":58},[62,1187,1188,1190],{"class":64,"line":65},[62,1189,69],{"class":68},[62,1191,73],{"class":72},[62,1193,1194,1196,1198],{"class":64,"line":76},[62,1195,79],{"class":68},[62,1197,83],{"class":82},[62,1199,73],{"class":72},[62,1201,1202,1204,1206,1208,1210,1212],{"class":64,"line":88},[62,1203,92],{"class":91},[62,1205,95],{"class":72},[62,1207,99],{"class":98},[62,1209,103],{"class":102},[62,1211,99],{"class":98},[62,1213,108],{"class":72},[62,1215,1216,1218,1220],{"class":64,"line":111},[62,1217,114],{"class":68},[62,1219,118],{"class":117},[62,1221,73],{"class":72},[62,1223,1224,1226,1228,1230,1232,1235,1237],{"class":64,"line":123},[62,1225,126],{"class":91},[62,1227,95],{"class":72},[62,1229,99],{"class":98},[62,1231,134],{"class":133},[62,1233,1234],{"class":102},"Stopped cleanly",[62,1236,99],{"class":98},[62,1238,108],{"class":72},[402,1240,1242],{"id":1241},"does-keyboardinterrupt-happen-only-in-loops","Does KeyboardInterrupt happen only in loops?",[14,1244,1245,1246,1248,1249,1252],{},"No. It can happen during ",[17,1247,331],{},", ",[17,1250,1251],{},"input",", file work, or other long-running operations if you interrupt the program.",[402,1254,1256],{"id":1255},"should-i-always-catch-keyboardinterrupt","Should I always catch KeyboardInterrupt?",[14,1258,1259],{},"Not always. Catch it when you want a clean shutdown or need to save data before exiting.",[41,1261,1263],{"id":1262},"see-also","See also",[158,1265,1266,1270,1275,1281,1286,1290],{},[161,1267,1268],{},[182,1269,185],{"href":184},[161,1271,1272],{},[182,1273,1274],{"href":486},"Using try, except, else, and finally in Python",[161,1276,1277],{},[182,1278,1280],{"href":1279},"\u002Fhow-to\u002Fhow-to-exit-a-program-in-python","How to exit a program in Python",[161,1282,1283],{},[182,1284,1285],{"href":227},"Python while loops explained",[161,1287,1288],{},[182,1289,377],{"href":376},[161,1291,1292],{},[182,1293,1294],{"href":1080},"Beginner guide to debugging Python code",[1296,1297,1298],"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 .s39Yj, html code.shiki .s39Yj{--shiki-light:#39ADB5;--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 .sZMiF, html code.shiki .sZMiF{--shiki-light:#E2931D;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .s_hVV, html code.shiki .s_hVV{--shiki-light:#90A4AE;--shiki-default:#005CC5;--shiki-dark:#79B8FF}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 .su5hD, html code.shiki .su5hD{--shiki-light:#90A4AE;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .slqww, html code.shiki .slqww{--shiki-light:#6182B8;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .srdBf, html code.shiki .srdBf{--shiki-light:#F76D47;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .smGrS, html code.shiki .smGrS{--shiki-light:#39ADB5;--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .sbgvK, html code.shiki .sbgvK{--shiki-light:#E2931D;--shiki-default:#6F42C1;--shiki-dark:#B392F0}html pre.shiki code .stzsN, html code.shiki .stzsN{--shiki-light:#91B859;--shiki-default:#005CC5;--shiki-dark:#79B8FF}",{"title":58,"searchDepth":76,"depth":76,"links":1300},[1301,1302,1303,1304,1305,1312,1313,1314,1315,1316,1322],{"id":43,"depth":76,"text":44},{"id":147,"depth":76,"text":148},{"id":189,"depth":76,"text":190},{"id":231,"depth":76,"text":232},{"id":380,"depth":76,"text":381,"children":1306},[1307,1308,1309,1310,1311],{"id":404,"depth":88,"text":405},{"id":417,"depth":88,"text":418},{"id":490,"depth":88,"text":491},{"id":617,"depth":88,"text":618},{"id":740,"depth":88,"text":741},{"id":809,"depth":76,"text":810},{"id":969,"depth":76,"text":970},{"id":1084,"depth":76,"text":1085},{"id":1135,"depth":76,"text":1136},{"id":1159,"depth":76,"text":1160,"children":1317},[1318,1319,1320,1321],{"id":1163,"depth":88,"text":1164},{"id":1170,"depth":88,"text":1171},{"id":1241,"depth":88,"text":1242},{"id":1255,"depth":88,"text":1256},{"id":1262,"depth":76,"text":1263},"Master keyboardinterrupt exception in python explained in our comprehensive Python beginner guide.","md",{},"\u002Ferrors\u002Fkeyboardinterrupt-exception-in-python-explained",{"title":5,"description":1323},"errors\u002Fkeyboardinterrupt-exception-in-python-explained","i3ELmDwhDKOftfYUWsKdv818rrVPGjWeLAK5I6_pp8Y",1777585480200]