相关疑难解决方法(0)

在这种情况下,为什么模式匹配在异常上失败?

我有这个简单的异常层次结构:

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)
Run Code Online (Sandbox Code Playgroud)

和这三个(虚拟)功能:

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))
Run Code Online (Sandbox Code Playgroud)

当你调用ThirdFunction时很容易看到:

  • firstFunction引发FirstLevelException
  • secondFunction捕获它,将其包装到SecondLevelException并抛出它
  • thirdFunction捕获它,将其包装到ThirdLevelException中并抛出它
  • 调用者可以捕获ThirdLevelException.

都好.现在我按以下方式更改thirdFunction:

member this.ThirdFunction a =
    25 |>
    try …
Run Code Online (Sandbox Code Playgroud)

f#

2
推荐指数
1
解决办法
378
查看次数

标签 统计

f# ×1