Exceptions: The failwith Function
The failwith
function generates an F# exception.
Syntax
failwith error-message-string
Remarks
The error-message-string in the previous syntax is a literal string or a value of type string
. It becomes the Message
property of the exception.
The exception that is generated by failwith
is a System.Exception
exception, which is a reference that has the name Failure
in F# code. The following code illustrates the use of failwith
to throw an exception.
let divideFailwith x y =
if (y = 0) then failwith "Divisor cannot be zero."
else
x / y
let testDivideFailwith x y =
try
divideFailwith x y
with
| Failure(msg) -> printfn "%s" msg; 0
let result1 = testDivideFailwith 100 0
See also
Spolupráca s nami v službe GitHub
Zdroj tohto obsahu nájdete v službe GitHub, kde môžete vytvárať a skúmať problémy a žiadosti o prijatie zmien. Ďalšie informácie nájdete v našom sprievodcovi prispievateľom.
Pripomienky
https://aka.ms/ContentUserFeedback.
Pripravujeme: V priebehu roka 2024 postupne zrušíme službu Problémy v službe GitHub ako mechanizmus pripomienok týkajúcich sa obsahu a nahradíme ju novým systémom pripomienok. Ďalšie informácie nájdete na stránke:Odoslať a zobraziť pripomienky pre