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

πŸ’¬ Feedback
πŸš€ Start Learning
Share:

Tags: