throws:声明一个方法可能产生的所有异常,不做任何处理,而是将其往上传,将异常抛给调用者,只是表示出现异常的一种可能性,并不一定会发生这些异常
throw:抛出一个具体的异常类型,当执行了 throw 时,一定抛出了某种类型的异常
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class Task {
public static void main(String[] args) throws FileNotFoundException {
test();
}
public static void test() throws FileNotFoundException {
File file = new File("abc");
if(!file.exists()) { //判断文件是否存在
// 文件不存在,则抛出文件不存在异常
throw new FileNotFoundException("该文件不存在");
} else {
FileInputStream fs = new FileInputStream(file);
}
}
}