...

/

Keep Trying...

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:
continue
finally:
print("Finally!")
def one_more_func(): # A gotcha!
try:
for i in range(3):
try:
1 / i
except ZeroDivisionError:
# Let's throw it here and handle it outside for loop
raise ZeroDivisionError("A trivial divide by zero error")
finally:
print("Iteration", i)
break
except 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()
Terminal 1
Terminal
Loading...

Explanation

...
...