try: #your code here print("Hello world!") except KeyboardInterrupt: #your code here print("Keyboard interrupt")