Here is a simple example:
try {
sub.ps1
}
catch {
Write-Warning "Caught: $_"
}
finally {
Write-Host "Done"
}
Use help about_Try_Catch_Finallyfor more information. Another way is to use trap, see help about_trap. If you have a C # or C ++ background, I would recommend using the Try_Catch_Finally method (but it also depends on what exactly you are doing).
source
share