Cách đơn giản nhất để xử lý các trường hợp ngoại lệ trong python là sử dụng khối "thử ngoại trừ".
Ví dụ
try:
fob = open("test.txt", "r")
fob.write("This is my test file for exception handling!!")
except IOError:
print "Error: can\'t find the file or read data"
else:
print "Write operation is performed successfully on the file"
fob.close() Đầu ra
Error: can't find the file or read data