`

深入探索 高效的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);
	}
}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics