Bird
Raised Fist0
PowerShellscripting~30 mins

Terminating vs non-terminating errors in PowerShell - Hands-On Comparison

Choose your learning style10 modes available
Understanding Terminating vs Non-Terminating Errors in PowerShell
📖 Scenario: You are writing a PowerShell script to process a list of files. Sometimes, errors happen when accessing files. You want to see how PowerShell handles different types of errors: terminating and non-terminating.
🎯 Goal: Learn how to create commands that produce terminating and non-terminating errors, and how to catch and display these errors in PowerShell.
📋 What You'll Learn
Create a list of file paths including one that does not exist
Set a variable to control error action preference
Write commands that cause terminating and non-terminating errors
Display error messages to understand the difference
💡 Why This Matters
🌍 Real World
In real scripts, handling errors properly helps keep automation running smoothly without stopping unexpectedly.
💼 Career
Understanding error types and handling is essential for system administrators and automation engineers to write reliable PowerShell scripts.
Progress0 / 4 steps
1
Create a list of file paths
Create a variable called filePaths that contains these exact strings: 'C:\Windows\notepad.exe', 'C:\InvalidPath\file.txt', and 'C:\Windows\System32\calc.exe'.
PowerShell
Hint

Use an array with @() and include the exact file paths as strings.

2
Set error action preference
Create a variable called errorAction and set it to the string 'Continue' to allow non-terminating errors to show but continue running.
PowerShell
Hint

Assign the string 'Continue' to the variable $errorAction.

3
Try to get file info with non-terminating errors
Use a foreach loop with variable $file to go through $filePaths. Inside the loop, run Get-Item $file -ErrorAction $errorAction to get file info. This will show non-terminating errors for invalid paths.
PowerShell
Hint

Use foreach to loop and Get-Item with the variable $file and -ErrorAction $errorAction.

4
Try to get file info with terminating errors and catch them
Set $errorAction to 'Stop'. Use a foreach loop with $file over $filePaths. Inside the loop, use try and catch blocks. In try, run Get-Item $file -ErrorAction $errorAction. In catch, print "Error caught: $($_.Exception.Message)".
PowerShell
Hint

Use try-catch with Get-Item and -ErrorAction 'Stop'. In catch, print the error message with Write-Output.

Practice

(1/5)
1. What is the main difference between terminating and non-terminating errors in PowerShell?
easy
A. Neither error affects script execution.
B. Terminating errors stop the script execution, non-terminating errors allow it to continue.
C. Both errors stop the script execution immediately.
D. Non-terminating errors stop the script execution, terminating errors allow it to continue.

Solution

  1. Step 1: Understand error types in PowerShell

    Terminating errors cause the script to stop unless handled, while non-terminating errors only show warnings.
  2. Step 2: Compare effects on script execution

    Terminating errors halt the script, non-terminating errors let it continue running.
  3. Final Answer:

    Terminating errors stop the script execution, non-terminating errors allow it to continue. -> Option B
  4. Quick Check:

    Terminating stops, non-terminating continues [OK]
Hint: Terminating errors stop scripts; non-terminating just warn [OK]
Common Mistakes:
  • Confusing which error type stops the script
  • Thinking non-terminating errors halt execution
  • Assuming both errors behave the same
2. Which of the following is the correct way to make a non-terminating error behave like a terminating error in PowerShell?
easy
A. Use the parameter -ErrorAction Stop
B. Use the parameter -ErrorAction Continue
C. Use the parameter -ErrorAction SilentlyContinue
D. Use the parameter -ErrorAction Ignore

Solution

  1. Step 1: Recall the -ErrorAction parameter options

    The -ErrorAction parameter controls how errors behave; Stop makes non-terminating errors act as terminating.
  2. Step 2: Match the correct option

    -ErrorAction Stop forces the error to stop the script, unlike Continue or SilentlyContinue which allow continuation.
  3. Final Answer:

    Use the parameter -ErrorAction Stop -> Option A
  4. Quick Check:

    -ErrorAction Stop makes errors terminate [OK]
Hint: Use -ErrorAction Stop to force termination on errors [OK]
Common Mistakes:
  • Using Continue or SilentlyContinue expecting termination
  • Confusing Ignore with Stop
  • Not knowing -ErrorAction controls error behavior
3. What will be the output of this PowerShell script?
Get-Item 'C:\nonexistentfile.txt' -ErrorAction Continue
Write-Output 'Script continues'
medium
A. No error message, only 'Script continues' is printed
B. Script stops with an error, 'Script continues' is not printed
C. Error message shown, then 'Script continues' is printed
D. Script crashes with no output

Solution

  1. Step 1: Understand -ErrorAction Continue behavior

    With -ErrorAction Continue, non-terminating errors show error messages but do not stop the script.
  2. Step 2: Trace script output

    The error about missing file is displayed, then the script prints 'Script continues'.
  3. Final Answer:

    Error message shown, then 'Script continues' is printed -> Option C
  4. Quick Check:

    Continue shows error but script runs [OK]
Hint: Continue shows error but script keeps running [OK]
Common Mistakes:
  • Assuming script stops on Continue error
  • Expecting no error message with Continue
  • Confusing Continue with Stop
4. You run this script but it continues unexpectedly:
Try {
  Get-Item 'C:\nonexistentfile.txt' -ErrorAction Stop
} Catch {
  Write-Output 'Caught error'
}
Write-Output 'Script continues'

What is the likely cause?
medium
A. The script stops because the Catch block is missing
B. The error is non-terminating, so Try/Catch does not catch it
C. The script stops because -ErrorAction Stop causes termination without Catch
D. The error is terminating and caught, so script continues after Catch

Solution

  1. Step 1: Analyze -ErrorAction Stop with Try/Catch

    Using -ErrorAction Stop makes the error terminating, so Try/Catch can catch it.
  2. Step 2: Understand script flow after Catch

    The Catch block runs and outputs 'Caught error', then script continues to print 'Script continues'.
  3. Final Answer:

    The error is terminating and caught, so script continues after Catch -> Option D
  4. Quick Check:

    Terminating error caught, script continues [OK]
Hint: Try/Catch catches terminating errors with -ErrorAction Stop [OK]
Common Mistakes:
  • Thinking non-terminating errors trigger Catch
  • Assuming script stops without Catch block
  • Confusing error action effects on Try/Catch
5. You want to run a script that tries to get multiple files, but you want it to stop immediately if any file is missing. Which approach correctly enforces this behavior?
hard
A. Use Get-Item with -ErrorAction Stop inside a Try/Catch block to handle errors.
B. Use Get-Item with -ErrorAction Continue and no error handling.
C. Use Get-Item with -ErrorAction SilentlyContinue and log errors later.
D. Use Get-Item without any -ErrorAction parameter.

Solution

  1. Step 1: Identify requirement to stop on missing file

    Stopping immediately on error means using terminating errors with -ErrorAction Stop.
  2. 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.
  3. Final Answer:

    Use Get-Item with -ErrorAction Stop inside a Try/Catch block to handle errors. -> Option A
  4. Quick Check:

    Stop errors + Try/Catch = immediate stop [OK]
Hint: Use -ErrorAction Stop with Try/Catch to halt on errors [OK]
Common Mistakes:
  • Using Continue or SilentlyContinue expecting stop
  • Not using Try/Catch to handle terminating errors
  • Ignoring error handling for critical failures
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard