函式 failwith 會產生 F# 例外狀況。
語法
failwith error-message-string
備註
上一個語法中的 error-message-string 是常值字串或 類型的 string值。 它會成為 Message 例外狀況的屬性。
所產生的 failwith 例外狀況是 System.Exception 例外狀況,這是在 F# 程式代碼中具有名稱 Failure 的參考。 下列程式代碼說明 failwith 如何使用 擲回例外狀況。
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