通常のプログラムの実行中に例外が発生することがあります。F#で例外を無視する方法
私はそれを認識しており、それを無視したい場合は、どうすればF#でこれを達成できますか?ここで
は警告でコンパイル私のコードです:
let sha = new SHA1CryptoServiceProvider()
let maxLength = 10000
let fileSign file =
let fs = File.OpenRead(file)
let mutable res = (0L, [|0uy|])
try
let flLen = fs.Length
let len = int (min (int64 maxLength) flLen)
// read 'len' bytes
let mutable pos = 0
while (pos < len) do
let chunk = fs.Read(buf, pos, len - pos)
pos <- pos + chunk
// get signature
let sign = sha.ComputeHash(buf, 0, len)
// store new result
res <- (flLen, sign)
with
| :? IOException as e -> e |> ignore
finally
if (fs <> null) then
fs.Dispose()
res
警告は次のとおりです。
error FS0010: Unexpected keyword 'finally' in binding. Expected incomplete structured construct at or before this point or other token.
私が欲しいもののために対応するC#と同等です:
FileStream fs = null;
try
{
fs = File.OpenRead(file);
// ... other stuff
}
catch
{
// I just do not specify anything
}
finally
{
if (fs != null)
fs.Dispose()
}
F#でwith
ブロックを省略しただけでは、例外は無視されません。