1. 程式人生 > >深入理解try catch吃掉異常,及catch(Exception e)中的異常

深入理解try catch吃掉異常,及catch(Exception e)中的異常

package test.s;
public class yichang {
	
	public static void main(String[] args) throws Exception{
		try{
			double a=aa();
			System.out.println(a);
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	public static double aa() throws Exception{
		double b = 0;
		try{
			b=1/0;
		}catch(Exception e){
			throw new Exception(e.getMessage());
		}
		return b;
	}

}

輸出:
java.lang.Exception: / by zero
	at test.s.yichang.aa(yichang.java:18)
	at test.s.yichang.main(yichang.java:6)

說明:這算是比較正常的異常寫法。aa()方法丟擲異常,mian方法捕獲異常,並打印出異常原因。

2,

package test.s;
public class yichang {
	
	public static void main(String[] args) throws Exception{
		try{
			double a=aa();
			System.out.println(a);
		}catch(Exception e){
		}
	}
	
	public static double aa() throws Exception{
		double b = 0;
		try{
			b=1/0;
		}catch(Exception e){
			throw new Exception(e.getMessage());
		}
		return b;
	}

}

沒有輸出;

說明:這個跟1的區別是main方法捕獲aa傳來的異常後沒有將異常打印出來,所以沒有任何輸出。

3,

package test.s;
public class yichang {
	
	public static void main(String[] args) throws Exception{
		try{
			double a=aa();
			System.out.println(a);
		}catch(NullPointerException e){
			e.printStackTrace();
		}
	}
	
	public static double aa() throws Exception{
		double b = 0;
		try{
			b=1/0;
		}catch(Exception e){
			throw new Exception(e.getMessage());
		}
		return b;
	}

}
輸出:
Exception in thread "main" java.lang.Exception: / by zero
	at test.s.yichang.aa(yichang.java:18)
	at test.s.yichang.main(yichang.java:6)

說明:在主方法中的catch(nullPointerException e)是空指標異常。而aa()方法丟擲來的異常是ArithmeticException,所以main方法雖然用try catch把aa()方法包裹起來,但是並沒有捕獲改異常。控制檯列印的是java自己處理打印出來的異常。

效果跟下面的程式碼是一樣的:也就是main方法中不用try catch

package test.s;
public class yichang {
	
	public static void main(String[] args) throws Exception{
			double a=aa();
			System.out.println(a);
	}
	
	public static double aa() throws Exception{
		double b = 0;
		try{
			b=1/0;
		}catch(Exception e){
			throw new Exception(e.getMessage());
		}
		return b;
	}

}

4,
package test.s;
public class yichang {
	
	public static void main(String[] args) throws Exception{
		try{
			double a=aa();
			System.out.println(a);
		}catch(NullPointerException e){
			e.printStackTrace();
		}
	}
	
	public static double aa() throws Exception{
		double b = 0;
		try{
			b=1/0;
		}catch(NullPointerException e){
			throw new NullPointerException(e.getMessage());
		}
		return b;
	}

}

輸出:
Exception in thread "main" java.lang.ArithmeticException: / by zero
	at test.s.yichang.aa(yichang.java:16)
	at test.s.yichang.main(yichang.java:6)

說明這種是catch(NullPointerException e),在aa方法中只能捕獲空指標異常,但是b=1/0報的是算術異常,因此也是無法捕獲的。使用debug跑程式會發現程式運到b=1/0就列印異常結束程式了。

因此同以下程式碼:

package test.s;
public class yichang {
	
	public static void main(String[] args){
			double a=aa();
			System.out.println(a);

	}
	
	public static double aa() {
		double b = 0;
			b=1/0;

		return b;
	}

}

5,
	package test.s;
	public class yichang {
		
		public static void main(String[] args) throws Exception{
			try{
				double a=aa();
				System.out.println(a);
			}catch(NullPointerException e){
				e.printStackTrace();
			}
		}
		
		public static double aa() throws Exception{
			double b = 0;
			try{
				b=1/0;
			}catch(ArithmeticException e){
				throw new ArithmeticException(e.getMessage());
			}
			return b;
		}
	
	}

輸出:
Exception in thread "main" java.lang.ArithmeticException: / by zero
	at test.s.yichang.aa(yichang.java:18)
	at test.s.yichang.main(yichang.java:6)
說明:這中情況也很明顯了。aa方法中的try catch 能捕獲異常,但是mian方法中的try catch不行

6,最準確的情況

	package test.s;
	public class yichang {
		
		public static void main(String[] args) throws Exception{
			try{
				double a=aa();
				System.out.println(a);
			}catch(ArithmeticException e){
				e.printStackTrace();
			}
		}
		
		public static double aa() throws Exception{
			double b = 0;
			try{
				b=1/0;
			}catch(ArithmeticException e){
				throw new ArithmeticException(e.getMessage());
			}
			return b;
		}
	
	}


輸出:

java.lang.ArithmeticException: / by zero
	at test.s.yichang.aa(yichang.java:18)
	at test.s.yichang.main(yichang.java:6)

說明:因為知道aa方法丟擲的異常是ArithmeticException,所以準確定位第一層異常捕獲。然後在main方法中也精確捕獲到aa方法拋來的算術異常。

總結,正確使用try catch 異常,try 不是能吃掉所有的異常,必須要在catch中使用正確的異常才能捕獲。但是在實際開發中,很難精確的捕獲可能存在的異常。因此我們大多使用第一種情況,exception是所有異常的父類,能捕獲到所有的異常。

新增:對於方法套嵌層級很多的,如果在最外層的方法被try catch,那麼無論多少層級,最後都會被最外層的try catch捕獲到,比如說在實際工作中我們經常會看到這樣的程式碼,最外層的方法被try catch,如果有個方法出現空指標異常,那麼最後列印的資訊會是最外層catch輸出的錯誤說明。