Java 如何捕获两个文件的FileNotFoundException?

Java 如何捕获两个文件的FileNotFoundException?,java,try-catch,filenotfoundexception,throw,throws,Java,Try Catch,Filenotfoundexception,Throw,Throws,我有两个文本文件,如果找不到这些文件,我想抛出一个异常。我有一个类FileReader,它检查文件是否存在,在我的主目录中,我尝试捕获异常 public FileReader() throws FileNotFoundException { super(); File file1 = new File("file1.txt"); File file2 = new File("file2.txt"); //Throw

我有两个文本文件,如果找不到这些文件,我想抛出一个异常。我有一个类FileReader,它检查文件是否存在,在我的主目录中,我尝试捕获异常

    public FileReader() throws FileNotFoundException {
         super();
         File file1 = new File("file1.txt");
         File file2 = new File("file2.txt");

         //Throws the FileNotFoundException if the files aren't found
         if (!file1.exists()) {
             throw new FileNotFoundException("File \"file1.txt\" was not found.");
         } else {
         //do something
         }
         if (!file2.exists()) {
             throw new FileNotFoundException("File \"file2.txt\" was not found.");
         } else {
         //do something
         }
public FileReader() throws FileNotFoundException {
    super();
    File file1 = new File("file1.txt");
    File file2 = new File("file2.txt");

    String message = "";

    if (!file1.exists()) {
        message = "File \"file1.txt\" was not found.";
    }
    if (!file2.exists()) {
        message += "File \"file2.txt\" was not found.";
    }

    //Throws the FileNotFoundException if the files aren't found
    if (!messag.isEmpty()) {
        throw new FileNotFoundException(message);
    }

    //do something
在另一个类中,如果文件丢失,我希望捕获异常

public class FileIO {

public static void main(String[] args) {

    try {
         //do stuff
    } catch(FileNotFoundException e) {
        System.out.println(e.getMessage());
    } 
如果只缺少一个文件,这将非常有效。但是如果file1和file2都丢失,我只捕获第一个丢失文件的异常,然后程序结束。我的输出是:

File "file1.txt" is not found.
我怎样才能捕捉到两者的异常?我希望它输出:

File "file1.txt" is not found.
File "file2.txt" is not found.

在引发异常之前,可以先构造错误消息

    public FileReader() throws FileNotFoundException {
         super();
         File file1 = new File("file1.txt");
         File file2 = new File("file2.txt");

         //Throws the FileNotFoundException if the files aren't found
         if (!file1.exists()) {
             throw new FileNotFoundException("File \"file1.txt\" was not found.");
         } else {
         //do something
         }
         if (!file2.exists()) {
             throw new FileNotFoundException("File \"file2.txt\" was not found.");
         } else {
         //do something
         }
public FileReader() throws FileNotFoundException {
    super();
    File file1 = new File("file1.txt");
    File file2 = new File("file2.txt");

    String message = "";

    if (!file1.exists()) {
        message = "File \"file1.txt\" was not found.";
    }
    if (!file2.exists()) {
        message += "File \"file2.txt\" was not found.";
    }

    //Throws the FileNotFoundException if the files aren't found
    if (!messag.isEmpty()) {
        throw new FileNotFoundException(message);
    }

    //do something

在引发异常之前,可以先构造错误消息

    public FileReader() throws FileNotFoundException {
         super();
         File file1 = new File("file1.txt");
         File file2 = new File("file2.txt");

         //Throws the FileNotFoundException if the files aren't found
         if (!file1.exists()) {
             throw new FileNotFoundException("File \"file1.txt\" was not found.");
         } else {
         //do something
         }
         if (!file2.exists()) {
             throw new FileNotFoundException("File \"file2.txt\" was not found.");
         } else {
         //do something
         }
public FileReader() throws FileNotFoundException {
    super();
    File file1 = new File("file1.txt");
    File file2 = new File("file2.txt");

    String message = "";

    if (!file1.exists()) {
        message = "File \"file1.txt\" was not found.";
    }
    if (!file2.exists()) {
        message += "File \"file2.txt\" was not found.";
    }

    //Throws the FileNotFoundException if the files aren't found
    if (!messag.isEmpty()) {
        throw new FileNotFoundException(message);
    }

    //do something

明亮的谢谢,太棒了。非常感谢。