There are four different syntaxes for raising exceptions in Python 3-
1.Raise exception
try:
raise ValueError
except ValueError as exp:
print ("Hello", exp)
2.raise exception (args)try:
raise ValueError("I have raised an Exception")
except ValueError as exp:
print ("Hello", exp)
3.Raise
This statement is used to re-raise the last exception.
def somefunction():
print("some cleaning")
x=10
y=0
result=None
try:
result=x/y
print(result)
except Exception:
somefunction()
raise
4. raise exception(args) from original_exception
An exception raised in response of another exception which contains the details of the original exception.
class MyCustomException(Exception):
pass
x=10
y=0
reuslt=Nonetry:
try:
result=x/y
except ZeroDivisionError as exp:
print("ZeroDivisionError -- ",exp)
raise MyCustomException("Zero Division ") from exp
except MyCustomException as exp:
print("MyException",exp)
print(exp.__cause__)