Interview Question

What is finally block?

finally always performs its suite as the try statement exits.

💡 Concept ✅ Quick Revision 🐍 Python

Answer

A finally suite runs as a try statement finishes, whether or not an exception occurred. • It is commonly used for cleanup. • A return or exception in finally can replace an earlier pending result or exception. • Context managers are often clearer for resource cleanup.

💡 Simple Example

try: print('work') finally: print('cleanup')

Output

work cleanup

⚡ Quick Revision

finally always performs its suite as the try statement exits.