What is exception handling in C#?
๐ก Concept: Exception Handling in C#
Exception handling is the process of responding to runtime errors or exceptional conditions gracefully to maintain program stability.
๐ Quick Intro
C# provides structured exception handling using try, catch, and finally blocks to catch and manage exceptions.
๐ง Analogy
Think of exception handling like a safety net under a tightrope walker, catching and managing errors to prevent a crash.
๐ง Technical Explanation
- โ๏ธ try block contains code that might throw exceptions.
- ๐ catch block handles specific or general exceptions.
- ๐ finally block executes code regardless of exceptions (e.g., cleanup).
- ๐ Exceptions are objects derived from System.Exception.
- ๐ Proper handling prevents app crashes and ensures graceful recovery.
๐ฏ Use Cases
- โ Handling file I/O errors.
- โ Managing database exceptions.
- โ Validating user input.
- โ Ensuring resources are released properly.
๐ป Code Example
try {
int[] numbers = {1, 2, 3};
Console.WriteLine(numbers[5]);
}
catch (IndexOutOfRangeException ex) {
Console.WriteLine(""Index out of range error: "" + ex.Message);
}
finally {
Console.WriteLine(""This block always runs."");
}

โ Interview Q&A
Q1: What is exception handling?
A: Managing runtime errors to keep programs stable.
Q2: What is the purpose of finally?
A: To execute code regardless of exceptions.
Q3: Can you catch multiple exceptions?
A: Yes, with multiple catch blocks.
Q4: What is a try block?
A: Code that might throw exceptions.
Q5: What base class do exceptions derive from?
A: System.Exception.
Q6: Is exception handling mandatory?
A: No, but recommended for robustness.
Q7: What is the difference between throw and throw ex?
A: throw preserves stack trace; throw ex resets it.
Q8: How to create custom exceptions?
A: Inherit from Exception class.
Q9: What is exception filtering?
A: Catching exceptions conditionally.
Q10: How to ensure resources are disposed?
A: Use finally or using blocks.
๐ MCQs
Q1. What is exception handling?
- Ignoring errors
- Managing runtime errors
- Causing errors
- Logging only
Q2. Purpose of finally block?
- Executes on error only
- Never executes
- Always executes code
- Executes before try
Q3. Can multiple exceptions be caught?
- No
- Yes
- Sometimes
- Never
Q4. What does try block contain?
- Code that handles exceptions
- Code that may throw exceptions
- Cleanup code
- Logging code
Q5. Base class for exceptions?
- Object
- System.Error
- System.Exception
- System.Class
Q6. Is exception handling mandatory?
- Yes
- No
- Optional
- Always required
Q7. Difference between throw and throw ex?
- Preserves stack trace
- Resets stack trace
- Both same
- None
Q8. How to create custom exceptions?
- Use existing exceptions
- Inherit Exception
- Override base
- Use interface
Q9. What is exception filtering?
- Always catch
- Conditional catch
- Never catch
- Log only
Q10. How to ensure disposal?
- try block
- finally or using
- catch block
- None
๐ก Bonus Insight
Proper exception handling improves application robustness and user experience by gracefully managing errors.
๐ PDF Download
Need a handy summary for your notes? Download this topic as a PDF!