What are try, catch, finally blocks?
๐ก Concept: try, catch, finally blocks
These blocks form the foundation of structured exception handling in C#, helping manage runtime errors gracefully.
๐ Quick Intro
try block contains risky code, catch handles exceptions, and finally executes cleanup code regardless of exceptions.
๐ง Analogy
Think of try as attempting a jump, catch as a safety net that catches you, and finally as the ground you land on.
๐ง Technical Explanation
- โ๏ธ try block: Contains code that may cause exceptions.
- ๐ catch block: Handles exceptions, can specify exception types.
- ๐ finally block: Executes code regardless of exceptions for cleanup.
- ๐งฉ Multiple catch blocks can handle different exception types.
- ๐ก finally is optional but useful for resource management.
๐ฏ Use Cases
- โ Handling file access exceptions.
- โ Managing database transaction errors.
- โ Cleaning up resources like file handles or database connections.
- โ Providing meaningful error messages to users.
๐ป Code Example
try {
int[] arr = {1, 2, 3};
Console.WriteLine(arr[10]);
}
catch (IndexOutOfRangeException ex) {
Console.WriteLine(""Caught an IndexOutOfRangeException: "" + ex.Message);
}
finally {
Console.WriteLine(""This block runs regardless of exceptions."");
}

โ Interview Q&A
Q1: What does the try block do?
A: Contains code that might throw exceptions.
Q2: What is the purpose of catch?
A: To handle exceptions.
Q3: When does finally run?
A: Always after try and catch.
Q4: Can there be multiple catch blocks?
A: Yes, for different exception types.
Q5: Is finally mandatory?
A: No, but recommended for cleanup.
Q6: Can catch block rethrow exceptions?
A: Yes, using throw.
Q7: What happens if no catch matches?
A: The exception propagates up the call stack.
Q8: How to catch all exceptions?
A: Use catch (Exception ex).
Q9: Can finally block modify return values?
A: No, but it runs after return statement.
Q10: How to nest try-catch?
A: Place try-catch inside another try or catch block.
๐ MCQs
Q1. What does try block do?
- Handles exceptions
- Contains risky code
- Cleanup code
- Logs errors
Q2. Purpose of catch?
- Ignore exceptions
- Handle exceptions
- Throw exceptions
- Log errors
Q3. When does finally run?
- Only on exceptions
- Only on success
- Always after try and catch
- Never
Q4. Can multiple catch blocks exist?
- No
- Yes
- Sometimes
- Never
Q5. Is finally mandatory?
- Yes
- No
- Optional
- Always
Q6. Can catch rethrow exceptions?
- No
- Yes
- Sometimes
- Never
Q7. What if no catch matches?
- Caught anyway
- Exception propagates
- Program terminates immediately
- Ignored
Q8. How to catch all exceptions?
- catch {}
- catch (Exception ex)
- catch (object ex)
- catch (Error ex)
Q9. Can finally modify return?
- Yes
- No
- Sometimes
- Always
Q10. How to nest try-catch?
- Not allowed
- Inside another try or catch
- Only inside finally
- Anywhere
๐ก Bonus Insight
Mastering try-catch-finally is essential for robust error handling in real-world C# applications.
๐ PDF Download
Need a handy summary for your notes? Download this topic as a PDF!