深入探索 高效的Java异常处理框架

突然想起应用中异常处理问题,以前很少应用这块知识,或者说应用得不好
看到一篇很好的文章,真的是能够从整体分析,架构,非常精彩,完全是有经验之谈
特发送连接地址
http://blog.csdn.net/huxin1/archive/2009/07/30/4395121.aspx
同时为了便于理解文中对运行时异常(也称为非检查型异常 unchecked exception)在应用程序的使用,特发一个实际例子来说明自定义运行时异常的方式和优越点,相比非运行时异常(也称为检查异常 checked exception)一定要申明和捕获带来程序编码上的“灾难”的缺点。


package com.fruitking.caipiao;

public class TestException {

/**
* @param args
*/
public static void main(String[] args) {
TestException test = new TestException();
try{
test.read();
}catch(DaoException e){
e.printStackTrace();
}
try{
test.write();
}catch(AppException e){
e.printStackTrace();
}
try{
test.rewrite();
}catch(AppException e){
e.printStackTrace();
}
test.write();
test.rewrite();//这句没有执行到,能理解么?
}

public void read()throws DaoException{//检查型异常
System.out.println();
System.out.println("this is a checked exception!");
throw new DaoException("出错啦");
}

public void write(){//不检查型异常
System.out.println();
System.out.println("this is a unchecked exception!");
throw new AppException("也出错啦");
}

public void rewrite(){//不检查型异常
System.out.println();
System.out.println("this is a unchecked exception too!");
write();
}

}

class DaoException extends Exception{

public DaoException(){
super();
}

public DaoException(String message){
super(message);
}
public DaoException(Throwable throwable){
super(throwable);
}
public DaoException(String message,Throwable throwable){
super(message, throwable);
}
}

class AppException extends RuntimeException{

public AppException(){
super();
}

public AppException(String message){
super(message);
}
public AppException(Throwable throwable){
super(throwable);
}
public AppException(String message,Throwable throwable){
super(message, throwable);
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值