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

Mysterious288
  • 365
  • 5
  • 24
  • 3
    Try adding `$ErrorActionPreference = 'Stop'` at the top of your script. Assuming that works, then `if($tiny -eq $null){...}` is not needed at all – Santiago Squarzon Feb 22 '22 at 03: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. – Santiago Squarzon Feb 22 '22 at 03:35
  • 2
    Does this answer your question? [PowerShell: try-catch not working](https://stackoverflow.com/questions/33456028/powershell-try-catch-not-working) – stackprotector Feb 22 '22 at 07:06

0 Answers0