Keep Trying...
We'll cover the following...
Let’s see how the old-fashioned try-catch behaves in Python.
Press + to interact
def some_func():try:return 'from_try'finally:return 'from_finally'def another_func():for _ in range(3):try:continuefinally:print("Finally!")def one_more_func(): # A gotcha!try:for i in range(3):try:1 / iexcept ZeroDivisionError:# Let's throw it here and handle it outside for loopraise ZeroDivisionError("A trivial divide by zero error")finally:print("Iteration", i)breakexcept ZeroDivisionError as e:print("Zero division error occurred", e)
Run the following commands in the terminal below and observe the outputs:
Press + to interact
>>> some_func()>>> another_func()>>> 1 / 0>>> one_more_func()