top of page

Try Except in Python

Updated: Apr 19, 2023

Python supports try and except statements.




Based on the actions performed inside try, if there is an exception, the corresponding exception is executed. Then, if there is an else block, it is executed and then finally block is executed if it is present.




a = 100

try:
    print(a)
except:
    print("Error")
else:
    print("Just print")
finally:
    print("Metric Coder")
    
try:
    print(b)
except NameError:
    print("Name error")
except:
    print("Error")
finally:
    print("Finally wow!")
    
if a > -1:
    raise Exception("Positive value...")

The link to the Github repository is here .

Subscribe to get all the updates

© 2025 Metric Coders. All Rights Reserved

bottom of page