Bird
0
0

Which of the following shows the correct way to write a catch block with a when clause in C#?

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

Which of the following shows the correct way to write a catch block with a when clause in C#?

Acatch (Exception ex) where (ex.Message == "Error") { /* handle */ }
Bcatch (Exception ex) when (ex.Message == "Error") { /* handle */ }
Ccatch (Exception ex) if (ex.Message == "Error") { /* handle */ }
Dcatch (Exception ex) filter (ex.Message == "Error") { /* handle */ }
Step-by-Step Solution
Solution:
  1. Step 1: Recall correct syntax

    The when keyword is used with parentheses for the condition.
  2. Step 2: Evaluate options

    catch (Exception ex) when (ex.Message == "Error") { /* handle */ } uses correct syntax: catch (Exception ex) when (condition). Options B, C, and D use invalid keywords.
  3. Final Answer:

    catch (Exception ex) when (ex.Message == "Error") { /* handle */ } -> Option B
  4. Quick Check:

    Only when is valid for conditional catch filters [OK]
Quick Trick: Use 'when' keyword with parentheses in catch [OK]
Common Mistakes:
MISTAKES
  • Using 'if' or 'where' instead of 'when'
  • Omitting parentheses around the condition
  • Using unsupported keywords like 'filter'

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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