【发布时间】:2018-01-27 11:51:07
【问题描述】:
问题/详情
我正在使用 PowerShell 并试图弄清楚自定义 Try Catch 语句是如何工作的。我当前的主要问题涉及混合 Try/Catch 和 If 语句。所以我想要实现的想法是这样的:
try {
if (!$someVariable.Text) { throw new exception 0 }
elseif ($someVariable.Text -lt 11) { throw new exception 1 }
elseif (!($someVariable.Text -match '[a-zA-Z\s]')) { throw new exception 2}
}
catch 0 {
[System.Windows.Forms.MessageBox]::Show("Custom Error Message 1")
}
catch 1 {
[System.Windows.Forms.MessageBox]::Show("Custom Error Message 2")
}
catch 2 {
[System.Windows.Forms.MessageBox]::Show("Custom Error Message 3")
}
现在我知道上面的代码在实际代码方面非常不准确,但我想直观地展示我的想法和尝试实现的目标。
问题
有谁知道如何使用 PowerShell 创建自定义错误消息,这可以帮助我实现接近上述想法的目标并稍微解释一下您的答案?提前谢谢你
到目前为止,下面的链接是我发现的最接近我想要实现的目标:
【问题讨论】:
标签: powershell if-statement error-handling try-catch