What is the purpose of the finally block?
Short answer
finally always runs (whether or not an exception occurred) and is used to release resources like files, connections or locks.
Advertisement
AModel 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.