Why can't a 'continue' statement be inside a 'finally' block?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
In Python (before 3.8), placing a continue statement inside a finally block raises SyntaxError: 'continue' not supported inside 'finally' clause. This restriction exists because finally is guaranteed to execute regardless of exceptions, and allowing continue to silently skip out of the finally block could mask exceptions or bypass critical cleanup code. Python 3.8 removed this restriction, but understanding the original reasoning helps avoid subtle bugs when mixing loop control with exception handling.
The Error (Python < 3.8)
Why It Was Prohibited
The finally block has a special contract: it always runs, whether the try block succeeds, raises an exception, or executes a return/break. Allowing continue inside finally creates ambiguous behavior:
If continue runs, it transfers control to the next loop iteration, and the ValueError is silently discarded. This violates the principle that exceptions should be explicitly caught, not implicitly ignored.
Python 3.8+: continue Is Allowed
Starting in Python 3.8 (PEP 601), continue inside finally is legal:
The continue in finally skips the rest of the loop body for even indices.
The Danger: Swallowing Exceptions
Even in Python 3.8+, continue in finally silently discards unhandled exceptions:
This is extremely dangerous. The exception is raised, finally runs, continue transfers control to the next iteration, and the exception disappears without being caught or logged.
Safe Patterns
Handle Exceptions Explicitly
Use a Flag Instead
Separate Cleanup from Control Flow
break and return in finally
The same concern applies to break and return:
Comparison Across Languages
| Language | continue in finally | Behavior |
| Python < 3.8 | SyntaxError | Prohibited |
| Python 3.8+ | Allowed | Silently swallows exceptions |
| Java | Compile error | Prohibited |
| C# | Compile error | Prohibited |
| JavaScript | Allowed | Silently swallows exceptions |
Java and C# also prohibit continue (and break, return) in finally blocks to prevent exception swallowing.
Common Pitfalls
- Using
continueinfinallyto skip iterations (Python 3.8+): While legal, this silently discards any pending exception from thetryblock. If an unhandled exception was about to propagate,continueswallows it without any indication. Always handle exceptions inexceptblocks instead. - Confusing
finallywithexceptfor control flow:finallyis for cleanup (closing files, releasing locks), not for handling errors. Placecontinue,break, and error-recovery logic inexceptblocks, not infinally. - Putting
returninfinallythat overrides thetryreturn value:returninfinallyreplaces whatever value was being returned fromtryorexcept. This can produce confusing results where a function returns a value unrelated to its actual logic. - Assuming
breakinfinallypropagates exceptions:breakinsidefinally(Python 3.8+) exits the loop and silently discards any pending exception, just likecontinue. The exception is lost without being logged or handled. - Writing code that depends on Python 3.8+ behavior: If your code uses
continueinfinally, it will break on Python 3.6/3.7 with aSyntaxError. Check your minimum Python version before using this pattern.
Summary
- Python < 3.8 raises
SyntaxErrorforcontinueinfinally— it was prohibited to prevent silent exception swallowing - Python 3.8+ allows it, but
continueinfinallysilently discards any pending exception - Place loop control (
continue,break) inexceptblocks, not infinally - Use
finallyonly for cleanup code (closing resources, releasing locks) - Java and C# also prohibit
continueinfinallyfor the same safety reasons
Related reading
- Why can't dataclasses have mutable defaults in their class attributes declaration?
- Why can't I build a list by assigning each element in turn? How can I add append the elements without getting an IndexError?
- Why can't I establish connection to rabbitMQ using python?
- Why can''t I use a list as a dict key in python? Exactly what can and cannot be used, and why?
- Why can't I find the 'rabbitmq.config' file while I have already installed RabbitMQ?
- Why can't I increase session.timeout.ms?
- Why can't Python parse this JSON data?
- Why can't Python parse this JSON data?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.