W3Teacher
  • Explore
    Browse by field
    Artificial Intelligence Accounting & Finance Information Technology
    Show all fields
  • Interview Q&A
    • All Interview Questions
    • Beginner
    • Intermediate
    • Advanced
  • Practice
    • MCQs
    • Quizzes
    • Mock Interviews
  • About
  • Sign in
  • Get Started
  1. Home
  2. C#
  3. Exception Handling
  4. Question
All 16 Exception Handling questions
More in Exception Handling
What is the difference between throw and throw ex inside a catch? How does a try/catch block work in C#, and what happens if no matching catch clause exists? What does the finally block guarantee, and when is it commonly used? When should you use using or await using? What are exception filters (when) and why use them? How do custom exception classes work in C#, and what's the recommended convention for creating one? What is exception filtering using the 'when' clause, and what advantage does it offer over catching and manually re-throwing? Why does 'throw;' (bare throw) preserve the original stack trace, while 'throw ex;' does not? When should you create custom exceptions vs use built-in ones? What is the recommended strategy for exceptions in a layered/enterprise app?
Advertisement
C# › Exception Handling

What is the purpose of the finally block?

Beginner

Answer

finally always runs (whether or not an exception occurred) and is used to release resources like files, connections or locks.
try { conn.Open(); ... }
finally { conn.Dispose(); }
Real-world example Ensuring a database connection is closed even if the query throws.
Previous question What is the difference between throw and throw ex inside a catch? Next question How does a try/catch block work in C#, and what happens if no matching catch clause exists?
All 16 Exception Handling questions
W3Teacher

Learn technologies. Ace technical interviews.


    Learn
    • Technologies
    • Tutorials
    • Interview Questions
    • MCQs
    Practice
    • Quizzes
    • Mock Interviews
    • Bookmarks
    • Dashboard
    Get the latest questions & tips

    Fresh interview questions and practice tips, straight to your inbox.

    Company
    • About
    • Contact us
    • Privacy Policy
    • Terms & Conditions

    © 2026 W3Teacher. All rights reserved.