nextRound
TechnologiesCoding ProblemsBookmarksLearning PathsLogin
nextRound
TechnologiesCoding ProblemsBookmarksLearning PathsLogin
nextRound

AI-powered interview preparation platform. Practice with curated questions, mock interviews, and personalized learning paths to crack your dream tech interview.

Quick Links

  • Technologies
  • Mock Interviews
  • Saved Questions
  • Pricing

Company

  • About Us
  • Contact Us

Legal

  • Privacy Policy
  • Terms of Use

© 2026 nextRound. All rights reserved.

Questions
4 of 8
1What does exception chaining (raise X from Y) accomplish, and how does it aid debugging?
2Your program is consuming increasing memory over time (a suspected memory leak) despite Python's garbage collector. How would you investigate this?
3What is the difference between except Exception and a bare except:? Why is the bare form discouraged?
4What is the purpose of the else and finally clauses in a try block?
5A production script fails intermittently with a RecursionError. How would you diagnose and fix it?
6What tools would you use to profile a slow Python function, and how do you interpret the output?
7How would you design a custom exception hierarchy for an application?
8How would you use Python's logging module effectively instead of print statements for debugging production code?
PythonPython
Basics
Control Flow and Functions
Data Structures
Comprehensions & Functional Programming
Iterators, Generators & Decorators
Object-Oriented Programming
Exception Handling & Debugging
Concurrency & Parallelism
Performance & Optimization
Testing
Security
Modules, Packaging & Environment
Type Hinting & Modern Python
System Design & Architecture with Python
Best Practices & Design Patterns
Edge Cases & Tricky Interview Questions
04 / 08

What is the purpose of the else and finally clauses in a try block?

Difficulty: 3/10
Try Except Else Finally, Control Flow, Cleanup

else runs when no exception occurred; finally always runs regardless of outcome

A try statement can have up to four blocks: try, except, else, and finally. The else block runs only if the try block completed without raising, which lets you separate the code that might fail from the code that should only run on success, and it prevents the except clause from accidentally catching errors raised by your success path. The finally block always runs, whether the try succeeded, raised a handled exception, raised an unhandled exception, or executed a return, break, or continue. It is the right place for cleanup, and it is the mechanism context managers use internally.

javascript
  1. 1

    else runs once, after try succeeds, before finally.

  2. 2

    finally always runs, even on return, break, continue, or an exception that is propagating.

  3. 3

    Order: try -> except (if raised) -> else (if not raised) -> finally (always).

  4. 4

    return inside finally overrides any return from try or except. That is almost always a bug.

  5. 5

    Trade-off: with statements are usually a better cleanup mechanism than try/finally, but finally is still needed when the resource is not a context manager.

  6. 6

    Common mistake: putting success-only logic inside try, which causes the except clause to catch bugs in the success path and mask them.

  7. 7

    Version note: try/except/else/finally has been stable since Python 2.5, when finally and except were merged into one statement.

Scenario Questions

0-2 years experience

  1. 1In what order do try, except, else, and finally run?
  2. 2Why might a finally block run even if no exception is raised?

2-5 years experience

  1. 1Your except clause is catching a bug in your own success-path code. How does moving that code to else fix it?
  2. 2You have a finally block with a return statement and your function returns the wrong value. Why?

5-8 years experience

  1. 1You need to acquire a resource, use it, and always release it, but the resource does not support with. How do you structure try/finally and testing?
  2. 2You are wrapping a database transaction. Where do commit, rollback, and cleanup belong?

8+ years experience

  1. 1Design a helper that guarantees cleanup across nested resources with partial failures, and explain how it interacts with contextlib.ExitStack.
  2. 2Explain the interaction between finally, generator close(), async cancellation, and exceptions raised during unwinding, with concrete failure modes.

Follow-up Questions

  • Why is returning inside finally dangerous?
  • When should you use a with statement instead of try/finally?
Sharethis question

Share via WhatsApp, X, Facebook, LinkedIn or copy link. Open Graph preview enabled.