Java基礎面試題之 try-catch-finally 與 null

請寫出下列程序的運行結果:

 

public static String exce(String a, String b) {
			try {		
				
				return a + "+" + b;	
				
			} catch (Exception e) {
				
				System.out.println("I'm Exception");
				
			}finally {
				
				System.out.println("I'm will execute");
				
			}
			return b;
		    }
		
           public static void main(String[] args) {
			
			System.out.println(exce(null,"hello"));
			
}

 

答案:

I'm will execute
null+hello

 

首先我們先來看程序的運行過程,這個程序沒有出錯,不會拋出異常,main方法調用exce方法之後先執行try語句塊裏面的代碼,沒發生異常,則執行finally裏的代碼,最後最後返回的是try語句塊裏的結果,程序並不會執行exce方法後面的return語句。

 

public static String exce(String a, String b) {
			try {		
				
				return a + "+" + b;	

			} catch (Exception e) {
				
				System.out.println("I'm Exception");
			
				
			}finally {
				
				System.out.println("I'm will execute");
				
			}
			return b;
		}
		
       public static void main(String[] args) {
			
			System.out.println(exce(null,"hello"));
			
		}

運行結果:

I'm will execute
null+hello

 

如果try代碼塊有return語句,發生異常,而catch代碼塊裏沒有return語句,則返回的是整個方法的後面return結果

public static String exce(String a, String b) {
			try {		
				
			
				int[] array = {1,2,3,4,};
				for (int i = 0; i < 5; i++) {
					System.out.println(array[i]);
				}
				return a + "+" + b;	
			} catch (Exception e) {
				
				System.out.println("I'm Exception");
				
				
			}finally {
				
				System.out.println("I'm will execute");
				
			}
			return b;
		}
		
       public static void main(String[] args) {
			
			System.out.println(exce(null,"hello"));
			
		}

運行結果:

1
2
3
4
I'm Exception
I'm will execute
hello

 

如果try代碼塊有return語句,發生異常,而catch代碼塊裏也有return語句則返回的是catch代碼塊的結果;

	public static String exce(String a, String b) {
			try {		
				
			
				int[] array = {1,2,3,4,};
				for (int i = 0; i < 5; i++) {
					System.out.println(array[i]);
				}
				return a + "+" + b;	
			} catch (Exception e) {
				
				System.out.println("I'm Exception");
				return "返回exception";
				
			}finally {
				
				System.out.println("I'm will execute");
				
			}
			
		}
		
       public static void main(String[] args) {
			
			System.out.println(exce(null,"hello"));
			
		}

運行結果:

1
2
3
4
I'm Exception
I'm will execute
返回exception

 

只要finally代碼塊有return語句,都返回finally代碼塊的結果。

public static String exce(String a, String b) {
			try {		
				
			
				int[] array = {1,2,3,4,};
				for (int i = 0; i < 5; i++) {
					System.out.println(array[i]);
				}
				return a + "+" + b;	
			} catch (Exception e) {
				
				System.out.println("I'm Exception");
				return "返回exception";
				
			}finally {
				
				System.out.println("I'm will execute");
				return b;
			}
		
		}
		
       public static void main(String[] args) {
			
			System.out.println(exce(null,"hello"));
			
		}

運行結果:

1
2
3
4
I'm Exception
I'm will execute
hello
 

總結:

1、如果try代碼塊有return語句,沒有發生異常,並且finally代碼塊裏沒有return語句則返回try代碼塊的結果;

2、如果try代碼塊有return語句,發生異常,而catch代碼塊裏也有return語句則返回的是catch代碼塊的結果;

3、如果try代碼塊有return語句,發生異常,而catch代碼塊裏沒有return語句,則返回的是整個方法的後面return結果;

4、只要finally代碼塊有return語句,都返回finally代碼塊的結果。

注意:

1、如果catch代碼塊或者finally代碼塊裏有了return語句則不能在該方法後面再寫return語句;

 

這兩種情況都會報錯!

 

 

---------------------------------------------------------------------------------分割線------------------------------------------------------------------------------------------

關於null的問題:

在前面的那道面試題裏,可能我們會有疑問,爲什麼傳了一個空值進去,不僅不報錯還輸出了字符串null,其實是因爲java裏面有一個方法:當我們把String的值賦值爲null的話,在這個方法裏面會進行判斷,如果等於null則會把null轉化爲字符串:“null”,這就是爲什麼傳null進去會輸出字符串“null”的原因。

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