Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/330.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 当发生输出异常时,如何保留程序的测试结果?_Java - Fatal编程技术网

Java 当发生输出异常时,如何保留程序的测试结果?

Java 当发生输出异常时,如何保留程序的测试结果?,java,Java,我需要一些关于程序逻辑的输入。 我的程序逻辑基本上如下所示: - Ask user to select input file; -- If exception --> error message, exit program; -- Otherwise --> continue program, perform tests; - After tests have been completed, ask user to select output file; -- If exceptio

我需要一些关于程序逻辑的输入。 我的程序逻辑基本上如下所示:

- Ask user to select input file;
-- If exception --> error message, exit program;
-- Otherwise --> continue program, perform tests;
- After tests have been completed, ask user to select output file;
-- If exception --> error message, exit program;
-- Otherwise --> save results to file, exit program;
public void printToFile() {

    JFileChooser outputFile;
    File outFileName;

    outputFile = new JFileChooser();

    outputFile.showSaveDialog(null);
    outFileName = outputFile.getSelectedFile();
    try {
        PrintStream outFile = new PrintStream(outFileName);
        // print results to file
    } catch (Exception e) {
        System.out.print("Can't be saved to this file! \n");
        System.exit(0); // exit program
    }
}
问题是,如果用户指定的输出文件无效,则所有测试结果都将丢失,程序将终止。如果用户选择了无效的输出文件,我想让他第二次尝试选择输出文件

我的输出代码当前如下所示:

- Ask user to select input file;
-- If exception --> error message, exit program;
-- Otherwise --> continue program, perform tests;
- After tests have been completed, ask user to select output file;
-- If exception --> error message, exit program;
-- Otherwise --> save results to file, exit program;
public void printToFile() {

    JFileChooser outputFile;
    File outFileName;

    outputFile = new JFileChooser();

    outputFile.showSaveDialog(null);
    outFileName = outputFile.getSelectedFile();
    try {
        PrintStream outFile = new PrintStream(outFileName);
        // print results to file
    } catch (Exception e) {
        System.out.print("Can't be saved to this file! \n");
        System.exit(0); // exit program
    }
}
我能想到的唯一解决方案是用以下代码替换
catch
块:

catch (Exception e) {
    System.out.print("Can't be saved to this file! \n");
    printToFile();
}
但是,如果用户不明白出了什么问题,他可能会反复选择同一个文件,从而创建一个(某种)无限循环。
是否有任何替代方法,或防止循环的方法?还是我想出的方法有帮助。。。捕获异常后,您可以向用户显示一条正确的消息,说明所选O/p文件有什么问题…

并且还制作了一个计数器,其背后的主要思想是用户选择2或3次错误的文件,然后程序将终止

这看起来像是一个无止境的循环,但事实并非如此。这是对外部条件的等待。 你想等多久取决于你自己。但在大多数情况下,只要允许用户取消,就不需要最大重试次数或超时。如果用户不想提供数据,他将按“取消”或终止您的应用程序。否则该应用程序将永远运行。你在乎吗

比较

while (needsUserData) {
   askForUserData()
}


只需不执行递归,您可能会运行stack space=)

为什么不使用while循环来捕获未找到文件时的条件,而不是System.exit()?谢谢,while循环确实是比递归更好的解决方案。如果没有更好的选择,这将是我的选择:)