কম্পিউটার টিউটোরিয়াল

পাইথনে থ্রেডে এক্সসেপশন হ্যান্ডলিং


দেওয়া কোডটি এক্সসেপশন ক্যাচ করতে নিচেরভাবে রি-রাইট করা হয়েছে:

import sys
import threading
import time
import Queue

def thread(args1, stop_event, queue_obj):
    print("start thread")
    stop_event.wait(12)
    if not stop_event.is_set():
        try:
            raise Exception("boom!")
        except Exception:
            queue_obj.put(sys.exc_info())
    pass

try:
    queue_obj = Queue.Queue()
    t_stop = threading.Event()
    t = threading.Thread(target=thread, args=(1, t_stop, queue_obj))
    t.start()
    time.sleep(15)
    print("stop thread!")
    t_stop.set()
    try:
        exc = queue_obj.get(block=False)
    except Queue.Empty:
        pass
    else:
        exc_type, exc_obj, exc_trace = exc
        print(exc_obj)
except Exception as e:
    print("It took too long")

আউটপুট:

C:/Users/TutorialsPoint1/~.py
start thread
stop thread!
boom!
  1. JSP-এ এক্সেপশন হ্যান্ডলিং: <c:catch> ট্যাগ

  2. C#-এ থ্রেড কীভাবে ডিস্ট্রয় করবেন?

  3. জাভাতে UncaughtExceptionHandler ব্যবহার করে এক্সেপশন কীভাবে হ্যান্ডেল করবেন?

  4. Tkinter Python-এ Thread ব্যবহার করার পদ্ধতি