访问修饰符 返回类型 方法名(参数列表) throws 异常类型1, 异常类型2 {
// 方法体
}
public void readConfigFile(String filePath) throws IOException {

BufferedReader reader = new BufferedReader(new FileReader(filePath));
// 读取配置文件内容
String line;
while ((line = reader.readLine()) != null) {
processConfigLine(line);
}
reader.close();
}
// 适合立即处理的场景 public boolean validateEmail(String email) {

if (email == null || !email.contains("@")) {
logger.warn("邮箱格式错误: {}", email);
return false;
}
return true;
}
// 适合抛出异常的场景
public void connectDatabase() throws SQLException {
// 数据库连接失败应该由调用者决定重试策略或降级方案
Connection conn = DriverManager.getConnection(url, username, password);
}