Param(
[ValidateRange(21,90)]
[int[]]$Age
)
How to catch exception when $age
is out of range?
Param(
[ValidateRange(21,90)]
[int[]]$Age
)
How to catch exception when $age
is out of range?
As has been pointed out in the comments, the validation error needs to be caught in the calling context
try{
.\Set-Age.ps1
}
catch [System.Management.Automation.ParameterBindingException] {
Write-Host "Error thrown while attempting to bind an argument to parameter $($_.Exception.ParameterName), with message: $($_.Exeption.Message)"
}
catch {
# something other than the parameter binder threw an exception
}