Bài 10 - Exception Trong Java - Xử Lý Ngoại Lệ Chuyên Nghiệp

🎥 Video minh họa nguồn: YTB Code Thu


1) Exception là gì?

  • Là sự kiện xảy ra trong quá trình thực thi chương trình làm gián đoạn luồng bình thường
  • VD: NullPointerException, ArrayIndexOutOfBoundsException, FileNotFoundException…

2) Try-catch cơ bản

try {
    int result = 10 / 0; // Ném ArithmeticException
} catch (ArithmeticException e) {
    System.out.println("Không thể chia cho 0");
} finally {
    System.out.println("Luôn được thực thi");
}

3) Multiple catch blocks

try {
    int[] arr = new int[5];
    arr[10] = 50; // ArrayIndexOutOfBoundsException
    int num = Integer.parseInt("abc"); // NumberFormatException
} catch (ArrayIndexOutOfBoundsException e) {
    System.out.println("Lỗi truy cập mảng");
} catch (NumberFormatException e) {
    System.out.println("Lỗi chuyển đổi số");
} catch (Exception e) { // Bắt tất cả exception khác
    System.out.println("Lỗi: " + e.getMessage());
}

4) Throw và Throws

public class AgeValidator {
    public static void validateAge(int age) throws IllegalArgumentException {
        if (age < 0) {
            throw new IllegalArgumentException("Tuổi không được âm");
        }
        if (age > 150) {
            throw new IllegalArgumentException("Tuổi không hợp lệ");
        }
    }
    
    public static void main(String[] args) {
        try {
            validateAge(-5);
        } catch (IllegalArgumentException e) {
            System.out.println("Lỗi: " + e.getMessage());
        }
    }
}

5) Custom Exception

class InsufficientBalanceException extends Exception {
    public InsufficientBalanceException(String message) {
        super(message);
    }
}

class BankAccount {
    private double balance;
    
    public void withdraw(double amount) throws InsufficientBalanceException {
        if (amount > balance) {
            throw new InsufficientBalanceException("Số dư không đủ");
        }
        balance -= amount;
    }
}

6) Best Practices

  1. Luôn xử lý các exception cụ thể trước Exception chung
  2. Đóng tài nguyên trong finally hoặc dùng try-with-resources
  3. Log exception đầy đủ trong production
  4. Tránh catch exception mà không xử lý
  5. Tạo custom exception có ý nghĩa

Tiếp theo: Bài 11: Collection trong Java

Built with Hugo
Theme Stack designed by Jimmy