java中的try语句块_Java中的try、catch、finally块简单的解析

packagecom.wangzhu;importjava.util.HashMap;importjava.util.Map;/*** 在try、catch、finally块中,若try中有return语句,则返回try中变量的值,

* 不管try块外是否对该变量进行了修改, 都不影响try中return的返回值。

* 若finally中有return语句,则忽略try、catch块中的return语句。

* 若finally中抛出异常,则整个try-catch-finally块中抛出异常。

*

* 注意:

* 尽量在try或者catch中使用return语句,通过finally快中达到对try或者catch返回值的修改是不可行的。

* finally块中避免使用return语句,因为finally快中如果使用return语句,

* 会显示的消化掉try、catch块中的异常信息, 屏蔽了错误的发生。

* finally块中皮面再次抛出异常,否则整个try-catch-finally块的方法抛出异常,并会消化掉try、catch块中的异常。

*

*@authorwangzhu2014-7-20下午8:48:43

**/

public classCatchDemo1 {/*** 返回:try

*

*@return

*/

public staticString test() {

String str= "";try{

str= "try";returnstr;

}catch(Exception e) {

str= "catch";returnstr;

}finally{

str= "finally";

}

}/*** 返回:finally

*

*@return

*/

public staticString test1() {

String str= "";try{

str= "try";returnstr;

}catch(Exception e) {

str= "catch";returnstr;

}finally{

str= "finally";returnstr;

}

}/*** 返回:catch

*

*@return

*/

public staticString test2() {

String str= "";try{

str= "try";

Integer.parseInt(null);returnstr;

}catch(Exception e) {

str= "catch";returnstr;

}finally{

str= "finally";

}

}/*** 返回:finally

*

*@return

*/

public staticString test3() {

String str= "";try{

str= "try";

Integer.parseInt(null);

}catch(Exception e) {

str= "catch";returnstr;

}finally{

str= "finally";returnstr;

}

}/*** 返回:finally

*

*@return

*/

public staticString test4() {

String str= "";try{

str= "try";

Integer.parseInt(null);returnstr;

}catch(Exception e) {

str= "catch";

Integer.parseInt(null);returnstr;

}finally{

str= "finally";//若去掉return语句则最后会报异常

returnstr;

}

}/*** 返回:finally

*

*@return

*/

public staticString test5() {

String str= "";try{

str= "try";

Integer.parseInt(null);returnstr;

}catch(NullPointerException e) {

str= "catch";returnstr;

}finally{

str= "finally";//若去掉return语句则最后会报异常

returnstr;

}

}public static String test6(Mapmap) {

String str= "";try{

str= "try";

map.put(str, str);

Integer.parseInt(null);returnstr;

}catch(NullPointerException e) {

str= "catch";

map.put(str, str);returnstr;

}finally{

str= "finally";

map.put(str, str);//若去掉return语句则最后会报异常

returnstr;

}

}public static voidmain(String[] args) {

System.out.println("try: " +CatchDemo1.test());

System.out.println("finally: " +CatchDemo1.test1());

System.out.println("catch: " +CatchDemo1.test2());

System.out.println("finally: " +CatchDemo1.test3());

System.out.println("finally: " +CatchDemo1.test4());

System.out.println("finally: " +CatchDemo1.test5());

Map map = new HashMap();

System.out.println("finally: " +CatchDemo1.test6(map));for(String key : map.keySet()) {

System.out.println(key+ "===" +map.get(key));

}

}

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值