【发布时间】:2019-07-04 09:32:02
【问题描述】:
我想为脚本中的每个命令处理异常。为此,我正在为try..catch 编写一个函数。该函数有两个参数:$command,要执行的命令,和$errorType,catch 块中指定的可选错误类型。
function tryCatch ($command, $errorType) {
try {
$command
} catch [$errorType] {
# function to be called if this error type occurs
catchError
}
}
但是我不知道如何将错误类型作为变量传递给 catch 块。我收到此错误:
在 \script.ps1:233 字符:25 + 尝试 {$command} 捕获 [$errorType] {catchError} + ~ '[' 后缺少类型名称。我试图绕过它,但似乎没有任何效果。有没有办法做到这一点?
【问题讨论】:
标签: powershell error-handling exception-handling try-catch