异常-Throwable--Error和Exception

本文通过两个示例详细介绍了Java中异常处理的方法,包括如何使用自定义异常进行错误管理,以及如何在service和controller层捕获并处理异常,确保系统的稳定运行。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

引用参考:
--java中的最顶级的异常类Throwable
[url]https://blog.youkuaiyun.com/pangqiandou/article/details/53448977[/url]
--【系列】重新认识Java语言——异常(Exception)
[url]https://blog.youkuaiyun.com/xialei199023/article/details/63251277[/url]
--Java 异常Exception e中e的getMessage()和toString()方法的区别
[url]http://blog.youkuaiyun.com/qq_34629352/article/details/54018840[/url]
--printStackTrace()、toString()、getMessage()的区别
[url]http://blog.youkuaiyun.com/qq_34629352/article/details/54018840[/url]

[b]示例1:[/b]

-->service:
public int doFun(Param p) throws BusinessException{
int rtnFlag=-1;
try {
......
}catch (BusinessException e) {
//业务异常处理
throw e;
}catch (Exception e){
//其它异常处理
logger.error("验证...异常", e);
throw new BusinessException("验证...异常", e);
}
return rtnFlag;
}

-->controller:
public InterfaceResponse doControllerFum(Map<String, Object> p){
try{
InterfaceResponseImpl response = new InterfaceResponseImpl();
...
return InterfaceResponse.success("...成功",response);
...
return InterfaceResponse.fail("...失败",null);
}catch(Exception e){
logger.error("...service异常:",e);
return InterfaceResponse.error("...service异常",null);
}
}

[b]示例2:[/b]
[img]http://dl2.iteye.com/upload/attachment/0124/0717/cf32bb62-97f8-36cf-9c3d-2a92245b6b63.jpg[/img]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值