Error Handling: try/catch and ErrorAction
By the end of this lesson you'll know the crucial difference between terminating and non-terminating errors, wrap risky code in try/catch/finally, promote errors with -ErrorAction Stop, raise your own with throw, and inspect what went wrong with $Error and -ErrorVariable — so your scripts fail gracefully instead of crashing.
Learn Error Handling: try/catch and ErrorAction in our free PowerShell course — an interactive lesson with worked examples, a practice exercise and a quick…
Part of the free Powershell course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.
Think of error handling like a circuit breaker in your house. A non-terminating error is a flickering light — annoying, but the power stays on and everything keeps running. A terminating error is a real fault that trips the breaker and stops the circuit. try is the wiring you're testing; catch is the breaker that trips safely instead of starting a fire; and finally is the inspector who always shows up afterward to tidy up, fault or no fault. -ErrorAction Stop is you deciding that a particular flicker is serious enough to trip the breaker on purpose .
1. Terminating vs Non-Terminating Errors
This distinction is the key to everything. A non-terminating error reports a problem but lets the command (and pipeline) keep going — this is the default for most cmdlets. A terminating error stops the current command dead. The catch is that try/catch only reacts to terminating errors. To make a non-terminating error catchable, promote it with -ErrorAction Stop . Read this worked example, then run it.
2. try / catch / finally
The structure is simple: try {' '} holds the risky code, catch {' '} runs only if a terminating error occurs, and finally {' '} always runs afterward for cleanup. Inside catch , the automatic variable $_ is the ErrorRecord ; $_.Exception.Message gives you the human-readable text.
Now you try. Fill in the three blanks using the hints in the comments, then run it.
3. -ErrorAction & $ErrorActionPreference
Every cmdlet accepts the common parameter -ErrorAction , which controls how that one command reacts to an error. Stop makes it terminating (catchable), SilentlyContinue suppresses it entirely, and Continue (the default) shows it but carries on. To change the default for an entire script or session, set the preference variable $ErrorActionPreference .
Prefer a per-command -ErrorAction Stop over flipping $ErrorActionPreference globally. The per-command form affects only the line you intend, so you won't accidentally make an unrelated command terminate. Reach for the global preference only when you genuinely want strict behaviour across a whole script.
Now combine the rest of the toolkit: throw raises your own terminating error, a typed catch [Type] handles one specific exception, -ErrorVariable captures a command's errors into your own variable, and $Error is the session-wide history with $Error[0] as the newest.
To find an exception's type for a typed catch, run the code once and inspect $Error[0].Exception.GetType().FullName — then paste that type into your catch [Type] .
No blanks this time — just a brief and an outline to keep you on track. Build it, run it, and check your output against the example in the comments. Writing functions that fail gracefully is exactly what production scripts demand.
Practice quiz
Which type of error can be caught by a try/catch block by default?
- A terminating error
- Any warning
- A verbose message
- A non-terminating error
Answer: A terminating error. Only terminating errors trigger catch. Non-terminating errors must be promoted with -ErrorAction Stop first.
What does -ErrorAction Stop do to a non-terminating error?
- Hides it completely
- Logs it to a file
- Turns it into a terminating error
- Retries the command
Answer: Turns it into a terminating error. -ErrorAction Stop promotes a non-terminating error into a terminating one so try/catch can handle it.
Which block always runs whether or not an error occurred?
- ensure
- finally
- catch
- trap
Answer: finally. The finally block runs after try (and any catch), making it ideal for cleanup like closing files.
What does -ErrorAction SilentlyContinue do?
- Stops the script
- Prompts the user
- Throws immediately
- Suppresses the error and continues
Answer: Suppresses the error and continues. SilentlyContinue suppresses the error display and lets the command continue without halting.
Which automatic variable holds the collection of recent errors?
- $Error
- $LastError
- $ErrorList
- $Exception
Answer: $Error. $Error is an array of the most recent errors, with $Error[0] being the newest.
How do you raise your own terminating error in PowerShell?
- stop 'message'
- throw 'message'
- raise 'message'
- error 'message'
Answer: throw 'message'. The throw keyword raises a terminating error that can be caught by an enclosing try/catch.
Which preference variable sets the default error behaviour for the session?
- $ErrorMode
- $OnError
- $DefaultError
- $ErrorActionPreference
Answer: $ErrorActionPreference. $ErrorActionPreference controls the default action (Continue, Stop, SilentlyContinue) for the whole session.
Inside a catch block, which variable holds the current error?
- $Error
- $Caught
- $_
- $Result
Answer: $_. Inside catch, $_ (the current object) is the ErrorRecord that was thrown.
How can you catch only a specific exception type?
- catch -Type
You write the .NET type in square brackets after catch to handle only that exception type.
What does the -ErrorVariable parameter do?
- Renames $Error
- Stores the command's errors in a named variable
- Sets the exit code
- Clears all errors
Answer: Stores the command's errors in a named variable. -ErrorVariable myErr captures that command's errors into $myErr without affecting $Error handling.