Khi code bị lỗi, chương trình sẽ dừng đột ngột. Dùng try-except để bắt lỗi:
Python
try:
so = int(input())
print(10 / so)
except ValueError:
print("Loi: khong phai so!")
except ZeroDivisionError:
print("Loi: chia cho 0!")
finally:
print("Luon chay")Các loại lỗi thường gặp
ValueError: sai kiểu dữ liệuZeroDivisionError: chia cho 0IndexError: truy cập ngoài mảngKeyError: key không tồn tại trong dict