catch error from param in Powershell without using try-catch

Viewed 43

I’m writing a Powershell script to call a file convert function(to execute ANYTRAN file). I was told to put param() in a try-catch by my boss but that seems to cause an error. Then how can I catch the error from param()? I think it’s possible to use if statement in a parent shell. Please give me some advice.

Below is the code.

$ErrorActionPreference = "Stop"
try{
    #-----------------------------------------------------------
    # 初期処理
    #-----------------------------------------------------------
    #---# 環境変数定義(define common env)
    #---& ".\commonEnv.ps1"
    # 共通関数インクルード(include common func)
    . (Resolve-Path ".\commonFunc.ps1").path
    # 引数取得(get parameter)
    Param(
        $inFile
       ,$outFile
       ,$flgZeroByte
    )
1 Answers

Your issue is not in params passing . "param", unless the syntax is incorrect, logically can't crash (and you can't handle whether it could, unless with error handling at scope higher than the definition of function) .

function sc1 {
param ( $v_f = '' )
    process {
        Write-Host $v_f
    }
}
sc1 '& 2'
Related