Skip to content

Instantly share code, notes, and snippets.

@mqamarmunir
Created August 31, 2017 07:16
Show Gist options
  • Save mqamarmunir/00ac695d8663b58e1becea70c88daa36 to your computer and use it in GitHub Desktop.
Save mqamarmunir/00ac695d8663b58e1becea70c88daa36 to your computer and use it in GitHub Desktop.
static void Main(string[] args)
{
try
{
// output "hello world" as method returns early
Console.WriteLine(GetStringData());
}
catch
{
// Exception is NOT caught here
}
Console.ReadLine();
}
public static string GetStringData()
{
MyAsyncMethod().ContinueWith(OnMyAsyncMethodFailed, TaskContinuationOptions.OnlyOnFaulted);
return "hello world";
}
public static async Task MyAsyncMethod()
{
await Task.Run(() => { throw new Exception("thrown on background thread"); });
}
public static void OnMyAsyncMethodFailed(Task task)
{
Exception ex = task.Exception;
// Deal with exceptions here however you want
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment