ITEEDU

Java Gossip: throw、throws

当程序发生错误而无法处理的时候,会丢出对应的例外对象,除此之外,在某些时刻,您可能会想要自行丢出例外,例如在例外处理结束后,再将例外丢出,让下一层例外处理区块来捕捉,若想要自行丢出例外,您可以使用"throw"关键词,并生成指定的例外对象,例如:

throw new ArithmeticException();

举个例子来说明,在Java的除法中,允许除数为浮点数0.0,所得到的是Infinity,即无穷数,如果您想要自行检验除零错误,可以自行丢出例外,最接近这个条件的是ArithmeticException,当除数为整数且为0时,就会引发这个例外,您可以如下丢出例外:

UseThrow.java
public class UseThrow {
	public static void main(String[] args) {
		double dblzero = 0.0;
		try {
			System.out.println("浮点数除以零: "
			+ (100 / dblzero));
			if(dblzero == 0)
			throw new ArithmeticException();
		}
		catch(ArithmeticException e) {
			System.out.println("发生除零例外");
		}
	}
}

执行结果:

浮点数除以零: Infinity 
发生除零例外

每个例外都必须有一个"catch"区块来捕捉,在巢状的try...catch时,必须注意该例外是由何者引发并由何者捕捉,例如:

UseThrow.java
public class UseThrow {
	public static void main(String[] args) {
		try {
			try {
				throw new ArrayIndexOutOfBoundsException();
			}
			catch(ArrayIndexOutOfBoundsException e) {
				System.out.println(
				"ArrayIndexOutOfBoundsException/内层try-catch");
			}
			throw new ArithmeticException();
		}
		catch(ArithmeticException e) {
			System.out.println("发生ArithmeticException");
		}
		catch(ArrayIndexOutOfBoundsException e) {
			System.out.println(
			"ArrayIndexOutOfBoundsException/外层try-catch");
		}
	}
}

执行结果:

ArrayIndexOutOfBoundsException/内层try-catch 
发生ArithmeticException?

在这个程序中,ArrayIndexOutOfBoundsException由内层try-catch丢出并捕捉,由于内层 已经捕捉了例外,所以外层的try-catch中之ArrayIndexOutOfBoundsException并不会捕捉到内层所丢出的例外,但如果 内层的try-catch并没有捕捉到这个例外,则外层try-catch就有机会捕捉这个例外,例如:

UseThrow.java
public class UseThrow {
	public static void main(String[] args) {
		try {
			try {
				throw new ArrayIndexOutOfBoundsException();
			}
			catch(ArithmeticException e) {
				System.out.println(
				"ArrayIndexOutOfBoundsException/内层try-catch");
			}
			throw new ArithmeticException();
		}
		catch(ArithmeticException e) {
			System.out.println("发生ArithmeticException");
		}
		catch(ArrayIndexOutOfBoundsException e) {
			System.out.println(
			"ArrayIndexOutOfBoundsException/外层try-catch");
		}
	}
}

执行结果:

ArrayIndexOutOfBoundsException/外层try-catch

程序中会订定许多方法(Method),这些方法中可能会因某些错误而引发例外,但您不希望直接在这个方法中处理这些例外,而希望呼叫这个它的方法来统一处理,这时候您可以使用"throws"关键词来宣告这个方法将会丢出例外,例如:

private void arrayMethod(int[] arr)
			throws ArrayIndexOutOfBoundsException,ArithmeticException {
	// 实作
}

 注意如果会丢出多种可能的例外时,中间使用逗点分隔;当有方法上使用"throws"宣告例外时,意味着呼叫该方法的呼叫者必须处理这些例外,而被呼叫方法可以保持程序逻辑的简洁,下面这个范例是"throws"的一个简单示范:

UseThrows.java
public class UseThrows {
	public static void main(String[] args) {
		try {
			throwsTest();
		}
		catch(ArithmeticException e) {
			System.out.println("捕捉例外");
		}
	}
	private static void throwsTest()
	throws ArithmeticException {
		System.out.println("这只是一个测试");
		// 程序处理过程假设发生例外
		throw new ArithmeticException();
	}
}

执行结果:

这只是一个测试
捕捉例外

简单的说,您要不就在方法中直接处理例外,要不就在方法上宣告该方法会丢回例外,由呼叫它的呼叫者来处理例外,另一方面,在方法上使用 "throws"宣告可丢出的例外,也表示了您只能丢出所宣告类型的例外,其它的例外您必须在方法中处理完,或是重新包装为所宣告的例外再丢出。

如果使用继承时,在父类别的某个方法上宣告了throws某些例外,而在子类别中重新定义该方法时,您可以:

  • 不处理例外(重新定义时不设定throws)
  • 可仅throws父类别中被重新定义的方法上之某些例外
  • 可throws被重新定义的方法上之例外之子类别

但是您不可以:

  • throws出额外的例外
  • throws被重新定义的方法上之例外之父类别