🧯 try-catch cơ bản
public class TryCatchDemo {
public static int parseIntSafe(String s) {
try {
return Integer.parseInt(s);
} catch (NumberFormatException e) {
return -1; // fallback
} finally {
// dọn dẹp tài nguyên nếu có
}
}
}
📤 throws & rethrow
void readFile(Path p) throws IOException {
Files.readAllLines(p); // có thể ném IOException
}
void service(Path p) {
try {
readFile(p);
} catch (IOException e) {
throw new RuntimeException("Đọc file thất bại", e); // wrap để log stack gốc
}
}
🧰 Custom Exception
class BusinessException extends RuntimeException {
public BusinessException(String message) { super(message); }
}
class UserService {
void register(String email) {
if (!email.contains("@")) throw new BusinessException("Email không hợp lệ");
}
}
✅ Best practices
- Bắt đúng ngoại lệ, không catch chung
Exception
trừ lớp boundary. - Ghi log kèm context; giữ nguyên
cause
khi wrap. - Dọn dẹp tài nguyên bằng try-with-resources.
try (var in = Files.newInputStream(Path.of("data.txt"))) {
// xử lý
}