0

I'm trying to capture the exception thrown while installing gems, I see the error on the command window however I'm not able to capture the error in catch block, I would like to capture and log in logs file, below is the code.

try{

    $tiny= .\gem install --local c:\mypath\tiny_tds-2.1.5-x64-mingw321.gem
    
    if($tiny -eq $null){
        throw
    }
    Write-Host "Installation completed  :" $tiny
}
catch{
$string_err = $_ | Out-String
Write-Host "*****************************"
Write-Host $string_err
Write-Host "*****************************"
}

Error that I get on powershell command window

enter image description here

3
  • 3
    Try adding $ErrorActionPreference = 'Stop' at the top of your script. Assuming that works, then if($tiny -eq $null){...} is not needed at all Commented Feb 22, 2022 at 3:24
  • 1
    :) basically the exception you were seeing was a non-terminating one, hence why the try was not able to catch it. By changing your error preference to Stop, all errors will be treated as terminating. Commented Feb 22, 2022 at 3:35
  • 2
    Does this answer your question? PowerShell: try-catch not working Commented Feb 22, 2022 at 7:06

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.