Playground / try / except / else / finally
try / except / else / finally
Interactive lab
Try it: try / except / else / finally
How an exception jumps out of a try block to the first matching except clause, when else runs, and why finally always runs.
How it works
- Statements in try run until one raises an exception; the rest of try is skipped.
- Python checks the except clauses in order and runs the first one whose type matches.
- If nothing was raised, the else block runs instead.
- finally runs in every case.
Default run (9 steps): Call safe_divide('4'). … Return 25.0. Output was: Worked: 25.0 / Done.
Simplified: One fixed function; int() and / are modelled exactly (including how Python prints floats), but nothing is executed as Python.
Educational simulation
Loading the simulation…
Related concepts
- Catching exceptions with try and except blocks →
- Error handling with try and except →
- Exception handling with try and except →
- Exception handling with try except →
- Handling errors with try and except →
- Handling multiple exception types →
- Introduction to exception handling with try and except →
- Try and except blocks →
- Try except blocks catching and handling errors →
- Finally blocks →
- Finally blocks for cleanup code →
- Try finally statements →
- Exception handling fundamentals →
- Exception types and how to catch them →
- Array index bounds and exceptions →
- Built in exception types →
- Checked and unchecked exceptions →
- Common exception types →
- Common exception types in python →
- Constructors and instance variables →
- Converting strings to numbers →
- Creating custom exception classes →
- Declaring exceptions with the throws clause →
- Error handling with try except blocks →
- Exception class and inheritance →
- Exception classes and the throwable hierarchy →
- Exception handling →
- Exception handling and caught exceptions →
- Exception handling basics →
- Exception handling best practices →
- Exception handling in c →
- Exception types in java →
- Exception types in python →
- Exceptions in java methods →
- Getting user input with input →
- Handling exceptions with try catch →
- Introduction to error handling →
- Multiple catch clauses and exception types →
- Multithreading and exceptions →
- Predefined java exception classes →
- Raising and catching custom exceptions →
- Raising and handling exceptions →
- Raising exceptions →
- Range checking in vector operations →
- Requesting exceptions →
- The document producing motion in large firms →
- Throwing and catching exceptions →
- Throwing and catching standard exceptions →
- Throwing and declaring exceptions →
- Throwing exceptions and writing your own exception classes →
- Throwing exceptions for precondition violations →
- Understanding class invariants and exception handling →
- Understanding errors and exceptions →
- Understanding exceptions and error messages →
- Using try catch to handle exceptions →
- Vector range checking and exception handling →
- Writing your own subroutines →
More labs in How Python runs your code
- The Call StackTrace a function call
- Loop TracerWatch a for loop accumulate
- Indexing and SlicingPick characters by position
- if / elif / elseWhich branch runs?
- Order of OperationsEvaluate it one step at a time
- Names and ObjectsTwo names, one list
- String methods, character by characterSplit, strip and search a string
- Format spec mini-languageBuild f"{value:spec}" piece by piece
- Type conversion: int(), float(), str(), bool()Convert text to numbers — or watch it fail
- Regular expressions: a backtracking matcherWatch a regex try, fail and backtrack
- Sorting with key= and reverse=Pick a key and watch Python sort by it
- Tuples: Comparison and UnpackingCompare two tuples item by item
- List Methods, Step by StepAppend, insert, pop and watch indexes shift
- Comprehensions, map() and filter()Step through a list comprehension
- Counting Words with a DictionaryBuild a word histogram with counts.get
- Sets: Unique Members and Venn OperationsDrop values into two sets and combine them
- while Loops, break and continueRe-check the condition every pass
- Scope: Local, Enclosing, Global, Built-inWhere does Python find this name?
- Classes, Instances and AttributesBuild objects, then look inside
- Inheritance and Method LookupFollow the MRO up the class tree
- Reading and Writing Text FilesMove the file pointer
- random.seed and the Mersenne TwisterSame seed, same 'random' numbers
- Debug by Tracing ExecutionFind the line that makes it wrong
- Reading a TracebackWalk the frames from where it failed
- Binding Arguments to ParametersBuild a call and watch Python match every argument
- What import Really DoesImport a module twice and see what runs
- Shallow vs Deep Copies of Nested ListsCopy a nested list, change it, see what's shared
- Defining and Calling FunctionsWatch a call get replaced by its return value
- Dictionary OperationsLook up, add, change and delete keys
- Functions as Values: LambdaPass a function and trace each call
- Validate Input Before ProcessingWhich bad input slips through?
- The math Module: Logs, Roots and AnglesPick the base, guard the input
- A Retry Decorator, Step by StepWrap a flaky function and watch it retry
- Escape Sequences in String LiteralsType a literal and see what Python stores
- Comparing Strings: Equality and OrderingSee which character decides
- Context Managers and the with StatementRaise an exception and watch the file close
- Assignment, Statement by StatementRight side first, then the name
- Syntax Errors and IndentationBreak a program, read Python's report
- What a for Loop Hands YouLoop over strings, lists, ranges and dicts
- All 106 labs →Browse every topic