A. The -ErrorAction SilentlyContinue prevents catch from running
B. The catch block is missing
C. Get-Item syntax is wrong
D. Write-Output cannot be used inside catch
Solution
Step 1: Understand -ErrorAction SilentlyContinue
This option stops errors from being thrown, so catch never triggers.
Step 2: Effect on error handling
Since no error is thrown, catch block is skipped, but script does not crash.
Final Answer:
The -ErrorAction SilentlyContinue prevents catch from running -> Option A
Quick Check:
-ErrorAction SilentlyContinue blocks catch = D [OK]
Hint: SilentlyContinue stops catch from catching errors [OK]
Common Mistakes:
Thinking catch block is missing
Assuming Get-Item syntax error
Believing Write-Output can't be in catch
5. You want your PowerShell script to try deleting a file, but if it fails, log the error and continue. Which script correctly uses error handling to do this?
hard
A. try { Remove-Item 'C:\file.txt' } catch { Write-Output 'Failed to delete file'; exit }
B. Remove-Item 'C:\file.txt' -ErrorAction Stop; Write-Output 'Continuing script'
C. try { Remove-Item 'C:\file.txt' } catch { Write-Output 'Failed to delete file' } Write-Output 'Continuing script'
D. Remove-Item 'C:\file.txt' -ErrorAction SilentlyContinue; Write-Output 'Failed to delete file'
Solution
Step 1: Check try-catch usage
try { Remove-Item 'C:\file.txt' } catch { Write-Output 'Failed to delete file' } Write-Output 'Continuing script' uses try to attempt deletion and catch to log failure without stopping.
Step 2: Confirm script continues
After catch, it outputs 'Continuing script', so script keeps running.
Final Answer:
try { Remove-Item 'C:\file.txt' } catch { Write-Output 'Failed to delete file' } Write-Output 'Continuing script' -> Option C
Quick Check:
Try-catch logs error and continues = C [OK]
Hint: Use try-catch and continue script after catch [OK]
Common Mistakes:
Using exit in catch stops script
Not using try-catch for error handling
Using SilentlyContinue but logging error anyway
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.