Terminating vs non-terminating errors in PowerShell - Performance Comparison
When running PowerShell scripts, errors can stop the script or let it continue. We want to understand how error handling affects the script's running steps.
How does the script's work change when errors stop it versus when it keeps going?
Analyze the time complexity of the following code snippet.
foreach ($file in Get-ChildItem -Path C:\Logs) {
try {
Get-Content $file.FullName -ErrorAction Stop
} catch {
Write-Host "Error reading $($file.Name)"
}
}
This script reads each file in a folder. If reading a file fails, it shows an error but continues to the next file.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping over each file and reading its content.
- How many times: Once per file in the folder.
Explain the growth pattern intuitively.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 file reads |
| 100 | About 100 file reads |
| 1000 | About 1000 file reads |
Pattern observation: The number of file reads grows directly with the number of files.
Time Complexity: O(n)
This means the script's work grows in a straight line with the number of files it processes.
[X] Wrong: "If an error happens, the script stops immediately, so fewer operations run."
[OK] Correct: In this script, errors are caught and handled, so the loop continues. The script still tries to read every file, keeping the operation count tied to the number of files.
Understanding how error handling affects script flow helps you explain how your scripts behave with different inputs. This skill shows you think about real script performance and reliability.
What if the script used -ErrorAction SilentlyContinue instead of -ErrorAction Stop? How would the time complexity change?
Practice
Solution
Step 1: Understand error types in PowerShell
Terminating errors cause the script to stop unless handled, while non-terminating errors only show warnings.Step 2: Compare effects on script execution
Terminating errors halt the script, non-terminating errors let it continue running.Final Answer:
Terminating errors stop the script execution, non-terminating errors allow it to continue. -> Option BQuick Check:
Terminating stops, non-terminating continues [OK]
- Confusing which error type stops the script
- Thinking non-terminating errors halt execution
- Assuming both errors behave the same
Solution
Step 1: Recall the -ErrorAction parameter options
The-ErrorActionparameter controls how errors behave;Stopmakes non-terminating errors act as terminating.Step 2: Match the correct option
-ErrorAction Stopforces the error to stop the script, unlike Continue or SilentlyContinue which allow continuation.Final Answer:
Use the parameter-ErrorAction Stop-> Option AQuick Check:
-ErrorAction Stopmakes errors terminate [OK]
- Using Continue or SilentlyContinue expecting termination
- Confusing Ignore with Stop
- Not knowing -ErrorAction controls error behavior
Get-Item 'C:\nonexistentfile.txt' -ErrorAction Continue Write-Output 'Script continues'
Solution
Step 1: Understand -ErrorAction Continue behavior
With-ErrorAction Continue, non-terminating errors show error messages but do not stop the script.Step 2: Trace script output
The error about missing file is displayed, then the script prints 'Script continues'.Final Answer:
Error message shown, then 'Script continues' is printed -> Option CQuick Check:
Continue shows error but script runs [OK]
- Assuming script stops on Continue error
- Expecting no error message with Continue
- Confusing Continue with Stop
Try {
Get-Item 'C:\nonexistentfile.txt' -ErrorAction Stop
} Catch {
Write-Output 'Caught error'
}
Write-Output 'Script continues'What is the likely cause?
Solution
Step 1: Analyze -ErrorAction Stop with Try/Catch
Using-ErrorAction Stopmakes the error terminating, so Try/Catch can catch it.Step 2: Understand script flow after Catch
The Catch block runs and outputs 'Caught error', then script continues to print 'Script continues'.Final Answer:
The error is terminating and caught, so script continues after Catch -> Option DQuick Check:
Terminating error caught, script continues [OK]
- Thinking non-terminating errors trigger Catch
- Assuming script stops without Catch block
- Confusing error action effects on Try/Catch
Solution
Step 1: Identify requirement to stop on missing file
Stopping immediately on error means using terminating errors with-ErrorAction Stop.Step 2: Use Try/Catch to handle and control flow
Wrapping calls in Try/Catch lets you catch the error and stop or handle it as needed.Final Answer:
UseGet-Itemwith-ErrorAction Stopinside a Try/Catch block to handle errors. -> Option AQuick Check:
Stop errors + Try/Catch = immediate stop [OK]
- Using Continue or SilentlyContinue expecting stop
- Not using Try/Catch to handle terminating errors
- Ignoring error handling for critical failures
Ready to prove it?
Pass the test and this pattern moves unknown
