throws---------->把异常交给调用处。
可以结合throw来同时使用。
throws 用在方法声明处,表示本方法不处理异常。可以结合throw使用
throw 表示在方法中手工抛出一个异常。
class Math {
public int div(int i, int j) throws Exception { // 交给调用处
System.out.println("********计算开始********");
int temp = 0;
try {
temp = i / j;
}
catch (Exception e) {
throw e; // 交给调用处
}
finally { // 必须执行
System.out.println("*********** END ********");
}
return temp;
}
} public class ThrowsDemo1 {
public static void main(String args[]) {
Math m = new Math(); // 实例化Math对象 try {
System.out.println("除法操作: " + m.div(10,0));
}
catch (Exception e) { // 进行异常捕获
System.out.println("异常产生: " + e);
}
}
}
当内部要finally时,则用throw来交给调用处出来。