Exceptions

dopeobama

Lurker
Member
Joined
Threads
11
Posts
40
Exceptions in Python are handled with try-except [exceptionname] blocks:

def some_function():
try:
# Division by zero raises an exception
10 / 0
except ZeroDivisionError:
print("Oops, invalid.")
else:
# Exception didn't occur, we're good.
pass
finally:
# This is executed after the code block is run
# and all exceptions have been handled, even
# if a new exception is raised while handling.
print("We're done with that.")

>>> some_function()
Oops, invalid.
We're done with that.
 
  • dopeobama
    Created
  • Last reply
  • 1
    Replies
  • 1K
    Views
  • 2
    Participants
  • Participants list