Java 异常: try...catch...finally

在看下面的内容之前,想问大家一个问题:

finally 语句是不是一定会被执行 疑问


使用 Integer.valueOf 方法模拟异常,来说明关于 try catch finally 之间的问题。


示例 1:没有异常,正常情况

public class TryCatchFinally {

	public static void main(String[] args) {
		try {
			System.out.println("try statement     :-)");
			Integer.valueOf("2");
		} catch (NumberFormatException nfe) {
			System.out.println("catch statement   :-)");
		} finally {
			System.out.println("finally statement :-)");
		}
	}
}

示例 2:没有异常,正常情况,但是 try 语句里面添加 return 
public class TryCatchFinally {

	public static void main(String[] args) {
		try {
			System.out.println("try statement     :-)");
			Integer.valueOf("2");
			return;
		} catch (NumberFormatException nfe) {
			System.out.println("catch statement   :-)");
		} finally {
			System.out.println("finally statement :-)");
		}
	}
}

示例 1 和 示例 2 的运行结果一致:




示例 3:制造异常

public class TryCatchFinally {

	public static void main(String[] args) {
		try {
			System.out.println("try statement     :-)");
			Integer.valueOf("bluetooth");
		} catch (NumberFormatException nfe) {
			System.out.println("catch statement   :-)");
		} finally {
			System.out.println("finally statement :-)");
		}
	}
}

示例 4:制造异常,catch 语句里面添加 return

public class TryCatchFinally {

	public static void main(String[] args) {
		try {
			System.out.println("try statement     :-)");
			Integer.valueOf("bluetooth");
		} catch (NumberFormatException nfe) {
			System.out.println("catch statement   :-)");
			return;
		} finally {
			System.out.println("finally statement :-)");
		}
	}
}

示例 3 和 示例 4 的运行结果一致:




由此,可以看出以上四种情况,finally 语句都被执行了。


示例 5:制造异常,catch 语句里面添加 return


目的:验证finally代码块后面的代码是否会被执行 ??????

public class TryCatchFinally {

	public static void main(String[] args) {
		try {
			System.out.println("try statement     :-)");
			Integer.valueOf("bluetooth");
		} catch (NumberFormatException nfe) {
			System.out.println("catch statement   :-)");
			return;
		} finally {
			System.out.println("finally statement :-)");
		}

		System.out.println("out of try...catch...finally statement");
	}
}

执行结果:




如果将 catch 语句里面添加 return 注释掉,执行结果:



示例 6:制造异常,try 语句里面添加 return 

public class TryCatchFinally {

	public static void main(String[] args) {
		try {
			System.out.println("try statement     :-)");
			Integer.valueOf("dd");
			return;
		} catch (NumberFormatException nfe) {
			System.out.println("catch statement   :-)");
		} finally {
			System.out.println("finally statement :-)");
		}

		System.out.println("out of try...catch...finally statement");
	}
}

执行结果:




示例 7:无异常,try 语句里面添加 return 

public class TryCatchFinally {

	public static void main(String[] args) {
		try {
			System.out.println("try statement     :-)");
			Integer.valueOf("2");
			return;
		} catch (NumberFormatException nfe) {
			System.out.println("catch statement   :-)");
		} finally {
			System.out.println("finally statement :-)");
		}

		System.out.println("out of try...catch...finally statement");
	}
}

执行结果(无异常,return 返回,所以finally代码块后面的语句就不会执行:-)




以上种种迹象表明:


finally 语句都被执行了,但是还有另外一种情况没有被验证,接着往下看 快哭了


就是在 try...catch... 里面退出 JVM :


public class TryCatchFinally {

	public static void main(String[] args) {
		try {
			System.out.println("try statement     :-)");
			Integer.valueOf("2eer");
			System.exit(0);
		} catch (NumberFormatException nfe) {
			System.out.println("catch statement   :-)");
			System.exit(0);
		} finally {
			System.out.println("finally statement :-)");
		}

		System.out.println("out of try...catch...finally statement");
	}
}


执行结果:



try 或者 catch 语句里面(任意一个)调用 System.exit ,那么 finally 语句就不会被执行。


see also:http://stackoverflow.com/questions/18097496/java-exception-not-caught








©️2020 CSDN 皮肤主题: 酷酷鲨 设计师:CSDN官方博客 返回首页