首先,我不明白为什么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"