try_catch处理异常


try_catch使用不影响其他功能的使用 这点比无脑throws强
public class Demo06Exception {
public static void main(String[] args){
String s="a.txt";
try{
add(s);
}
catch (FileNotFoundException e){
System.out.println(e);
}
delete();
updata();
find();
}

private static void add(String s)throws FileNotFoundException{

if(!s.endsWith(".txt")){
throw new FileNotFoundException("文件找不到");//编译异常
}
System.out.println("我要执行了");
}

private static void delete() {
}

private static void updata() {
    
    
}

private static void find() {
    
}

}

posted @ 2024-06-24 22:51  DemoLiQi  阅读(17)  评论(0)    收藏  举报