這一節我們來看看異常鏈

這篇文章就是個筆記,大家可以直接不看。

public class catchline {
	public static void main(String[] args){
		catchline ct=new catchline();
		try{
			ct.test2();
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	public void test1() throws DrinkException{
		throw  new DrinkException("喝車別開酒");
		
	}
	public void test2(){
		try{
			test1();
		}catch(Exception e){//注意這裏必須是Exception e不能只是DrinkException e。那不完善,一般的做法都是在這裏拋出一個普通的大類異常,Exception
			RuntimeException newExc=new RuntimeException("司機一滴酒,親人兩行淚~~");
			newExc.initCause(e);
			throw newExc;
		}
	}
}
class DrinkException extends Exception {
	private static final long serialVersionUID = 1L;
	public DrinkException(String message){
			super(message);
			 
	}
	public DrinkException(){
	}
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章