异常提供了一种将控制权从程序的一部分转移到另一部分的方法,F#异常处理提供以下构造-
构建 | 说明 |
---|---|
raise expr | 引发给定的异常。 |
failwith expr | 引发 System.Exception 异常。 |
try expr with rules | 捕获与模式规则匹配的表达式。 |
try expr finally expr | 执行 finally 表达式。 |
| :? ArgumentException | 与给定的.NET异常类型匹配的规则。 |
| :? ArgumentException as e | 与给定的.NET异常类型匹配的规则,将名称 e 绑定到异常对象值。 |
|Failure(msg)→expr | 与给定的数据承载F#异常匹配的规则。 |
| exn→expr | 与任何异常匹配的规则,将名称 exn 绑定到异常对象值。 |
| exn当expr→expr | 在给定条件下匹配异常的规则,将名称 exn 绑定到异常对象值。 |
让无涯教程从异常处理的基本语法开始。
F#异常处理块的基本语法是-
exception exception-type of argument-type
try ... with 表达式用于F#语言中的异常处理。
try expression1 with | pattern1 -> expression2 | pattern2 -> expression3 ...
通过 try ... finally 表达式,即使代码块引发异常,您也可以执行清理代码。
try expression1 finally expression2
raise 函数用于指示发生了错误或异常情况,它还在异常对象中捕获有关错误的信息。
raise (expression)
failwith 函数生成一个F#异常。
failwith error-message-string
invalidArg 函数生成一个参数异常。
invalidArg parameter-name error-message-string
以下程序通过一个简单的try…块展示了基本的异常处理-
let divisionprog x y = try Some (x/y) with | :? System.DivideByZeroException -> printfn "Division by zero!"; None let result1 = divisionprog 100 0
编译并执行程序时,将产生以下输出-
Division by zero!
F#提供了 exception 类型来声明异常。您可以直接在 try ... with 表达式的过滤器中使用异常类型。
以下示例演示了这一点-
exception Error1 of string //Using a tuple type as the argument type. exception Error2 of string * int let myfunction x y = try if x = y then raise (Error1("Equal Number Error")) else raise (Error2("Error Not detected", 100)) with | Error1(str) -> printfn "Error1 %s" str | Error2(str, i) -> printfn "Error2 %s %d" str i myfunction 20 10 myfunction 5 5
编译并执行程序时,将产生以下输出-
Error2 Error Not detected 100 Error1 Equal Number Error
以下示例演示了嵌套异常处理-
exception InnerError of string exception OuterError of string let func1 x y = try try if x = y then raise (InnerError("inner error")) else raise (OuterError("outer error")) with | InnerError(str) -> printfn "Error:%s" str finally printfn "From the finally block." let func2 x y = try func1 x y with | OuterError(str) -> printfn "Error: %s" str func2 100 150 func2 100 100 func2 100 120
编译并执行程序时,将产生以下输出-
From the finally block. Error: outer error Error:inner error From the finally block. From the finally block. Error: outer error
以下函数演示 failwith 函数-
let divisionFunc x y = if (y = 0) then failwith "Divisor cannot be zero." else x/y let trydivisionFunc x y = try divisionFunc x y with | Failure(msg) -> printfn "%s" msg; 0 let result1 = trydivisionFunc 100 0 let result2 = trydivisionFunc 100 4 printfn "%A" result1 printfn "%A" result2
编译并执行程序时,将产生以下输出-
Divisor cannot be zero. 0 25
invalidArg 函数生成一个参数异常。
let days = [| "Sunday"; "Monday"; "Tuesday"; "Wednesday"; "Thursday"; "Friday"; "Saturday" |] let findDay day = if (day > 7 || day < 1) then invalidArg "day" (sprintf "You have entered %d." day) days.[day - 1] printfn "%s" (findDay 1) printfn "%s" (findDay 5) printfn "%s" (findDay 9)
编译并执行程序时,将产生以下输出-
Sunday Thursday Unhandled Exception: System.ArgumentException: You have entered 9. …
根据系统的不同,还将显示有关文件和导致系统错误的变量的其他信息。
祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)