1

I have two questions:

If I have a method like this:

public void DoMyWork()
{
   throw new MyException(anyString);
}

...and I call it async like this:

public void DoMyWorkAsync()
{
   try
   {
      new Thread(DoMyWork).Start();
   }

   catch (MyException)
   {
      // Do anything
   }
}

First of all, will the exception be caught with a try-block like this? And if so, will the thread be ended, because normally with an exception the thread stops, but if I catch it, will it end, too, or do I have to implement a CancellationToken then?

Brandon
  • 645
  • 5
  • 13
Dominic B.
  • 1,897
  • 1
  • 16
  • 31

2 Answers2

2

1) No, it will not. Exceptions in threads must be dealt with in the thread. The main thread does not control the flow of execution of the thread, thus, does not capture the exception.

2) The application will terminate. More here: http://msdn.microsoft.com/en-us/library/ms228965(v=vs.110).aspx

Adriano Carneiro
  • 57,693
  • 12
  • 90
  • 123
  • Hm ok, I cannot just go ahead and put a try/catch around it, because the method should be used as normal, too and it should be free how you would handle the exceptions, that is the problem. – Dominic B. May 06 '14 at 16:54
1

First of all, will the exception be catched with a try-block like this?

No, it won't be caught.

You need to wrap the code inside DoMyWork method with try/catch to catch that exception.

Note:It will result in "Unhandled exception", any unhandled exception will tear down the process(Application crash).

Sriram Sakthivel
  • 72,067
  • 7
  • 111
  • 189
  • Ok thanks. So, I cannot just go ahead and put a try/catch around it, because the method should be used as normal, too and it should be free how you would handle the exceptions, that is the problem. – Dominic B. May 06 '14 at 16:53
  • Only way is catch the exceptions in expected places, if you don't you'll be punished with "Application crash" – Sriram Sakthivel May 06 '14 at 16:55
  • Ok, is there maybe an alternative? Like tasks? How far can I do it with them maybe? – Dominic B. May 06 '14 at 17:00
  • @Trade Yes, Tasks are better alternatives. It will not crash your process if unhandled exception is thrown(in .net 4.5). You can read more about it [here](http://blogs.msdn.com/b/pfxteam/archive/2009/05/31/9674669.aspx) and [here](http://blogs.msdn.com/b/pfxteam/archive/2011/09/28/task-exception-handling-in-net-4-5.aspx) – Sriram Sakthivel May 06 '14 at 17:10
  • Thanks! I also found this. http://stackoverflow.com/questions/5983779/catch-exception-that-is-thrown-in-different-thread Is it possible to make the handler overridable, so that the user could implement his own way to handle the exeption? Would that be a solution? – Dominic B. May 06 '14 at 17:22
  • Yes, you can pass the `Action` while creating task which you will store in `Task.State` property, then in `ExceptionHandler` method you will call the `action(task.Exception)` so that user will be able to pass thier own delegates which encapsulates custom error handling. – Sriram Sakthivel May 06 '14 at 17:55