Python - Raising Exceptions


Raising Exceptions



You can raise exceptions by using the raise statement.
print(1)
raise ValueError
print(2)
You need to specify the type of the exception raised.

Exceptions can be raised with arguments that give detail about them.
For example:
name = "123"
raise NameError("Invalid name!")

In except blocks, the raise statement can be used without arguments to re-raise whatever exception occurred.
For example:
try:
num = 5 / 0
except:
print("An error occurred")
raise











Comments

Popular posts from this blog

Python - Argument Passing

Python - How to Escape special characters in String