Bird
0
0

You have a custom exception class FileFormatException that needs to include an error code integer along with the message. How should you implement this?

hard🚀 Application Q9 of 15
C Sharp (C#) - Exception Handling
You have a custom exception class FileFormatException that needs to include an error code integer along with the message. How should you implement this?
AStore error code in the Message string
BAdd an int property for error code and initialize it via constructor
CUse a static variable for error code
DOverride ToString to return error code only
Step-by-Step Solution
Solution:
  1. Step 1: Add property for error code

    Custom exceptions can have additional properties like an int ErrorCode to hold extra info.
  2. Step 2: Initialize property in constructor

    The constructor should accept error code and message, passing message to base and setting the property.
  3. Final Answer:

    Add an int property for error code and initialize it via constructor -> Option B
  4. Quick Check:

    Use properties and constructor for extra exception data [OK]
Quick Trick: Use properties and constructor parameters for extra exception info [OK]
Common Mistakes:
MISTAKES
  • Putting error code inside message string
  • Using static variables for instance data
  • Overriding ToString without storing data

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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