1
Param(
    [ValidateRange(21,90)]
    [int[]]$Age
)

How to catch exception when $age is out of range?

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
  • 1
    Exceptions from parameter validation must be caught by the caller. It can't be caught in the `Param()` block. That would defeat the purpose of parameter validation. – Ansgar Wiechers Sep 27 '19 at 11:57
  • so what is the main purpose of [ValidateRange] if i cant catch an exception? – Артем Черемісін Sep 27 '19 at 12:15
  • Save exceptions for [exceptional situations](https://stackoverflow.com/q/180937/503046). It's generally expected that users will provide inappropriate parameter values.`[citation needed`] – vonPryz Sep 27 '19 at 12:42
  • The purpose is to ensure that the script or function is only run if the parameter received valid values and is not run otherwise. If you want parameters to accept anything and deal with whatever value you're getting yourself: don't use parameter validation. Do your own type checking in the function/script body (for instance in a `Begin {}` block). – Ansgar Wiechers Sep 27 '19 at 12:56
  • Besides, I didn't say you can't catch the exception. I said you can't catch it in the `Param()` block. – Ansgar Wiechers Sep 27 '19 at 12:56
  • @AnsgarWiechers - how does one catch the error from the calling context? i added `[CmdletBinding()]` & have tired `-ErrorAction`, `-ErrorVariable`, and redirecting the error stream ... they all fail to stop the display of that error. it DOES end up in the `$Error` automatic variable ... but i see no way to stop the _display_ of the red error text. – Lee_Dailey Sep 27 '19 at 13:01
  • @Lee_Dailey That depends on what the calling context actually is. Running a PowerShell script from a non-PowerShell environment requires different handling than calling a function from within a PowerShell script. – Ansgar Wiechers Sep 27 '19 at 13:04
  • @AnsgarWiechers - i took the OPs code, made a function of it, added `[ComdletBinding()]` to it and one line of output using `$Age`. i cannot find any way to prevent a red error text block from showing up in the ISE or in the powershell console when i use this call >>> `Test-RangeValidation -Age 1` <<< it seems like there SHOULD be a way to capture that so it does not interrupt the output AND so it allows one to gracefully handle it. **_i am lost for ideas ... the OPs question seems quite on-point to me._** – Lee_Dailey Sep 27 '19 at 13:15
  • This block should accept parameters while script starts up , i'm not using this block for functions – Артем Черемісін Sep 27 '19 at 13:27
  • Then the error handling depends on how you invoke the script. From within PowerShell: use `try { & 'C:\path\to\your.ps1' -Age 4 } catch {...}` when calling the script. From outside PowerShell: redirect the error output stream and evaluate the exit code. – Ansgar Wiechers Sep 27 '19 at 13:50

1 Answers1

0

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
}
Mathias R. Jessen
  • 157,619
  • 12
  • 148
  • 206