I have this simple exception hierarchy:
type FirstLevelException(msg) = inherit System.Exception (msg) type SecondLevelException(msg, inner) = inherit System.Exception (msg, inner) type ThirdLevelException(msg, inner) = inherit System.Exception (msg, inner)
and these three (dummy) functions:
member this.FirstFunction a = raise (new FirstLevelException("one")) member this.SecondFunction a = try this.FirstFunction a with | :? FirstLevelException as ex -> raise (new SecondLevelException("two", ex)) member this.ThirdFunction a = try this.SecondFunction 25 with | :? SecondLevelException as ex -> raise (new ThirdLevelException("three", ex))
It is easy to see that when you call ThirdFunction:
- firstFunction throws FirstLevelException
- secondFunction catches it, wraps it in a SecondLevelException, and throws it
- thirdFunction catches it, wraps it in a ThirdLevelException and throws it
- the caller may catch a ThirdLevelException.
All is well. Now I am changing thirdFunction as follows:
member this.ThirdFunction a = 25 |> try this.SecondFunction with | :? SecondLevelException as ex -> raise (new ThirdLevelException("three", ex))
things get weird: it looks like pattern matching in ThirdFunction is no longer working, and the SecondLevelException is thrown right up to the caller of ThirdFunction without being wrapped in a ThirdLevelException.
I am sure there is a logical explanation that my C # -deformed mind does not see. Can someone shed some light?
source share