I am trying to catch exceptions within a Task
I have created but they never "bubble up" to where the Task
is ran from, they are always caught by Visual Studio as runtime errors.
Here is a SSCCE I have created to show the issue
using System;
using System.Threading.Tasks;
namespace ConsoleApplicationSandbox
{
class Program
{
static void Main(string[] args)
{
CreateTheIssue();
Console.ReadLine();
}
public static async void CreateTheIssue()
{
try
{
int val = await ExceptionThrowerClass.DoSomething();
}
catch(Exception ex)
{
Console.Out.WriteLine("Exception of type " + ex.GetType() + " caught, message: " + ex.Message);
}
}
}
public static class ExceptionThrowerClass
{
public static Task<int> DoSomething()
{
return Task.Run(() =>
{
throw new FooException("How exceptional!");
return 1;
});
}
}
public class FooException : Exception
{
public FooException() : base () { }
public FooException(String message) : base(message) { }
public FooException(String message, Exception inner) : base (message, inner) { }
}
}
Visual Studio notifies me when I throw FooExcepion
in DoSomething
that the exception is not being caught. From this SO answer and this MSDN page I thought that the exceptions would just "bubble up" to myTask.Wait()
and subsequently be caught. Though this does not seem to be that case.
The project settings for this SCCEE are targeting .NET 4.5.2 and I am using Visual Studio 2015. Is it that my targeted .NET framework cannot catch exceptions in this manner? How can I catch FooException
in the try/catch
block within Main
?
EDIT: I have altered the SSCCE to use the async
/await
pattern instead of Task.Wait
.