處理多個異常

DemoLiQi發表於2024-06-24

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() {
    
}

}