Cách đơn giản nhất để handle exceptions trong Python là dùng block try-except.
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()
Kết quả
Error: can't find the file or read data