Bird
0
0

How can you catch only specific exceptions, like DivideByZeroException, in PowerShell's catch block?

hard📝 Application Q9 of 15
PowerShell - Error Handling
How can you catch only specific exceptions, like DivideByZeroException, in PowerShell's catch block?
Acatch [DivideByZeroException] { Write-Output 'Divide by zero error' }
Bcatch { if ($_ -eq 'DivideByZeroException') { Write-Output 'Error' } }
Ccatch DivideByZeroException { Write-Output 'Error' }
Dcatch (DivideByZeroException) { Write-Output 'Error' }
Step-by-Step Solution
Solution:
  1. Step 1: Recall catch syntax for specific exceptions

    PowerShell uses catch [ExceptionType] { } to catch specific errors.
  2. Step 2: Identify correct syntax

    catch [DivideByZeroException] { Write-Output 'Divide by zero error' } correctly uses catch with exception type in square brackets.
  3. Final Answer:

    catch [DivideByZeroException] { Write-Output 'Divide by zero error' } -> Option A
  4. Quick Check:

    Use catch [ExceptionType] for specific errors [OK]
Quick Trick: Use catch [ExceptionType] to filter errors [OK]
Common Mistakes:
  • Using parentheses instead of brackets
  • Trying to filter inside catch block manually
  • Omitting brackets around exception type

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PowerShell Quizzes