try / catch

say it: “TRY or KACH” /traɪ ɔːr kætʃ/

Runs code and handles errors instead of crashing.

What does try / catch do?

try/catch handles errors: code in try runs, and if a terminating error happens, catch runs instead of the script crashing.

try {
    Get-Content .\settings.json -ErrorAction Stop
}
catch {
    Write-Warning "Could not read settings: $($_.Exception.Message)"
}

Level: Journeyman · More PowerShell terms at this level

Updated