Durante l'esecuzione normale del programma può verificarsi un'eccezione.Come ignorare le eccezioni in F #
Nel caso in cui sono a conoscenza e voglio solo ignorarlo - come faccio a raggiungere questo in F #?
Ecco il mio codice, che compila con un avvertimento:
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
L'avvertimento è:
error FS0010: Unexpected keyword 'finally' in binding. Expected incomplete structured construct at or before this point or other token.
La corrispondente C# equivalenti per quello che voglio è:
FileStream fs = null;
try
{
fs = File.OpenRead(file);
// ... other stuff
}
catch
{
// I just do not specify anything
}
finally
{
if (fs != null)
fs.Dispose()
}
Se ometto il blocco with
in F #, l'eccezione non viene ignorata.