Bird
0
0

Which of the following is the correct syntax to use a when clause in a catch block?

easy📝 Syntax Q12 of 15
C Sharp (C#) - Exception Handling

Which of the following is the correct syntax to use a when clause in a catch block?

try {
    // code
} catch (Exception ex) _____ {
    // handle
}
Awhile (ex.Message.Contains("error"))
Bif (ex.Message.Contains("error"))
Cwhere (ex.Message.Contains("error"))
Dwhen (ex.Message.Contains("error"))
Step-by-Step Solution
Solution:
  1. Step 1: Recall the correct keyword for condition in catch

    The correct keyword to add a condition in catch is when.
  2. Step 2: Match the syntax with the options

    Only when (ex.Message.Contains("error")) uses when correctly with the condition.
  3. Final Answer:

    when (ex.Message.Contains("error")) -> Option D
  4. Quick Check:

    Use 'when' keyword for catch condition [OK]
Quick Trick: Use 'when' keyword, not if/where/while in catch [OK]
Common Mistakes:
MISTAKES
  • Using if instead of when in catch
  • Confusing when with where or while
  • Missing parentheses after when

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More C Sharp (C#) Quizzes