`

谜题36-谜题

F# 
阅读更多
谜题36:优柔寡断
public class Main36 {
	public static void main(String[] args) {
		System.out.println(decision()); //false
	}
	private static boolean decision() {
		try{
			return true;
		}
		finally{
			return false;
		}
	}
}

原因:在一个try-finally语句中,finally总是在控制权离开try语句块时执行的。无论try语句块是正常结束还是意外结束,情况都是如此。
总之,千万不要用return、break、continue或throw来退出finally语句块,并且千万不要允许让受检查的异常传播到finally语句块之外。

谜题37:极端不可思议
先看下面三个程序,都会打印出什么呢?
public class Main1 {

	public static void main(String[] args) {
		 try {
		  System.out.println("Hello world");
		 } catch (IOException e) {
		 System.out.println("I've never seen println fail!");
		 }}}

public class Main2 {

	public static void main(String[] args) {
		 try {
		 //if you have nothing nice to say,say nothing.
		 } catch (Exception e) {
		 System.out.println("This can't happen!");
		 }}}

import java.io.IOException;
interface Type1 {
	void f() throws CloneNotSupportedException;
}
interface Type2 {
	void f() throws InterruptedException;
}
interface Type3 extends Type1, Type2 {

}
public class Main37 implements Type3 {

	public static void main(String[] args) {
		Type3 t3 = new Main37();
		t3.f();//打印Hello world
	}
	@Override
	public void f() {
		System.out.println("Hello world");
	}
}

第一个程序编译不过,因为println方法没有声明会抛出任何受检查异常,而IOException却是一个受检查异常,语言规范中描述到:如果一个catch子句要捕捉一个类型为E的受检查异常,而相应的try子句不能抛出E的某个子类型的异常,那么这就是一个编译错误
第二个可以编译,但什么都不会打印。捕捉Exception或Throwable的catch子句是合法的,不管相应的try子句的内容如何。
第三个也可以编译,一个方法可以抛出的受检查异常集合时它所适用的所有类型声明要抛出的受检查异常的交集,而不是合集。因此静态类型为Type3的对象上的方法f根本就不会抛出异常。
谜题38:不受欢迎的宾客
public class Main38 {

	public static final long GUEST_USER_ID = -1;
	private static final long USER_ID;
	static {
		try {
			USER_ID = getUserIdFromEnvironment();
		} catch (IdUnavailableException e) {
			USER_ID = GUEST_USER_ID;
		}
	}

	public static void main(String[] args) {

		System.out.println("User ID: " + USER_ID);
	}

	private static long getUserIdFromEnvironment()
			throws IdUnavailableException {
		throw new IdUnavailableException();
	}

}

  该程序不能编译,要确定一个程序是否可以不止一次对一个空final进行赋值是很困难的问题,事实上,这是不可能的。一个空final域只有在它的确未赋值的地方才可以被赋值。
谜题39:您好,再见
public class Main39 {
	public static void main(String[] args) {
		try {
			System.out.println("hello world!");
			System.exit(0);
		}
		finally{
			System.out.println("goodbye world!");
		}
	}
}
只打印出hello world!
不论try语句块的执行是正常地还是意外地结束,finally语句块确实都会执行。然而,在这个程序中,try语句块根本没有结束其执行过程。System.exit方法将停止当前线程和所有其他当场死亡的线程。finally子句的出现不能给予线程继续执行的特殊权限。
谜题40:不情愿的构造器
public class Reluctant {

	private Reluctant internalInstance = new Reluctant();
	public Reluctant() throws Exception{
		throw new Exception("I'm not coming out!");
	}
	
	public static void main(String[] args) {
		try {
			Reluctant b = new Reluctant();
			System.out.println("Surprise!");
		} catch (Exception e) {
			System.out.println("I told you so");
		}
	}
}

会抛出java.lang.StackOverflowError异常。 与大多数抛出java.lang.StackOverflowError异常的程序一样本程序也包含了一个无限递归。当你调用一个构造器时,实例变量的初始化操作将先于构造器的程序体而运行。因为java.lang.StackOverflowError是Error的子类型,而不是Exception的子类型,所以
main中的catch子句无法捕捉到它。需要注意,构造器必须声明其实例初始化操作会抛出的所有受检查异常。
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics