Created
February 25, 2020 10:52
-
-
Save Szer/cf5caa7e3b2a047c811701561e7026f3 to your computer and use it in GitHub Desktop.
ExceptionHandling
This file contains 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 (|Is|_|) (x: 'b) : 'a option = | |
match box x with | |
| :? 'a as a -> Some a | |
| _ -> None | |
let (|Inner|_|) (b: exn): 'a option when 'a :> Exception = | |
match box b.InnerException with | |
| :? 'a as a -> Some a | |
| _ -> None | |
try | |
() | |
with | |
| :? AggregateException as a when (a.InnerException :? NullReferenceException) -> | |
let b = a.InnerException :?> NullReferenceException | |
printfn "%s" b.Message | |
try | |
let a = new AggregateException(new NullReferenceException("MOCK")) | |
raise a | |
with | |
| Is (a: AggregateException) & Inner (b: NullReferenceException) -> | |
printfn "%s" b.Message // b is NullReferenceException here, no need to cast |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment