Created
August 31, 2017 07:16
-
-
Save mqamarmunir/00ac695d8663b58e1becea70c88daa36 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
Reference: https://stackoverflow.com/questions/15522900/how-to-safely-call-an-async-method-in-c-sharp-without-await