儘管由Java運行時系統提供的默認異常處理程序對於調試是很有用的,但通常你希望自己處理異常。這樣做有兩個好處。第一,它允許你修正錯誤。第二,它防止程序自動終止。
package com.lyf;
public class My {
public static void main(String []arg){
int a,b;
try{
b=0;
a=3/b;
System.out.println("NO printed!");
}catch(ArithmeticException e){
System.out.println("Exception: "+e);
System.out.println("Division by zero");
}
System.out.println("the last!");
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
注意在try塊中的對println( )的調用是永遠不會執行的。一旦異常被引發,程序控制由try塊轉到catch塊。執行永遠不會從catch塊“返回”到try塊。一旦執行了catch語句,程序控制從整個try/catch機制的下面一行繼續。
一個try和它的catch語句形成了一個單元。catch子句的範圍限制於try語句前面所定義的語句。一個catch語句不能捕獲另一個try聲明所引發的異常(除非是嵌套的try語句情況)。
被try保護的語句聲明必須在一個大括號之內(也就是說,它們必須在一個塊中)。你不能單獨使用try。
Throwable重載toString( )方法(由Object定義),所以它返回一個包含異常描述的字符串。你可以通過在println( )中傳給異常一個參數來顯示該異常的描述。例如,前面程序的
catch (ArithmeticException e) {
System.out.println("Exception: " + e);
}
- 1
- 2
- 3
被零除錯誤顯示下面的消息:
Exception: java.lang.ArithmeticException: / by zero
try、catch、finally中的細節分析
package com.lyf;
public class My{
public static final String test() {
String t = "";
try {
t = "try";
return t;
} catch (Exception e) {
// result = "catch";
t = "catch";
return t;
} finally {
t = "finally";
}
}
public static void main(String[] args) {
System.out.print(My.test());
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
首先程序執行try語句塊,把變量t賦值爲try,由於沒有發現異常,接下來執行finally語句塊,把變量t賦值爲finally,然後return t,則t的值是finally,最後t的值就是finally,程序結果應該顯示finally,但是實際結果爲try。
package com.lyf;
public class My{
public static final String test() {
String t = "";
try {
t = "try";
return t;
} catch (Exception e) {
// result = "catch";
t = "catch";
return t;
} finally {
t = "finally";
return t;
}
}
public static void main(String[] args) {
System.out.print(My.test());
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
這裏稍微修改了 第一段代碼,只是在finally語句塊裏面加入了 一個 return t 的表達式。
按照第一段代碼的解釋,先進行try{}語句,然後在return之前把當前的t的值try保存到一個變量t’,然後執行finally語句塊,修改了變量t的值,在返回變量t。
package com.lyf;
public class My{
public static final String test() {
String t = "";
try {
t = "try";
Integer.parseInt(null);
return t;
} catch (Exception e) {
System.out.println("Exception: "+e);
t = "catch";
return t;
} finally {
t = "finally";
// System.out.println(t);
// return t;
}
}
public static void main(String[] args) {
System.out.print(My.test());
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
這裏面try語句裏面會拋出 java.lang.NumberFormatException,所以程序會先執行catch語句中的邏輯,t賦值爲catch,在執行return之前,會把返回值保存到一個臨時變量裏面t ‘,執行finally的邏輯,t賦值爲finally,但是返回值和t’,所以變量t的值和返回值已經沒有關係了,返回的是catch
package com.lyf;
public class My{
public static final String test() {
String t = "";
try {
t = "try";
Integer.parseInt(null);
return t;
} catch (Exception e) {
System.out.println("Exception: "+e);
t = "catch";
return t;
} finally {
t = "finally";
// System.out.println(t);
return t;
}
}
public static void main(String[] args) {
System.out.print(My.test());
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
由於try語句裏面拋出異常,程序轉入catch語句塊,catch語句在執行return語句之前執行finally,而finally語句有return,則直接執行finally的語句值,返回finally
package com.lyf;
public class My{
public static final String test() {
String t = "";
try {
t = "try";
Integer.parseInt(null);
return t;
} catch (Exception e) {
System.out.println("Exception: "+e);
t = "catch";
Integer.parseInt(null);
return t;
} finally {
t = "finally";
// System.out.println(t);
//return t;
}
}
public static void main(String[] args) {
System.out.print(My.test());
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
這個例子在catch語句塊添加了Integer.parser(null)語句,強制拋出了一個異常。然後finally語句塊裏面沒有return語句。繼續分析一下,由於try語句拋出異常,程序進入catch語句塊,catch語句塊又拋出一個異常,說明catch語句要退出,則執行finally語句塊,對t進行賦值。然後catch語句塊裏面拋出異常。結果是拋出java.lang.NumberFormatException異常
package com.lyf;
public class My{
public static final String test() {
String t = "";
try {
t = "try";
Integer.parseInt(null);
return t;
} catch (Exception e) {
System.out.println("Exception: "+e);
t = "catch";
Integer.parseInt(null);
return t;
} finally {
t = "finally";
// System.out.println(t);
return t;
}
}
public static void main(String[] args) {
System.out.print(My.test());
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
這個例子和上面例子中唯一不同的是,這個例子裏面finally 語句裏面有return語句塊。try catch中運行的邏輯和上面例子一樣,當catch語句塊裏面拋出異常之後,進入finally語句快,然後返回t。則程序忽略catch語句塊裏面拋出的異常信息,直接返回t對應的值 也就是finally。方法不會拋出異常
package com.lyf;
public class My{
public static final String test() {
String t = "";
try {
t = "try";
Integer.parseInt(null);
return t;
} catch (NullPointerException e) {
t = "catch";
return t;
} finally {
t = "finally";
}
}
public static void main(String[] args) {
System.out.print(My.test());
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
這個例子裏面catch語句裏面catch的是NPE異常,而不是java.lang.NumberFormatException異常,所以不會進入catch語句塊,直接進入finally語句塊,finally對t賦值之後,由try語句拋出java.lang.NumberFormatException異常。
package com.lyf;
public class My{
public static final String test() {
String t = "";
try {
t = "try";
Integer.parseInt(null);
return t;
} catch (NullPointerException e) {
t = "catch";
return t;
} finally {
t = "finally";
return t;
}
}
public static void main(String[] args) {
System.out.print(My.test());
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
和上面的例子中try catch的邏輯相同,try語句執行完成執行finally語句,finally賦值t 並且返回t ,最後程序結果返回finally
package com.lyf;
public class My{
public static final String test() {
String t = "";
try {
t = "try";
Integer.parseInt(null);
return t;
} catch (NullPointerException e) {
t = "catch";
return t;
} finally {
t = "finally";
String.valueOf(null);
return t;
}
}
public static void main(String[] args) {
System.out.print(My.test());
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
這個例子中,對finally語句中添加了String.valueOf(null), 強制拋出NPE異常。首先程序執行try語句,在返回執行,執行finally語句塊,finally語句拋出NPE異常,整個結果返回NPE異常。
package com.lyf;
public class My{
public static final String test() {
String t = "";
try {
t = "try";
Integer.parseInt(null);
return t;
} catch (Exception e) {
t = "catch";
return t;
} finally {
System.out.println("ddddddd");
}
}
public static void main(String[] args) {
System.out.print(My.test());
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
結果:
ddddddd
catch
return 的結果是在所有合法程序執行之後出現的
注意:主函數調用子函數得到結果的過程,好比主函數準備一個空罐子,當子函數要返回結果時,先把結果放到罐子裏,然後再將程序邏輯返回到主函數。
<link rel="stylesheet" href="https://csdnimg.cn/release/phoenix/template/css/markdown_views-ea0013b516.css">
</div>