|
Este artículo se tradujo de forma manual. Mueva el puntero sobre las frases del artículo para ver el texto original. Más información.
|
Traducción
Original
|
Excepciones: la expresión try...finally (F#)
try expression1 finally expression2
let divide x y = let stream : System.IO.FileStream = System.IO.File.Create("test.txt") let writer : System.IO.StreamWriter = new System.IO.StreamWriter(stream) try writer.WriteLine("test1"); Some( x / y ) finally writer.Flush() printfn "Closing stream" stream.Close() let result = try divide 100 0 with | :? System.DivideByZeroException -> printfn "Exception handled."; None
Closing stream Exception handled.
exception InnerError of string exception OuterError of string let function1 x y = try try if x = y then raise (InnerError("inner")) else raise (OuterError("outer")) with | InnerError(str) -> printfn "Error1 %s" str finally printfn "Always print this." let function2 x y = try function1 x y with | OuterError(str) -> printfn "Error2 %s" str function2 100 100 function2 100 10