How to manually raise an exception in Python

2 Answers

0 votes
x = 2345
y = 0

try:
    if y == 0:
        raise ZeroDivisionError("Error: divide by zero (y = 0)")
        
        x = x/y
except Exception as e:
    print("Exception : " + repr(e))

    




'''
run:

Exception : ZeroDivisionError('Error: divide by zero (y = 0)')

'''

 



answered Apr 27, 2021 by avibootz
0 votes
i = "python"

try:
    if not type(i) is int:
        raise TypeError("i is not an int")
except Exception as e:
    print("Exception : "+ repr(e))
    




'''
run:

Exception : TypeError('i is not an int')

'''

 



answered Apr 27, 2021 by avibootz

Related questions

1 answer 284 views
2 answers 261 views
261 views asked Sep 11, 2018 by avibootz
2 answers 221 views
1 answer 304 views
2 answers 273 views
273 views asked Apr 27, 2021 by avibootz
...