0% found this document useful (0 votes)
35 views

Python Exception Notes

The document discusses exception handling in Python. It provides examples of different types of errors like syntax errors, logical errors, and runtime errors. It then demonstrates how to handle errors using try and except blocks, including handling specific exceptions like ZeroDivisionError and ValueError. Finally, it shows how to raise custom exceptions using the raise statement.
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views

Python Exception Notes

The document discusses exception handling in Python. It provides examples of different types of errors like syntax errors, logical errors, and runtime errors. It then demonstrates how to handle errors using try and except blocks, including handling specific exceptions like ZeroDivisionError and ValueError. Finally, it shows how to raise custom exceptions using the raise statement.
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

Exception handling in python

*************************
or error

to avoid the abnormal termination of the program

* syntax error

while true:

True ==>
* logical error
/
//

* run time error


*************
ex:1
a=int(input("enter a"))
b=int(input("enter b"))
c=a/b
print(c)

ex:2

c=0
a=int(input("enter a"))
b=int(input("enter b"))
try:
c=a/b
except:
print('error occured, may be zero divide')
print(c)

ex:3
a=int(input("enter a"))
b=int(input("enter b"))
try:
c=a/b
print(c)
except ZeroDivisionError:
print("zero division error")

ex:4 ( multi except)


try:
a=int(input("enter a"))
b=int(input("enter b"))

c=a/b
print(c)
except ZeroDivisionError:
print("zero division error")
except ValueError:
print("check the input")
ex:5 (finally)
try:
a=int(input("enter a"))
b=int(input("enter b"))

c=a/b
print(c)
except ZeroDivisionError:
print("zero division error")
except ValueError:
print("check the input")
finally:
print(" thank you")

Raising Exception
****************
The raise statement allows the programmer to force a specific exception to occur.

ex:

try:
raise NameError("Hi there") # Raise Error
except NameError:
print ("An exception")

You might also like