import threading import time import os def worker(name): try: while True: print(f"Thread {name} is working...") time.sleep(1) if name == "Thread-2": raise Exception("Error in Thread-2") except Exception as e: print(f"Error occurred in {name}: {str(e)}") os._exit(1) # 建立多個執行緒 threads = [] for i in range(3): t = threading.Thread(target=worker, args=(f"Thread-{i+1}",)) threads.append(t) t.start() # 等待所有執行緒完成 for t in threads: t.join()