大可制作:QQ群:31564239(asp|jsp|php|mysql)

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被覆盖的方法上之异常之父类