else block in a try-except statement

PHOTO EMBED

Tue Jun 18 2024 02:44:46 GMT+0000 (Coordinated Universal Time)

Saved by @pynerds #python

#The else block only gets executed if the try block terminates successfully i.e  no exception was raised inside the block.


import math

try:
   print("Hello, World!")
   print(10/ 2)
   print(math.sqrt(9))

except ValueError:
   print("a valu error has occurred")

except IndexError:
    print("IndexError has occurred")

else:
    print("No Exception was raised.")
content_copyCOPY

https://www.pynerds.com/exception-handling-in-python/