Java 在尝试/捕获时不是100%确定
我被要求将带有抛出异常的代码转换为try/catch块。我已经设置好了它,但不确定要用什么来代替单词output,以便它可以运行。我不确定在阅读了这本书和oracles信息try/catch之后,我知道需要做什么才能打印txt文件。我将发布要修改的代码,然后使用try/catch进行更改。谢谢你的帮助Java 在尝试/捕获时不是100%确定,java,Java,我被要求将带有抛出异常的代码转换为try/catch块。我已经设置好了它,但不确定要用什么来代替单词output,以便它可以运行。我不确定在阅读了这本书和oracles信息try/catch之后,我知道需要做什么才能打印txt文件。我将发布要修改的代码,然后使用try/catch进行更改。谢谢你的帮助 public class WriteData { public static void main(String[] args) throws Exception { java.io
public class WriteData {
public static void main(String[] args) throws Exception {
java.io.File file = new java.io.File("scores.txt");
if (file.exists()) {
System.out.println("File already exists");
System.exit(0);
}
// Create a file
java.io.PrintWriter output = new java.io.PrintWriter(file);
// Write formatted output to the file
output.print("John T Smith ");
output.println(90);
output.print("Eric K Jones ");
output.println(85);
// Close the file
output.close();
}
}
下面是为Try/Catch更改的代码
import java.io.FileNotFoundException;
public class WriteData {
public static void main(String[] args) {
java.io.File file = new java.io.File("scores.txt");
try {
output = new java.io.PrintWriter(file);
} catch (FileNotFoundException ex) {
ex.printStackTrace();
}
// Create a file
java.io.PrintWriter output = new java.io.PrintWriter(file);
// Write formatted output to the file
output.print("John T Smith ");
output.println(90);
output.print("Eric K Jones ");
output.println(85);
// Close the file
output.close();
}
}
- 在不需要输出的情况下,将其实例化两次
- 所有与输出相关的处理都应该在try块中完成,这样,如果发生错误并且堆栈重定向到exception块,就不会执行try块
- 输出应该在finally块中,以确保无论发生什么情况,文件都已关闭
import java.io.*;
public class WriteData {
public static void main(String[] args) {
File file = null;
PrintWriter output = null;
try
{
file = new File("scores.txt");
output = new PrintWriter(file);
output.print("John T Smith ");
output.println(90);
output.print("Eric K Jones ");
output.println(85);
} catch (FileNotFoundException ex) {
ex.printStackTrace();
}
finally
{
//The output not be instanciated if scores.txt was not found.
if(output != null)
output.close();
}
}
}
在我看来,这是处理你案件的最好办法
try {
output = new java.io.PrintWriter(file); // output is not defined yet
} catch (FileNotFoundException ex) {
ex.printStackTrace();
}
// Create a file
// This one will throw the FileNotFoundException
java.io.PrintWriter output = new java.io.PrintWriter(file);
你可以这样修改它
try {
java.io.PrintWriter output = new java.io.PrintWriter(file);
//rest of the code
} catch (FileNotFoundException ex) {
ex.printStackTrace();
}
对于您的错误:
从jean-François Savard的解决方案中删除IoException的catch块
FileNotFoundException是PrintWriter()引发的已检查异常。作为实践,只捕获API签名中声明的异常
(事实上,保留任何一个块都应该有效,因为FileNotFoundException扩展了IOException)那么您的问题是什么?无法打印?将catch移动到方法的最后一行,只需将所有内容放入try中,但catch除外(包含堆栈跟踪打印)。我重新安排了@zgc7009,以便将所有内容都放入try with catch的末尾。现在我在output=newjava.io.PrintWriter(文件)上得到一个错误;错误是“输出”无法解析为变量。是的,这是有效的,除非存在带IOException的编译器错误。我还担心,由于这比我被问到的更能改变它,它可能超出了我的导师试图让我们得到的。错误是什么?我没有电脑自动取款机(我在iPad上)。。此外,我相信你的老师会很高兴看到你做的比他要求的更多(如果你确实理解的话)。如果有什么我可以澄清的,请告诉我。错误是“IOException的不可访问的catch块,只会抛出更多的特定异常,并且它们由prevoius catch块处理。”在CMD prompr中,它是不可访问的catch子句定义的。请注意,自Java7以来,可以在try/catch中自动处理资源。。。你应该读一些关于这个的教程。