Bird
0
0

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📝 Application Q15 of 15
PowerShell - Error Handling
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?
Atry { Remove-Item 'C:\file.txt' } catch { Write-Output 'Failed to delete file'; exit }
BRemove-Item 'C:\file.txt' -ErrorAction Stop; Write-Output 'Continuing script'
Ctry { Remove-Item 'C:\file.txt' } catch { Write-Output 'Failed to delete file' } Write-Output 'Continuing script'
DRemove-Item 'C:\file.txt' -ErrorAction SilentlyContinue; Write-Output 'Failed to delete file'
Step-by-Step Solution
Solution:
  1. 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.
  2. Step 2: Confirm script continues

    After catch, it outputs 'Continuing script', so script keeps running.
  3. Final Answer:

    try { Remove-Item 'C:\file.txt' } catch { Write-Output 'Failed to delete file' } Write-Output 'Continuing script' -> Option C
  4. Quick Check:

    Try-catch logs error and continues = C [OK]
Quick Trick: 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

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PowerShell Quizzes