
Java如何抛出自定义异常?5步实现精准错误处理
1. 理解Java异常体系
Java异常分为Checked Exception(编译时异常)和Unchecked Exception(运行时异常)。自定义异常通常继承这两种基础类型:
– 业务异常推荐继承`Exception`(Checked)
– 非关键异常可继承`RuntimeException`(Unchecked)
体系结构示例:
“`java
Throwable
├─ Exception (Checked)
│ └─ YourCustomException
└─ RuntimeException (Unchecked)
└─ YourBusinessException
“`
2. 创建自定义异常类
通过继承基础异常类实现,建议添加构造方法重载:
“`java
// 继承RuntimeException的未检查异常
public class PaymentFailedException extends RuntimeException {
// 带错误信息的构造器
public PaymentFailedException(String message) {
super(message);
}
// 带错误信息和原因的构造器(重点:异常链)
public PaymentFailedException(String message, Throwable cause) {
super(message, cause);
}
}
“`
3. 抛出异常的最佳实践
在业务逻辑中通过`throw`触发异常,推荐添加详细错误上下文:
“`java
public class OrderService {
public void processPayment(double amount) {
if (amount <= 0) {
// 标红:抛出自定义异常
throw new PaymentFailedException(“支付金额必须大于0,当前金额:” + amount);
}
// 其他处理逻辑…
}
}
“`
4. 异常处理与传递
通过`try-catch`捕获或声明抛出,注意异常包装技巧:
“`java
try {
orderService.processPayment(-100);
} catch (PaymentFailedException e) {
// 重点:记录完整堆栈
logger.error(“支付失败: {}”, e.getMessage(), e);
// 转换为用户友好提示
throw new UserFriendlyException(“支付参数不合法,请检查金额”);
}
“`
5. 实战案例:电商库存校验
完整示例演示自定义异常的典型应用场景:
“`java
// 1. 定义库存不足异常
public class OutOfStockException extends Exception {
private String sku;
public OutOfStockException(String sku, int available) {
super(String.format(“SKU %s 库存不足,当前可用:%d”, sku, available));
this.sku = sku;
}
public String getSku() { return sku; }
}
// 2. 业务逻辑中使用
public class InventoryService {
private Map stock = new HashMap();
public void deductStock(String sku, int quantity) throws OutOfStockException {
int available = stock.getOrDefault(sku, 0);
if (available < quantity) {
throw new OutOfStockException(sku, available);
}
stock.put(sku, available – quantity);
}
}
// 3. 控制器层处理
@RestController
public class OrderController {
@PostMapping("/orders")
public ResponseEntity createOrder(@RequestBody Order order) {
try {
inventoryService.deductStock(order.getSku(), order.getQuantity());
return ResponseEntity.ok().build();
} catch (OutOfStockException e) {
// 重点:区分异常类型处理
return ResponseEntity.badRequest()
.body(new ErrorResponse(“STOCK_ERROR”, e.getMessage()));
}
}
}
“`
关键总结
1. 继承体系选择:根据业务需求决定继承`Exception`还是`RuntimeException`
2. 丰富上下文:在异常中包含业务数据(如SKU、金额等)
3. 异常处理分层:底层抛具体异常,上层做统一包装
4. 日志记录:始终记录完整的异常堆栈(`logger.error(message, e)`)
5. 用户提示:将技术异常转换为用户可理解的消息
通过这5个步骤,可以构建健壮的异常处理体系,显著提升系统可维护性。