What are try, catch, finally blocks?
π‘ Concept: Try, Catch, Finally Blocks
These blocks allow C# programs to handle exceptions, attempt recovery, and ensure cleanup of resources.
π Quick Intro
The try block encloses code that may throw exceptions, catch blocks handle those exceptions, and finally block runs code regardless of exceptions.
π§ Analogy
Think of try as testing waters, catch as a safety net for errors, and finally as cleaning up after the test, no matter what happens.
π§ Technical Explanation
- πΉ try: Code that might fail is placed here.
- πΉ catch: Handles specific or general exceptions.
- πΉ finally: Runs cleanup code, executes always.
- πΉ Multiple catch blocks allow handling different exceptions.
- πΉ Finally is optional but recommended for resource cleanup.
π― Use Cases
- β Handling file I/O operations safely.
- β Managing network or database errors.
- β Cleaning up resources like streams and connections.
- β Ensuring code runs regardless of exceptions.
π» Code Example
try {
int value = int.Parse(""abc"");
}
catch (FormatException ex) {
Console.WriteLine(""Input format is invalid."");
}
finally {
Console.WriteLine(""Cleanup code runs here."");
}

β Interview Q&A
Q1: What does the try block do?
A: Contains code that may throw exceptions.
Q2: When is finally executed?
A: Always, after try and catch.
Q3: Can you have multiple catch blocks?
A: Yes, to handle different exception types.
Q4: Is finally block mandatory?
A: No, itβs optional.
Q5: Can catch block rethrow exceptions?
A: Yes, using throw.
Q6: What happens if exception is not caught?
A: Program terminates or propagates up the stack.
Q7: Can try be used without catch?
A: No, try must have at least one catch or finally.
Q8: What is the order of execution?
A: try, catch (if exception), then finally.
Q9: Can finally change the exception?
A: No, but it can suppress exceptions.
Q10: Are exceptions performance intensive?
A: Yes, use exceptions judiciously.
π MCQs
Q1. What does the try block do?
- Contains risky code
- Handles exceptions
- Runs always
- Cleans up resources
Q2. When is finally executed?
- Only on success
- Only on failure
- Always after try and catch
- Never
Q3. Can there be multiple catch blocks?
- No
- Yes
- Sometimes
- No idea
Q4. Is finally block mandatory?
- Yes
- No
- Sometimes
- Always
Q5. Can catch rethrow exceptions?
- No
- Yes
- Sometimes
- Never
Q6. What happens if exception not caught?
- Program continues
- Program terminates or propagates
- Program ignores
- Program logs
Q7. Can try be without catch?
- Yes
- No
- Sometimes
- No idea
Q8. Order of execution?
- Catch, try, finally
- Try, finally, catch
- Try, catch, finally
- Finally, catch, try
Q9. Can finally change exception?
- Yes
- No
- Sometimes
- Always
Q10. Are exceptions costly?
- No
- Use judiciously
- Always costly
- Never
π‘ Bonus Insight
Understanding the structure and order of try-catch-finally blocks is essential for writing reliable and maintainable C# code.
π PDF Download
Need a handy summary for your notes? Download this topic as a PDF!