gpt4 book ai didi

asynchronous - 为什么 Async.Start 似乎会传播无法捕获的异常?

转载 作者:行者123 更新时间:2023-12-04 05:00:01 25 4
gpt4 key购买 nike

考虑这个控制台应用程序:

let throwAsync = 
async { failwith "" }

[<EntryPoint>]
let main argv =
try Async.Start throwAsync
with _ -> printfn "Failed"
System.Console.Read() |> ignore
0

该应用程序在运行时立即崩溃。这对我来说没有意义,有两个原因:
  • AFAIK(例如 this blog post )异常不应该通过 Async.Start 冒泡(渲染 try ... with 毫无意义,但它在第 2 点)
  • (令人惊讶的)抛出代码被 try ... with 包围,但未捕获异常(它从不打印 "Failed" ,并且应用程序再次崩溃)。

  • 这是怎么回事?

    最佳答案

    在异步块执行的线程池线程上抛出异常。
    所以是的,这意味着异常不会传播到运行 Async.Start 的线程。 ,以及 try-with块永远不会被击中。但是,这也意味着异常现在会在其他地方抛出,如果没有任何异常处理,它会使您的应用程序崩溃。
    报价MSDN :

    Unhandled exceptions in thread pool threads terminate the process. There are three exceptions to this rule:

    • A System.Threading.ThreadAbortException is thrown in a thread pool thread because Thread.Abort was called.
    • A System.AppDomainUnloadedException is thrown in a thread pool thread because the application domain is being unloaded.
    • The common language runtime or a host process terminates the thread.

    For more information, see Exceptions in Managed Threads.

    关于asynchronous - 为什么 Async.Start 似乎会传播无法捕获的异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55693931/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com