引发异常

This commit is contained in:
Sanchime 2022-03-05 22:15:30 +08:00
parent ee20268ce2
commit 38c49a703f
2 changed files with 44 additions and 0 deletions

View File

@ -18,5 +18,6 @@
<Compile Include="TypeCreating.fs" />
<Compile Include="PatternMatching.fs" />
<Compile Include="ComputationExpressions.fs" />
<Compile Include="ExceptionThrowing.fs"/>
</ItemGroup>
</Project>

View File

@ -0,0 +1,43 @@
module ExceptionThrowing
open System
//
//
exception ErrorMessage of string
// 使exceptionSystem.Exception
try // 使try
Some (10 / 0)
with // 使用 with,
| :? DivideByZeroException -> printfn "除数不能为0"; None
|> ignore // 忽略值, try with
// .NET,F#,
let div x y =
try
if y = 0 then
raise (ErrorMessage ("除数不能为0")) // 使raise,C#throw
else
raise (ErrorMessage (""))
with
| ErrorMessage (e) -> printfn "%s" e
div 10 0
try
try // 使try
Some (10 / 0)
with // 使用 with
| :? DivideByZeroException -> printfn "除数不能为0"; None
|> ignore
finally
printfn ""
// ,finally
// try with与try finally
// withfinally,使
// reraise
// failwithF#,System.Exception