Bird
Raised Fist0

Which of the following is the correct syntax to catch an exception in C#?

easy📝 Syntax Q12 of Q15
C Sharp (C#) - Exception Handling
Which of the following is the correct syntax to catch an exception in C#?
Atry { /* code */ } catch (ex Exception) { /* handle */ }
Btry { /* code */ } catch Exception ex { /* handle */ }
Ctry { /* code */ } catch { Exception ex } { /* handle */ }
Dtry { /* code */ } catch (Exception ex) { /* handle */ }
Step-by-Step Solution
Solution:
  1. Step 1: Recall correct catch syntax

    The catch block must have parentheses with exception type and variable: catch (Exception ex).
  2. Step 2: Identify correct option

    Only try { /* code */ } catch (Exception ex) { /* handle */ } uses the correct syntax with parentheses and exception variable.
  3. Final Answer:

    try { /* code */ } catch (Exception ex) { /* handle */ } -> Option D
  4. Quick Check:

    Correct catch syntax = catch (Exception ex) [OK]
Quick Trick: Catch needs parentheses with exception type and variable [OK]
Common Mistakes:
MISTAKES
  • Omitting parentheses around exception
  • Swapping exception type and variable order
  • Using braces instead of parentheses

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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