真真正正的try_catch_finally语句的最根本的地方 和return .system.exit(0)

package Text01;

class Exercise {

	public static String catchMethod() {
		System.out.print("call catchMethod and return  --->>  ");
		return "catchMethod";
	} // finally后续处理工作

	public static String finallyMethod() {
		System.out.println();
		System.out.print("call finallyMethod and do something  --->>  ");
		return "finallyMethod";
	}

	public static String catchTest() {
		try {
			int i = 10 / 0; // 抛出 Exception,后续处理被拒绝
			System.out.println("i vaule is : " + i);
			return "Test"; // Exception 已经抛出,没有获得被执行的机会
		} catch (Exception e) {
			System.out.println(" -- Exception --");
			return catchMethod(); // Exception 抛出,获得了调用方法并返回方法值的机会
		}
	}

	public static String catchFinallyTest1() {
		try {
			int i = 10 / 0; // 抛出 Exception,后续处理被拒绝
			System.out.println("i vaule is : " + i);
			return "Test"; // Exception 已经抛出,没有获得被执行的机会
		} catch (Exception e) {
			System.out.println(" -- Exception --");
			return catchMethod(); // Exception 抛出,获得了调用方法的机会,但方法值在 finally
									// 执行完后才返回
		} finally {
			return finallyMethod(); // Exception 抛出,finally 代码块将在 catch 执行
									// return 之前被执行
		}
	}

	public static String catchFinallyTest2() {
		try {
			int i = 10 / 2; // 不抛出 Exception
			System.out.println("i vaule is : " + i);
			return "Test"; // 获得被执行的机会,但执行需要在 finally 执行完成之后才能被执行
		} catch (Exception e) {
			System.out.println(" -- Exception --");
			return catchMethod();
		} finally {
			finallyMethod();
			// return "finally"; // finally 中含有 return 语句,这个 return
			// 将结束这个方法,不会在执行完之后再跳回
			// try 或 catch 继续执行,方法到此结束,返回 false
		}
	}

	public static String catchFinallyTest3() {
		try {
			int i = 10 / 2; // 不抛出 Exception
			System.out.println("i vaule is : " + i);
			return "Test"; // 获得被执行的机会,但执行需要在 finally 执行完成之后才能被执行
		} catch (Exception e) {
			System.out.println(" -- Exception --");
			return catchMethod();
		} finally {
			finallyMethod();
//			System.exit(0);
			 return "finally"; // finally 中含有 return 语句,这个 return
			// 将结束这个方法,不会在执行完之后再跳回
			// try 或 catch 继续执行,方法到此结束,返回 false
		}
	}

	public static String catchFinallyTest4() {
		try {
			int i = 10 / 0; // 不抛出 Exception
			System.out.println("i vaule is : " + i);
			return "Test"; // 获得被执行的机会,但执行需要在 finally 执行完成之后才能被执行
		} catch (Exception e) {
			System.out.println(" -- Exception --");
			return catchMethod();
		}

	}

	public static void main(String[] args) {
//		Cout.addLine("0---------"+catchTest());Cout.endl();
//		Cout.addLine("1---------"+catchFinallyTest1());Cout.endl();//我这里使用了一个自定义的包,你们可以把它们全部变成System.out.println()语句
//		Cout.addLine("2---------"+catchFinallyTest2());Cout.endl();
		Cout.addLine("3---------"+catchFinallyTest3());Cout.endl();
		Cout.addLine("4---------"+catchFinallyTest4());Cout.endl();
	}
}


看完了你们可以试下变下程序,这个还是自己去实践才是最好的收获。


 -- Exception --
call catchMethod and return  --->>  0---------catchMethod

 -- Exception --
call catchMethod and return  --->>  
call finallyMethod and do something  --->>  1---------finallyMethod

i vaule is : 5

call finallyMethod and do something  --->>  2---------Test

i vaule is : 5

call finallyMethod and do something  --->>  3---------finally

 -- Exception --
call catchMethod and return  --->>  4---------catchMethod

看到了这个应该就懂了吧!!!

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值