我知道如何捕获特定异常,如下例所示:
let test_zip_archive candidate_zip_archive =
let rc =
try
ZipFile.Open(candidate_zip_archive.ToString(), ZipArchiveMode.Read) |> ignore
zip_file_ok
with
| :? System.IO.InvalidDataException -> not_a_zip_file
| :? System.IO.FileNotFoundException -> file_not_found
| :? System.NotSupportedException -> unsupported_exception
rc
我正在阅读一堆文章,看看是否可以在with
,就像通配符匹配一样。这样的构造是否存在,如果存在,它是什么?
是的,你可以这样做:
let foo =
try
//stuff (e.g.)
//failwith "wat?"
//raise (System.InvalidCastException())
0 //return 0 when OK
with ex ->
printfn "%A" ex //ex is any exception
-1 //return -1 on error
这和C#的一样catch (Exception ex) { }
要放弃你可以做的错误with _ -> -1
(与C#的相同catch { }
)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)