如何在提供有效输入之前运行“try”阻止

时间:2016-10-16 01:46:30

标签: powershell error-handling try-catch user-input

首先,我不明白为什么catch块之后的代码会在-ErrorAction设置为Stop的情况下被执行。最初的问题与我打算在用户无效时再次询问用户的意图有关。我想将代码执行回到try块的catch块。感谢您的想法和帮助。

这是我的代码:

try
{
    [ValidateRange(5,9)] #Enter some other number to trigger an error.
    [System.Byte]$cInput=Read-Host "Enter a number from 5 to 9" -ErrorAction Stop
}
catch
{
    Write-Verbose "Invalid input. Try again." #I want to go back to the "try" block here.
}
Write-Output "Thank you." #Why is this still executed in case of error?

谢谢

编辑:您需要以下内容才能看到详细消息

$VerbosePreference="Continue"

0 个答案:

没有答案