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 .

0 views

Related Posts

How to Install and Run Ollama on macOS

Ollama is a powerful tool that allows you to run large language models locally on your Mac. This guide will walk you through the steps to...

bottom of page