F#无法捕获TimeoutException [英] F# can't catch TimeoutException
问题描述
如何发生?我明确地将Async.RunSyncronly调用到 try ... with
。
在F#异步工作流程中尝试/使用不直接映射到CLR保护的块 - 相反,如果在用户代码中引发异常,则库代码将捕获并重新路由到最近的错误继续(可以是,块
, finally block
,定制错误继续在 Async.StartWithContinueations
etc ...)。这有一个后果,调试器可能会报告用户代码中未处理的异常,可以在稍后处理和处理。
下面的代码片段报告了调试器中的类似错误,但执行完成成功
let error():int = raise(System.TimeoutException())
pre>
让运行()= async {
try
let result = error()
return result
with
:? System.TimeoutException - >返回-1
}
let r = Async.RunSynchronously(run())
printfn%dr
My question is really simple. Please take a look at screenshot:
How could that happen? I explicitly put call to Async.RunSyncronously into
try ... with
.解决方案
try/with
in F# async workflows do not map directly to CLR protected blocks - instead if exception is raised in user code, library code will catch it and re-route to the nearest error continuation (which can be i.ewith block
,finally block
, custom error continuation supplied inAsync.StartWithContinuations
etc...). This has a consequence that debugger might report about unhandled exceptions in user code that can be handled and processed later.Snippet below reports the similar error in debugger but nevertheless execution is finished successfully
let error (): int = raise (System.TimeoutException()) let run() = async { try let result = error() return result with :? System.TimeoutException -> return -1 } let r = Async.RunSynchronously (run()) printfn "%d" r
这篇关于F#无法捕获TimeoutException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!