Catching an exception thrown in an asynchronous callback

后端 未结 3 608
日久生厌
日久生厌 2021-01-04 23:10

I have a method that takes a callback argument to execute asynchronously, but the catch block doesn\'t seem to be catching any exceptions thrown by the synchronous call (

3条回答
  •  一整个雨季
    2021-01-04 23:37

    In short, no.

    When you call submitDelegate.BeginInvoke, it spawns the new thread, returns, and promptly exits your try/catch block (while the new thread runs in the background).

    You could, however, catch all unhandled exceptions like this:

    AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(YourException);

    This will catch everything in the application domain, however (not just your async method).

提交回复
热议问题