- throws用在函数上。后面跟着一个或多个异常类。而throw在函数内,跟着异常对象。
- throws是声明抛出异常,调用这个函数的人可以提前知道可能出现的异常,但着只代表一种会出现异常的可能性,也许会出现,也许不会出现。
- throw抛出具体的异常,而且throw下面的语句也不会执行,所以千万不要在throw后再写其他语句,因为执行不到,而执行throw时,代表一定抛出了对应的异常对象。
具体例子:
throws:
int divide (int a, int b) throws Exception{
return a/0;
}
throw:
public static void main(String[] args){
String test = "";
if(test.equals(""){
throw new NumberFormatException();
}
System.out.print(test);
}