【发布时间】:2017-05-01 23:40:15
【问题描述】:
我正在尝试捕获异常调用运行另一个函数的函数,如下所示:
$ErrorActionPreference = "Stop"
function f {
$a = 1
$b = $a / 0
}
function Main($f) {
try {
$f
} catch [System.Exception] {
"Caught exception"
}
}
Main(f)
问题是异常没有被捕获并且powershell显示如下消息:
Attempted to divide by zero.
In C:\test.ps1:4 car:5
+ $b = $a / 0
+ ~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : RuntimeException
为什么即使$ErrorActionPreference = "Stop"在代码顶部也没有捕获到异常?
【问题讨论】:
-
您的示例中没有高阶函数
标签: powershell exception try-catch