java中添加try和catch怎_Java try和catch的使用

盡管由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!");

}

}

注意在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);

}

被零除錯誤顯示下面的消息:

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());

}

}

首先程序執行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());

}

}

這里稍微修改了 第一段代碼,只是在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());

}

}

這里面try語句里面會拋出 java.lang.NumberFormatException,所以程序會先執行catch語句中的邏輯,t賦值為catch,在執行return之前,會把返回值保存到一個臨時變量里面t ‘,執行finally的邏輯,t賦值為finally,但是返回值和t’,所以變量t的值和返回值已經沒有關系了,返回的是catchpackage 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());

}

}

由於try語句里面拋出異常,程序轉入catch語句塊,catch語句在執行return語句之前執行finally,而finally語句有return,則直接執行finally的語句值,返回finallypackage 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());

}

}

這個例子在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());

}

}

這個例子和上面例子中唯一不同的是,這個例子里面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());

}

}

這個例子里面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());

}

}

和上面的例子中try catch的邏輯相同,try語句執行完成執行finally語句,finally賦值t 並且返回t ,最后程序結果返回finallypackage 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());

}

}

這個例子中,對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());

}

}

結果:

ddddddd

catch

return 的結果是在所有合法程序執行之后出現的

對以上所有的例子進行總結

1 try、catch、finally語句中,在如果try語句有return語句,則返回的之后當前try中變量此時對應的值,此后對變量做任何的修改,都不影響try中return的返回值

2 如果finally塊中有return 語句,則返回try或catch中的返回語句忽略。

3 如果finally塊中拋出異常,則整個try、catch、finally塊中拋出異常

所以使用try、catch、finally語句塊中需要注意的是

1 盡量在try或者catch中使用return語句。通過finally塊中達到對try或者catch返回值修改是不可行的。

2 finally塊中避免使用return語句,因為finally塊中如果使用return語句,會顯示的消化掉try、catch塊中的異常信息,屏蔽了錯誤的發生

3 finally塊中避免再次拋出異常,否則整個包含try語句塊的方法回拋出異常,並且會消化掉try、catch塊中的異常

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值