Java执行finally语句

【转载自:http://blog.csdn.net/jiasanshou/article/details/6996932

主要通过几个例子来说明finally语句的执行,至于具体的原因,有待探讨。


开始的(1)(2)(3)例子是为了说明finally语句块不一定被执行。

(1)在try语句块之前有return,没有执行try,也就没有执行finally

package finally_test;

public class test1 
{
	public static void main(String[] args)
	{
		//System.out.println("hello world!");
		System.out.println("return value of test():"+test());
	}
	//在try语句块之前遇到return返回调用test()的函数,没有执行try
	public static int test()
	{
		int i=1;
		System.out.println("the previous statement of try block");
		//return 0;
		//这里不能直接return 0;否则会报错,因为直接造成后面的语句没有执行
		if(i==1)
		{
			return 0;
		}
		//这里采用if,如果释放内存的操作放在之后,也就不会被执行,造成内存泄露,当然指的是C/C++
		try
		{
			System.out.println("try block");
			return i;
		}
		finally
		{
			System.out.println("finally block");
		}
	}
}

运行结果:

the previous statement of try block
return value of test():0


(2) try之前遇到其他异常,提前退出程序

package finally_test;

public class test2
{
	public static void main(String[] args)
	{
		//System.out.println("hello world!");
		System.out.println("return value of test():"+test());
	}
	public static int test()
	{
		int i=1;
		System.out.println("the previous statement of try block");
		i=i/0;
		//try之前遇到其他异常,提前退出程序
		try
		{
			System.out.println("try block");
			return i;
		}
		finally
		{
			System.out.println("finally block");
		}
	}
}
运行结果:

the previous statement of try block
Exception in thread "main" java.lang.ArithmeticException: / by zero
	at finally_test.test2.test(test2.java:14)
	at finally_test.test2.main(test2.java:8)


(3) 在 try 语句块中执行了 System.exit (0) 语句,终止了 Java 虚拟机的运行,也就不执行finally语句块

package finally_test;

public class test3 
{
	public static void main(String[] args)
	{
		//System.out.println("hello world!");
		System.out.println("return value of test():"+test());
	}
	public static int test()
	{
		int i=1;
		try
		{
			System.out.println("try block");
			System.exit(0);
			return i;
		}
		finally
		{
			System.out.println("finally block");
		}
	}
}

运行结果:

try block

(4)很正常的例子。

package finally_test;

public class test4 
{
	public static void main(String[] args)
	{
		try
		{
			System.out.println("try block");
			return;
		}
		finally
		{
			System.out.println("finally block");
		}
	}
}

运行结果:

try block
finally block

(5)注意,在Eclipse里,在int i=1;这一行会警告“the value of the local variable i is not used”

package finally_test;

public class test5 
{
	public static void main(String[] args)
	{
		System.out.println("return value of test():"+test());
	}
	public static int test()
	{
		int i=1;
		try
		{
			System.out.println("try block");
			i=1/0;
			return 1;
		}
		catch(Exception e)
		{
			System.out.println("exception block");
			return 2;
		}
		finally
		{
			System.out.println("finally block");
		}
	}
}

运行结果:

try block
exception block
finally block
return value of test():2

(6)注意,在Eclipse里,会提示"finally block does not complete normally",具体原因见【 http://blog.csdn.net/woshixuye/article/details/8529447】,就是说在finally语句块中不应该有return语句。

package finally_test;

public class test6
{
	public static void main(String[] args)
	{
		//System.out.println("hello world!");
		System.out.println("return value of test():"+test());
	}
	public static int test()
	{
		try
		{
			return 0;
		}
		finally
		{
			return 1;
		}
	}
}

运行结果:

return value of test():1

(7)这个例子非常变态!详细解释见原来的链接。

package finally_test;

public class test7
{
	public static void main(String[] args)
	{
		//System.out.println("hello world!");
		System.out.println("return value of test():"+test());
	}
	public static int test()
	{
		int i=1;
		try
		{
			//i=4;
			return i;
		}
		finally
		{
			++i;
		}
	}
}
运行结果:

return value of test():1


(8)主要是和第(7)个例子做个对比。

package finally_test;

public class test8
{
	public static void main(String[] args)
	{
		//System.out.println("hello world!");
		System.out.println("return value of test():"+test());
	}
	public static int test()
	{
		int i=1;
		try
		{
			i=4;
		}
		finally
		{
			++i;
			//return i;
		}
		return i;
	}
}
运行结果:

return value of test():5

(9)这个例子也非常变态!

package finally_test;

public class test9
{
	public static void main(String[] args)
	{
		//System.out.println("hello world!");
		System.out.println(test());
	}
	public static String test()
	{
		try
		{
			System.out.println("try block");
			return test1();
			//return test1();等同于String tmp=test1(); return tmp;
		}
		finally
		{
			System.out.println("finally block");
		}
	}
	public static String test1()
	{
		System.out.println("return statement");
		return "after return";
	}
}
运行结果:

try block
return statement
finally block
after return

(10)这个例子和(9)差不多

package finally_test;

public class test10
{
	public static void main(String[] args)
	{
		//System.out.println("hello world!");
		System.out.println(test());
	}
	public static int test()
	{
		try
		{
			System.out.println("try block");
			int x=1;
			return test1(x);
			//先执行test1(x);再执行finally,最后return
		}
		finally
		{
			System.out.println("finally block");
		}
	}
	public static int test1(int x)
	{
		System.out.println("return statement");
		x++;
		return x;
	}
}

运行结果:

try block
return statement
finally block
2




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值