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!

πŸ” Navigation

Learn More About C# πŸ“š

1. What is C#? πŸ‘‰ Explained
2. Main Features of C# πŸ‘‰ Explained
3. Difference Between C# and Java πŸ‘‰ Explained
4. Common Language Runtime (CLR) in C# πŸ‘‰ Explained
5. Common Type System (CTS) in C# πŸ‘‰ Explained
6. Common Language Specification (CLS) in C# πŸ‘‰ Explained
7. Value Types vs Reference Types in C# πŸ‘‰ Explained
8. What is a Namespace in C#? πŸ‘‰ Explained
9. Purpose of the 'using' Keyword in C# πŸ‘‰ Explained
10. Different Data Types in C# πŸ‘‰ Explained
11. Difference Between int and Int32 in C# πŸ‘‰ Explained
12. Difference Between float, double, and decimal in C# πŸ‘‰ Explained
13. What is the Default Value of a Boolean in C#? πŸ‘‰ Explained
14. What is Boxing and Unboxing in C# πŸ‘‰ Explained
15. What are the Different Types of Operators in C# πŸ‘‰ Explained
16. Difference Between Equals and == in C# πŸ‘‰ Explained
17. What is the Null-Coalescing Operator ?? in C# πŸ‘‰ Explained
18. What is the Ternary Operator in C# πŸ‘‰ Explained
19. How Does the Switch Statement Work in C# πŸ‘‰ Explained
20. What is Object-Oriented Programming in C# πŸ‘‰ Explained
21. What are the Four Pillars of OOP in C# πŸ‘‰ Explained
22. What is Encapsulation in C# πŸ‘‰ Explained
23. What is Inheritance in C# πŸ‘‰ Explained
24. What is Polymorphism in C# πŸ‘‰ Explained
25. What is Abstraction in C# πŸ‘‰ Explained
26. What is an Abstract Class in C# πŸ‘‰ Explained
27. What is an Interface in C# πŸ‘‰ Explained
28. Can a Class Implement Multiple Interfaces in C#? πŸ‘‰ Explained
29. Difference Between Abstract Class and Interface in C# πŸ‘‰ Explained
30. How Do You Create a Class in C#? πŸ‘‰ Explained
31. What is a Constructor in C# πŸ‘‰ Explained
32. What Are the Types of Constructors in C# πŸ‘‰ Explained
33. What is a Static Constructor in C# πŸ‘‰ Explained
34. Difference Between Static and Non-Static Members in C# πŸ‘‰ Explained
35. What is the Use of 'this' Keyword in C# πŸ‘‰ Explained
36. What is a Destructor in C# πŸ‘‰ Explained
37. What is Object Initializer Syntax in C# πŸ‘‰ Explained
38. What is the Difference Between Field and Property in C# πŸ‘‰ Explained
Share:

Tags:


Feedback Modal Popup