1
爲什麼即使生成錯誤時PowerShell也不會捕獲語句?在這種情況下,我故意刪除數據文件夾以查看catch塊是否被調用。從PowerShell的Powershell - Catch塊即使在發生錯誤時也不會調用
Set-Content : Could not find a part of the path 'D:\Data\CUST.csv'.
At D:\Data\Scripts\CUST_HOUSEKEPPING.ps1:55 char:79
+ $getData2 | Where-Object{[regex]::matches($_,"\,").count -eq 22} | set-content <<<< $outPath
+ CategoryInfo : ObjectNotFound: (D:\Data\CUST.csv:String) [Set-Content], DirectoryNotFo
undException
+ FullyQualifiedErrorId : GetContentWriterDirectoryNotFoundError,Microsoft.PowerShell.Commands.SetContentCommand
'-ErrorAction Stop'是必需的,因爲錯誤是非終端(管道可以繼續工作)。 Try/catch只捕獲通常會阻止腳本/函數繼續執行的終止錯誤。 –