声明异常throws
捕获异常try…catch
package Text1;
import java.io.IOException;
public class Demo12 {
public static void main(String[] args) {
try {
readFile("d:\\a.tx");
} catch (IOException e) {
System.out.println("传递文件的后缀不是txt");
}
System.out.println("后续代码");
}
public static void readFile(String fileName) throws IOException {
if(!fileName.endsWith(".txt")){
throw new IOException("文件后缀名不对");
}
System.out.println("路径没问题,读取文件");
}
}