处理多个异常

try{}catch(异常名 接受异常){输出接受的异常}catch(异常名2 接受2){输出2}

public class Demo07Exception {
public static void main(String[] args) {
String s="a.txt";
try {
add(s);//当下面的方法中将异常向上抛出后会来到方法调用处
}catch(FileNotFoundException f){
System.out.println(f);
}catch (IOException e){
System.out.println(e);
}
delete();
updata();
find();
}

private static void add(String s)throws IOException,FileNotFoundException {
    if(s==null){
        throw new IOException("IOException");
    }
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 23:00  DemoLiQi  阅读(23)  评论(0)    收藏  举报