What am I doing wrong in try{}catch{} statement?


I can not figure out why Exception on connection never being caught in “catch” statement. See below.

Read “The Big Book of PowerShell Error Handling.” You’re not specifying an -ErrorAction, so there’s no exception to be caught.

I thought by default all errors will be caught in try{} catch{} statement unless you choose to alter default behavior.

Well, no. Not exactly. This is all in the free book, but briefly, a command can generate terminating exceptions or non-terminating errors. You can’t catch non-terminating. It’s up to the command author which is produced by default, but the general rule is non-terminating for most operations.