Created
June 17, 2019 08:22
-
-
Save tocalai/3beb9be53ca566a74f9c23360436ae22 to your computer and use it in GitHub Desktop.
Register of global task unobserved task exception handler
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 void Main (string[] args) | |
{ | |
// register of global domain unhandled exception hanlder | |
AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException; | |
// register of global task unobserved task exception handler | |
TaskScheduler.UnobservedTaskException += TaskScheduler_UnobservedTaskException; | |
// exception was catch by global task unobserved task exception handler | |
var t1 = RunTaskWithException (); | |
#if (!DEBUG) | |
Thread.Sleep(1000); | |
GC.Collect(); | |
GC.WaitForPendingFinalizers(); | |
GC.Collect(); | |
Thread.Sleep(1000); | |
#endif | |
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 void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e) | |
{ | |
Console.WriteLine($"Unhandled exception occurs {e.ExceptionObject}."); | |
} | |
private static Task<int> RunTaskWithException () | |
{ | |
return System.Threading.Tasks.Task.Factory.StartNew (() => | |
{ | |
var n1 = 1; | |
var n2 = 0; | |
var result = n1 / n2; | |
return result; | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment