繁体   English   中英

PowerShell 在 Param() 块中捕获异常

[英]PowerShell catch exception in Param() block

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

$age超出范围时如何捕获异常?

正如评论中所指出的,需要在调用上下文中捕获验证错误

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
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM