Catching unhandled exception on separate threads

@Ani have already answered your question. Although I don’t agree that unhandled exceptions in threads should terminate applications. Using threads usually means that you have some kind of server application. Bringing it down could result in a lot of angry users.

I’ve written a small piece about proper exception handling: https://coderr.io/exception-handling

You should always catch exceptions for threads. I usually use the following pattern:

  void ThreadMethod(object state)
  {
      try
      {
          ActualWorkerMethod();
      }
      catch (Exception err)
      {
          _logger.Error("Unhandled exception in thread.", err);
      }
  }

  void ActualWorkerMethod()
  {
      // do something clever
  }

It’s a whole lot easier to find thread methods that doesn’t handle exceptions properly by moving the logic into a seperate method and just keep the try/catch block in the thread method.

Leave a Comment