Created
June 17, 2019 08:09
-
-
Save tocalai/c2af3bb0e1f22f145b099cf4823c3c72 to your computer and use it in GitHub Desktop.
Traditional handle task with try-catch block
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
class Program | |
{ | |
static async System.Threading.Tasks.Task Main (string[] args) | |
{ | |
// register of global domain unhandled exception hanlder | |
AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException; | |
// traditional handle task with try-catch block | |
var result = await RunTaskWithExceptionInnerTryCatch (); | |
Console.WriteLine ("Press any key to exit..."); | |
Console.ReadKey (); | |
} | |
private static void CurrentDomain_UnhandledException (object sender, UnhandledExceptionEventArgs e) | |
{ | |
Console.WriteLine ($"Unhandled exception occurs {e.ExceptionObject}."); | |
} | |
private static Task<int> RunTaskWithExceptionInnerTryCatch () | |
{ | |
return System.Threading.Tasks.Task.Factory.StartNew (() => | |
{ | |
try | |
{ | |
var n1 = 1; | |
var n2 = 0; | |
var result = n1 / n2; | |
return result; | |
} | |
catch (Exception ex) | |
{ | |
Console.WriteLine ($"Exception occurs, {ex.Message}"); | |
return 0; | |
} | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment