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!

๐Ÿ” Navigation

๐Ÿ’ฌ Feedback
๐Ÿš€ Start Learning
Share:

Tags: