Bird
Raised Fist0
PowerShellscripting~20 mins

Try-Catch-Finally in PowerShell - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
PowerShell Try-Catch-Finally Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
What is the output of this PowerShell try-catch-finally script?
Consider this script that tries to divide numbers and handles errors:
PowerShell
try {
  $result = 10 / 0
  Write-Output "Result: $result"
} catch {
  Write-Output "Error caught: $_"
} finally {
  Write-Output "Cleanup done"
}
A
Error caught: Attempted to divide by zero.
Cleanup done
B
Result: 0
Cleanup done
C
Error caught: $_
Cleanup done
D
Result: Infinity
Cleanup done
Attempts:
2 left
💡 Hint
Remember dividing by zero causes an exception caught by catch block.
🧠 Conceptual
intermediate
1:30remaining
Which statement about PowerShell try-catch-finally is true?
Choose the correct statement about how try-catch-finally works in PowerShell.
AThe catch block runs only if an error occurs in try.
BThe finally block runs only if no error occurs in try.
CThe catch block runs even if no error occurs in try.
DThe finally block runs only if an error occurs in try.
Attempts:
2 left
💡 Hint
Think about when catch and finally blocks execute.
🔧 Debug
advanced
2:30remaining
Why does this PowerShell script not catch the error?
Look at this script and find why the catch block never runs:
PowerShell
try {
  Write-Output "Start"
  Get-Item 'C:\nonexistentfile.txt'
  Write-Output "End"
} catch {
  Write-Output "Caught error"
} finally {
  Write-Output "Done"
}
AThe finally block prevents catch from running.
BThe catch block is missing a parameter to catch the error.
CGet-Item does not throw terminating errors by default, so catch is skipped.
DThe script syntax is invalid, so catch never runs.
Attempts:
2 left
💡 Hint
Check the type of error Get-Item throws by default.
🚀 Application
advanced
3:00remaining
What is the output of this script with nested try-catch-finally?
Analyze this nested try-catch-finally script and select the correct output:
PowerShell
try {
  Write-Output "Outer try start"
  try {
    Write-Output "Inner try start"
    throw "Inner error"
    Write-Output "Inner try end"
  } catch {
    Write-Output "Inner catch: $_"
  } finally {
    Write-Output "Inner finally"
  }
  Write-Output "Outer try end"
} catch {
  Write-Output "Outer catch: $_"
} finally {
  Write-Output "Outer finally"
}
A
Outer try start
Inner try start
Inner catch: Inner error
Inner finally
Outer finally
B
Outer try start
Inner try start
Inner finally
Outer catch: Inner error
Outer finally
C
Outer try start
Inner try start
Inner catch: Inner error
Outer catch: Inner error
Inner finally
Outer finally
D
Outer try start
Inner try start
Inner catch: Inner error
Inner finally
Outer try end
Outer finally
Attempts:
2 left
💡 Hint
Remember that the inner catch handles the inner throw, so outer catch is not triggered.
🧠 Conceptual
expert
3:00remaining
What happens if a finally block itself throws an error in PowerShell?
Consider a try-catch-finally where the finally block throws an error. What is the behavior?
AThe script terminates immediately without running catch or finally.
BThe error from finally replaces any previous error and must be handled separately.
CThe catch block catches errors from finally automatically.
DThe error in finally is ignored and does not affect script execution.
Attempts:
2 left
💡 Hint
Think about error precedence when finally throws an error.

Practice

(1/5)
1. What is the main purpose of the try block in PowerShell's Try-Catch-Finally structure?
easy
A. To always run code regardless of errors
B. To handle errors after they occur
C. To run code only if no errors happen
D. To run code that might cause an error

Solution

  1. Step 1: Understand the role of try

    The try block contains code that might cause an error during execution.
  2. Step 2: Differentiate from catch and finally

    catch handles errors after they happen, and finally runs code always, but try is where risky code runs.
  3. Final Answer:

    To run code that might cause an error -> Option D
  4. Quick Check:

    try runs risky code = A [OK]
Hint: Try block runs code that may fail [OK]
Common Mistakes:
  • Confusing try with catch
  • Thinking finally runs only on errors
  • Believing try runs only if no errors
2. Which of the following is the correct syntax to catch an error in PowerShell?
easy
A. catch { Write-Host 'Error occurred' }
B. catch() { Write-Host 'Error occurred' }
C. catch: { Write-Host 'Error occurred' }
D. catch[] { Write-Host 'Error occurred' }

Solution

  1. Step 1: Recall PowerShell catch syntax

    In PowerShell, catch is followed by a script block in curly braces without parentheses or other symbols.
  2. Step 2: Identify correct syntax

    catch { Write-Host 'Error occurred' } uses catch { ... } which is correct; others add invalid symbols like parentheses or colons.
  3. Final Answer:

    catch { Write-Host 'Error occurred' } -> Option A
  4. Quick Check:

    Correct catch syntax uses braces only = A [OK]
Hint: Catch uses braces {} without parentheses [OK]
Common Mistakes:
  • Adding parentheses after catch
  • Using colon or brackets with catch
  • Missing curly braces around catch code
3. What will be the output of this PowerShell script?
try {
  1 / 0
  Write-Output 'Success'
} catch {
  Write-Output 'Error caught'
} finally {
  Write-Output 'Always runs'
}
medium
A. Error caught Always runs
B. Success
C. Success Always runs
D. Error caught

Solution

  1. Step 1: Analyze the try block

    The code attempts to divide 1 by 0, which causes a runtime error, so the line Write-Output 'Success' is skipped.
  2. Step 2: Check catch and finally behavior

    The catch block runs and outputs 'Error caught'. The finally block always runs and outputs 'Always runs'.
  3. Final Answer:

    Error caught Always runs -> Option A
  4. Quick Check:

    Error triggers catch, finally always runs = C [OK]
Hint: Error skips try rest, runs catch then finally [OK]
Common Mistakes:
  • Assuming 'Success' prints despite error
  • Ignoring finally block output
  • Thinking catch runs only sometimes
4. Identify the error in this PowerShell script:
try {
  Write-Output 'Start'
} catch {
  Write-Output 'Error'
} finaly {
  Write-Output 'Done'
}
medium
A. Missing parentheses after catch
B. No error in the script
C. Misspelled 'finally' keyword
D. Missing semicolon after Write-Output

Solution

  1. Step 1: Check spelling of keywords

    The script uses finaly instead of the correct finally keyword.
  2. Step 2: Verify other syntax elements

    Catch does not require parentheses, and semicolons are optional in PowerShell; so these are not errors.
  3. Final Answer:

    Misspelled 'finally' keyword -> Option C
  4. Quick Check:

    Keyword spelling matters: 'finally' correct = B [OK]
Hint: Check spelling of 'finally' carefully [OK]
Common Mistakes:
  • Thinking catch needs parentheses
  • Expecting semicolons after commands
  • Missing typo in finally keyword
5. You want to ensure a log file is always closed after writing, even if an error occurs during writing. Which Try-Catch-Finally structure correctly achieves this?
hard
A. try { Write-Output 'Write log' } catch { Close-LogFile } finally { Write-Output 'Error' }
B. try { Write-Output 'Write log' } catch { Write-Output 'Error' } finally { Close-LogFile }
C. catch { Write-Output 'Error' } try { Write-Output 'Write log' } finally { Close-LogFile }
D. try { Write-Output 'Write log' } finally { Close-LogFile } catch { Write-Output 'Error' }

Solution

  1. Step 1: Understand Try-Catch-Finally order

    The correct order is try, then catch, then finally. The finally block always runs last.
  2. Step 2: Identify correct usage for closing log file

    try { Write-Output 'Write log' } catch { Write-Output 'Error' } finally { Close-LogFile } correctly places Close-LogFile in finally to ensure it runs always, even if an error occurs.
  3. Final Answer:

    try { Write-Output 'Write log' } catch { Write-Output 'Error' } finally { Close-LogFile } -> Option B
  4. Quick Check:

    Try-Catch-Finally order with cleanup in finally = D [OK]
Hint: Put cleanup code in finally block [OK]
Common Mistakes:
  • Placing finally before catch
  • Putting cleanup in catch only
  • Misordering Try-Catch-Finally blocks
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