Excepciones en F#
try/with atrapa por patrón; failwith lanza con mensaje.
// try/with catches by pattern; failwith raises with a message.
let openHive name =
if name = "Thistle" then failwith "wax-sealed shut"
sprintf "%s open for inspection" name
let attempt name =
try
openHive name
with
| Failure reason -> sprintf "%s stays closed: %s" name reason
["Willow"; "Thistle"] |> List.iter (attempt >> printfn "%s")
// invalidArg carries the parameter name with the complaint.
let frames n = if n < 0 then invalidArg "n" "negative frames" else n
printfn "Frames checked: %d" (frames 9)
Cómo funciona
failwithlanza un Failure cargando su string.with | Failure reason ->matchea la captura.invalidArgnombra el parámetro ofensor.
Palabras clave y builtins usados aquí
elseifletthentrywith
El intento, en números
- Líneas
- 16
- Caracteres a escribir
- 511
- Tokens
- 89
- Ritmo de tres estrellas
- 65 tpm
Al ritmo de tres estrellas de 65 tokens por minuto, este intento toma unos 82 segundos.
Paso 1 de 3 en Firmas y pereza; paso 22 de 27 en Fundamentos del lenguaje.