I have a windows service which runs a number of async tasks, however I'm having a problem with catching exceptions and returning them back to the calling method. In the following psuedo-code example, the try/catch is simply stopping the entire service rather than running up the stack. What's the best way to implement a Try/Catch in an async task?
foreach (var t in tables)
{
try
{
new Task(async () =>
{
try
{
//Do stuff
}
catch (DataError e)
{
throw e;
}
catch (Exception e)
{
throw e;
}
}
};
}
catch (Exception e)
{
return new DatabaseActionResult();
}
}
Ultimately, I want to catch and return a DatabaseActionResult object no matter what happens in any task. Again though, once the throw e happens, it totally shuts down the service and none of the other Try/Catch blocks catch anything.
How can I get this working without "awaiting" the task?