try: file = open("example.txt", "w") file.write("Hello, World! This is a test file.") except Exception as e: print(f"出现错误: {e}") finally: file.close()