- try — khối code có thể raise exception
- except — xử lý exception
- else — chạy nếu KHÔNG có exception trong try
- finally — LUÔN chạy, dù có exception hay không — dùng để cleanup
python
try:
result = 10 / divisor
except ZeroDivisionError as e:
print(f"Error: {e}")
except (TypeError, ValueError):
print("Type or value error")
else:
print(f"Success: {result}") # Chỉ khi không có exception
finally:
print("Cleanup") # Luôn chạy