How to handle Python exception in Threads?

Python allows you to run multiple tasks at the same time using threads. However, handling exceptions in threads can be tricky because exceptions that happen inside a thread don't get passed to the main thread by default.

To deal with this, you can catch errors inside the thread's function or create a custom thread class to handle them. This helps you find issues in threads and handle them properly in the main program.

Exception Handling in Threads

When an exception occurs inside a thread, it only affects that thread, and unless explicitly handled, it will be ignored silently. To properly handle these exceptions, we need to catch them inside the thread function.

Example

In the following example, we start a thread that raises an exception. If we don't catch it inside the thread, the main program does not know about it −

import threading

def faulty_thread():
    print("Thread started")
    raise ValueError("Something went wrong inside the thread")

t = threading.Thread(target=faulty_thread)
t.start()
t.join()
print("Main program continues...")

We can see in the output below that the thread crashes with a ValueError, but the main program continues execution −

Thread started
Exception in thread Thread-1 (faulty_thread):
Traceback (most recent call last):
  File "/usr/lib/python3.x/threading.py", line xxx, in run
    self._target(*self._args, **self._kwargs)
  File "<string>", line 5, in faulty_thread
ValueError: Something went wrong inside the thread
Main program continues...

Using Try-Except Inside Thread

To catch exceptions, wrap the code inside the thread's target function using a try-except block. This way, any error that occurs within the thread can be handled without crashing the program.

Example

In this example, the exception is caught and handled within the thread, so the thread does not crash unexpectedly −

import threading

def safe_thread():
    try:
        print("Thread started")
        raise ValueError("Error inside thread")
    except Exception as e:
        print("Caught exception in thread:", e)

t = threading.Thread(target=safe_thread)
t.start()
t.join()
print("Main program continues...")

The output of the above code is −

Thread started
Caught exception in thread: Error inside thread
Main program continues...

Using Custom Thread Class

You can create a custom thread class that captures exceptions and stores them in an attribute. This allows the main program to check for errors after the thread has finished running.

Example

In this example, we subclass Thread to save any exception that occurs and check it after the thread has finished −

import threading

class SafeThread(threading.Thread):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.exception = None

    def run(self):
        try:
            if self._target:
                self._target(*self._args, **self._kwargs)
        except Exception as e:
            self.exception = e

def task():
    raise RuntimeError("Thread failure")

t = SafeThread(target=task)
t.start()
t.join()

if t.exception:
    print("Exception caught from thread:", t.exception)

The output of the above code is −

Exception caught from thread: Thread failure

Using Queue for Exception Communication

Another approach is to use a queue to pass exceptions from the thread back to the main program. This method is useful when you need to handle multiple threads and their exceptions.

Example

Here we use a queue to communicate exceptions between threads and the main program −

import threading
import queue

def worker_thread(error_queue):
    try:
        print("Thread working...")
        raise ConnectionError("Network error occurred")
    except Exception as e:
        error_queue.put(e)

# Create queue for error communication
error_queue = queue.Queue()

t = threading.Thread(target=worker_thread, args=(error_queue,))
t.start()
t.join()

# Check for exceptions
if not error_queue.empty():
    exception = error_queue.get()
    print("Exception from thread:", exception)

The output of the above code is −

Thread working...
Exception from thread: Network error occurred

Comparison

Method Pros Cons
Try-except in thread Simple, handles locally Main thread unaware of errors
Custom thread class Main thread can check exceptions Requires subclassing
Queue communication Works with multiple threads More complex setup

Conclusion

Use try-except blocks inside thread functions for simple error handling. For communication with the main thread, use custom thread classes or queues to pass exceptions back to the main program.

Updated on: 2026-03-24T16:28:44+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements