Reputation: 1907
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?
Upvotes: 1
Views: 291
Reputation: 58665
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
Upvotes: 2
Reputation: 73502
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).
Upvotes: 1