Created
September 29, 2011 00:03
-
-
Save theburningmonk/1249638 to your computer and use it in GitHub Desktop.
Contrived F# example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| open System | |
| let throwException = | |
| function | |
| | 0 -> failwithf "Default exception" | |
| | 1 -> raise <| new ArgumentException("Invalid N") | |
| | 2 -> raise <| new ArgumentNullException() | |
| | 3 | 4 | 5 -> raise <| new ArgumentOutOfRangeException() | |
| | _ -> raise <| new ApplicationException() | |
| let getExitCode = | |
| try | |
| let random = new Random(DateTime.Now.Millisecond) | |
| throwException <| random.Next 8 | |
| 0 | |
| with | |
| // combine patterns | |
| | :? ArgumentNullException | |
| | :? ArgumentOutOfRangeException | |
| -> printfn "Argument was either null or out of range" | |
| 1 | |
| | :? ArgumentException as argEx | |
| -> printfn "Argument exception was caught: %s" argEx.Message | |
| 2 | |
| | _ as ex // wildcard match, 'ex' will be of type System.Exception | |
| -> printfn "Generic exception was caught: %s" ex.Message | |
| 3 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
the wildcard isn't even required.
| ex -> printfn "Generic exception was caught: %s" ex.Message