📖 Python Cơ bản - Xử lý ngoại lệ và Testing
50 phút

Xử lý ngoại lệ và Testing trong Python

Xử lý ngoại lệ (Exception Handling)

try-except cơ bản

try:
    number = int(input("Nhập một số: "))
    result = 10 / number
    print(f"Kết quả: {result}")
except ValueError:
    print("Lỗi: Vui lòng nhập số hợp lệ!")
except ZeroDivisionError:
    print("Lỗi: Không thể chia cho 0!")
except Exception as e:
    print(f"Lỗi không xác định: {e}")

try-except-else-finally

def read_file(filename):
    try:
        file = open(filename, 'r', encoding='utf-8')
    except FileNotFoundError:
        print("File không tồn tại!")
        return None
    except PermissionError:
        print("Không có quyền truy cập file!")
        return None
    else:
        # Chỉ chạy nếu không có lỗi
        content = file.read()
        file.close()
        return content
    finally:
        # Luôn luôn chạy
        print("Hoàn thành xử lý file")

result = read_file("data.txt")

Tạo exception custom

class InvalidAgeError(Exception):
    """Exception cho tuổi không hợp lệ"""
    def __init__(self, age, message="Tuổi không hợp lệ"):
        self.age = age
        self.message = message
        super().__init__(self.message)
    
    def __str__(self):
        return f"{self.message}: {self.age}"

def set_age(age):
    if age < 0 or age > 150:
        raise InvalidAgeError(age, "Tuổi phải từ 0 đến 150")
    return age

try:
    age = set_age(200)
except InvalidAgeError as e:
    print(e)

Unit Testing với unittest

Viết test cases

# calculator.py
def add(a, b):
    return a + b

def subtract(a, b):
    return a - b

def multiply(a, b):
    return a * b

def divide(a, b):
    if b == 0:
        raise ValueError("Không thể chia cho 0!")
    return a / b

# test_calculator.py
import unittest
from calculator import add, subtract, multiply, divide

class TestCalculator(unittest.TestCase):
    
    def test_add(self):
        self.assertEqual(add(2, 3), 5)
        self.assertEqual(add(-1, 1), 0)
        self.assertEqual(add(0, 0), 0)
    
    def test_subtract(self):
        self.assertEqual(subtract(5, 3), 2)
        self.assertEqual(subtract(0, 5), -5)
    
    def test_multiply(self):
        self.assertEqual(multiply(3, 4), 12)
        self.assertEqual(multiply(0, 5), 0)
    
    def test_divide(self):
        self.assertEqual(divide(10, 2), 5)
        self.assertEqual(divide(5, 2), 2.5)
        
        # Test exception
        with self.assertRaises(ValueError):
            divide(10, 0)

if __name__ == '__main__':
    unittest.main()

Chạy tests

python -m unittest test_calculator.py
python -m unittest discover  # Tìm và chạy tất cả tests

Debugging với pdb

import pdb

def complex_calculation(a, b, c):
    result = a * b
    pdb.set_trace()  # Điểm dừng debug
    result += c
    result /= a
    return result

# Gọi hàm để debug
print(complex_calculation(10, 5, 3))

Logging

import logging

# Cấu hình logging
logging.basicConfig(
    level=logging.DEBUG,
    format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
    handlers=[
        logging.FileHandler('app.log'),
        logging.StreamHandler()
    ]
)

def process_data(data):
    logging.info(f"Bắt đầu xử lý data: {data}")
    
    try:
        result = int(data) * 2
        logging.debug(f"Kết quả tính toán: {result}")
        return result
    except ValueError as e:
        logging.error(f"Lỗi xử lý data: {e}")
        return None

# Sử dụng
process_data("10")
process_data("abc")

📝 Bài tập (1)

  1. Tạo unit tests cho class BankAccount

Bài học "Xử lý ngoại lệ và Testing" - Khóa học "Python Cơ bản"