When should I use Write-Error vs. Throw? Terminating vs. non-terminating errors

前端 未结 6 1679
别跟我提以往
别跟我提以往 2020-11-28 03:55

Looking at a Get-WebFile script over on PoshCode, http://poshcode.org/3226, I noticed this strange-to-me contraption:

$URL_Format_Error = [string]\"...\"
Wri         


        
6条回答
  •  刺人心
    刺人心 (楼主)
    2020-11-28 04:27

    Write-Error allows the consumer of the function to suppress the error message with -ErrorAction SilentlyContinue (alternatively -ea 0). While throw requires a try{...} catch {..}

    To use a try...catch with Write-Error:

    try {
        SomeFunction -ErrorAction Stop
    }
    catch {
        DoSomething
    }
    

提交回复
热议问题