<strong>package</strong> com.ibm.dw.sample.exception;
/**
* 自定义 RuntimeException
* 添加错误代码属性
*/
<strong>public</strong> <strong>class</strong> <strong>RuntimeException</strong> <strong>extends</strong> <strong>java</strong>.<strong>lang</strong>.<strong>RuntimeException</strong> {
//默认错误代码
<strong>public</strong> <strong>static</strong> <strong>final</strong> Integer GENERIC = 1000000;
//错误代码
<strong>private</strong> Integer errorCode;
<strong>public</strong> <strong>RuntimeException</strong>(Integer errorCode, Throwable cause) {
<strong>this</strong>(errorCode, <strong>null</strong>, cause);
}
<strong>public</strong> <strong>RuntimeException</strong>(String message, Throwable cause) {
//利用通用错误代码
<strong>this</strong>(GENERIC, message, cause);
}
<strong>public</strong> <strong>RuntimeException</strong>(Integer errorCode, String message, Throwable cause) {
<strong>super</strong>(message, cause);
<strong>this</strong>.errorCode = errorCode;
}
<strong>public</strong> Integer <strong>getErrorCode</strong>() {
<strong>return</strong> errorCode;
}
}
<strong>public</strong> Customer <strong>retrieveCustomerById</strong>(Long id) <strong>throw</strong> SQLException {
//根据 ID 查询数据库
}
<strong>public</strong> Customer <strong>retrieveCustomerById</strong>(Long id) {
<strong>try</strong>{
//根据 ID 查询数据库
}<strong>catch</strong>(SQLException e){
//利用非检测异常封装检测异常,降低层次耦合
<strong>throw</strong> <strong>new</strong> RuntimeException(SQLErrorCode, e);
}<strong>finally</strong>{
//关闭连接,清理资源
}
}
<strong>public</strong> <strong>void</strong> <strong>retrieveObjectById</strong>(Long id){
<strong>try</strong>{
//..some code that throws SQLException
}<strong>catch</strong>(SQLException ex){
/**
*了解的人都知道,这里的异常打印毫无意义,仅仅是将错误堆栈输出到控制台。
* 而在 Production 环境中,需要将错误堆栈输出到日志。
* 而且这里 catch 处理之后程序继续执行,会导致进一步的问题*/
ex.printStacktrace();
}
}
<strong>public</strong> <strong>void</strong> <strong>retrieveObjectById</strong>(Long id){
<strong>try</strong>{
//..some code that throws SQLException
}
<strong>catch</strong>(SQLException ex){
<strong>throw</strong> <strong>new</strong> RuntimeException(“Exception <strong>in</strong> retieveObjectById”, ex);
}
<strong>finally</strong>{
//clean up resultset, statement, connection etc
}
}
<strong>for</strong>(<strong>int</strong> i=0; i<100; i++){
<strong>try</strong>{
}<strong>catch</strong>(XXXException e){
//….
}
}
<strong>public</strong> <strong>void</strong> <strong>retrieveObjectById</strong>(Long id){
<strong>try</strong>{
//…抛出 IOException 的代码调用
//…抛出 SQLException 的代码调用
}<strong>catch</strong>(Exception e){
//这里利用基类 Exception 捕捉的所有潜在的异常,如果多个层次这样捕捉,会丢失原始异常的有效信息
<strong>throw</strong> <strong>new</strong> RuntimeException(“Exception <strong>in</strong> retieveObjectById”, e);
}
}
<strong>public</strong> <strong>void</strong> <strong>retrieveObjectById</strong>(Long id){
<strong>try</strong>{
//..some code that throws RuntimeException, IOException, SQLException
}<strong>catch</strong>(IOException e){
//仅仅捕捉 IOException
<strong>throw</strong> <strong>new</strong> RuntimeException(/*指定这里 IOException 对应的错误代码*/code,“Exception <strong>in</strong> retieveObjectById”, e);
}<strong>catch</strong>(SQLException e){
//仅仅捕捉 SQLException
<strong>throw</strong> <strong>new</strong> RuntimeException(/*指定这里 SQLException 对应的错误代码*/code,“Exception <strong>in</strong> retieveObjectById”, e);
}
}
<strong>try</strong>{
//可能抛出 RuntimeException、IOExeption 或者其它;
//注意这里和误区六的区别,这里是一段代码抛出多种异常。以上是多段代码,各自抛出不同的异常
}<strong>catch</strong>(<strong>Exception</strong> e){
//一如既往的将 Exception 转换成 RuntimeException,但是这里的 e 其实是 RuntimeException 的实例,已经在前段代码中封装过
<strong>throw</strong> <strong>new</strong> RuntimeException(/**/code, /**/, e);
}
<strong>public</strong> <strong>class</strong> <strong>A</strong> {
<strong>private</strong> <strong>static</strong> Logger logger = LoggerFactory.getLogger(A.class);
<strong>public</strong> <strong>void</strong> <strong>process</strong>(){
<strong>try</strong>{
//实例化 B 类,可以换成其它注入等方式
B b = <strong>new</strong> B();
b.process();
//other code might cause exception
} <strong>catch</strong>(XXXException e){
//如果 B 类 process 方法抛出异常,异常会在 B 类中被打印,在这里也会被打印,从而会打印 2 次
logger.error(e);
<strong>throw</strong> <strong>new</strong> RuntimeException(/* 错误代码 */ errorCode, /*异常信息*/msg, e);
}
}
}
<strong>public</strong> <strong>class</strong> <strong>B</strong>{
<strong>private</strong> <strong>static</strong> Logger logger = LoggerFactory.getLogger(B.class);
<strong>public</strong> <strong>void</strong> <strong>process</strong>(){
<strong>try</strong>{
//可能抛出异常的代码
}
<strong>catch</strong>(XXXException e){
logger.error(e);
<strong>throw</strong> <strong>new</strong> RuntimeException(/* 错误代码 */ errorCode, /*异常信息*/msg, e);
}
}
}
public <strong>void</strong> retieveObjectById(Long id){
<strong>try</strong>{
//..some code that throws SQLException
}<strong>catch</strong>(SQLException ex){
//将参数信息添加到异常信息中
<strong>throw</strong> <strong>new</strong> RuntimeException(“Exception <strong>in</strong> retieveObjectById <strong>with</strong> Object Id :”+ id, ex);
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有