F#无法捕获TimeoutException [英] F# can't catch TimeoutException

查看:220
本文介绍了F#无法捕获TimeoutException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题很简单。请看看截图:





如何发生?我明确地将Async.RunSyncronly调用到 try ... with

解决方案

在F#异步工作流程中尝试/使用不直接映射到CLR保护的块 - 相反,如果在用户代码中引发异常,则库代码将捕获并重新路由到最近的错误继续(可以是,块 finally block ,定制错误继续在 Async.StartWithContinueations etc ...)。这有一个后果,调试器可能会报告用户代码中未处理的异常,可以在稍后处理和处理。



下面的代码片段报告了调试器中的类似错误,但执行完成成功

  let error():int = raise(System.TimeoutException())

让运行()= async {
try
let result = error()
return result
with
:? System.TimeoutException - >返回-1
}

let r = Async.RunSynchronously(run())
printfn%dr
pre>

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.e with block, finally block, custom error continuation supplied in Async.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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆