【问题标题】:How can I delete a Exception handler? [closed]如何删除异常处理程序? [关闭]
【发布时间】:2012-10-25 03:04:48
【问题描述】:

我想从这段代码中删除异常处理程序,因为我不明白它存在的原因,而且我相信它在我的代码阶段没有做任何事情。 我有这个:

public void ToFile(){
               try{
                   PrintWriter pw = new PrintWriter(new FileWriter(file, false));

                    for (String st:stringarray){ 
                          pw.println(st);
                          pw.close();
                            }
                    }catch(Exception exception)
                    {}
            }

当我删除异常部分时,我将其转换为这个,但它给了我错误...:

public void ToFile(){

                            PrintWriter pw = new PrintWriter();
                            for (String st:items){ 
                                    pw.println(srtingarray);
                                    pw.close();
                            }

            }

我该怎么办? 编辑 错误: 没有找到适合 PrintWriter 的构造函数。

【问题讨论】:

  • 处理异常是一个好习惯。为什么要删除该代码?
  • 你得到什么错误
  • "What should I do ? EDIT ERROR: no suitable constructor found for PrintWriter." 我会查看 PrintWriter API 并仅使用适当的构造函数。您将需要阅读我正在考虑的 Java 教程中的异常教程。您还需要了解介绍/基础教程。
  • 我看不出一个人多次调用 close() 方法的任何原因。任何时候st 有多个元素都会抛出异常。这是很奇怪的代码。
  • "because I dont understand exactly what exceptions do, I want to delete them." 这有点像去找汽车修理工说:“我不知道如何使用我车上的刹车。你能把它们去掉吗?”答案是学习如何使用你的休息时间(例外)。请转至this link to the Java Tutorials 并搜索例外部分。

标签: java exception-handling


【解决方案1】:

试试这样的:

public void toFile() throws IOException {
    PrintWriter pw = new PrintWriter(new FileWriter(file, false));

    for (String str: items){ 
        pw.println(str);
    }

    pw.close();
}

写入失败时使用PrintWriter可以生成IOExceptions。你可以在你的函数中捕获并处理它们,或者添加一个throws 子句,允许异常向上传播到调用者。

(我还将close() 调用移到循环之外,并修正了您如何使用pw。)

知道添加throws 子句并不能完全解决您的问题,因为调用者仍需要处理异常。它只是将问题提升了一个级别。不过,这是正确的做法。 没有办法完全忽略IOException。这些函数可能会出错,Java 会强制您在某个时候处理这​​些错误。这是好事

如果您想在这里处理异常,您可以这样做。在catch 子句中做something 是个好主意。看到 catch(Exception e) { } 和一个空的处理程序让婴儿 Knuth 很难过。

public void toFile() {
    try {
        PrintWriter pw = new PrintWriter(new FileWriter(file, false));

        for (String str: items){ 
            pw.println(str);
        }

        pw.close();
    }
    catch (IOException exception) {
        exception.printStackTrace();
    }
}

【讨论】:

  • 我现在应该如何从 main 调用 ToFile?我提出并反对MyObject,在我写MyObject.ToFile();之后,它告诉我我没有处理异常。
【解决方案2】:

异常处理程序正在“处理”1当文件无法打开时new FileWriter(file, false) 可能抛出的 IOException。如果你不打算处理它,你必须在方法签名中声明它:

public void toFile() throws IOException {
    PrintWriter pw = new PrintWriter(new FileWriter(file, false));
    for (String st : items) { 
        pw.println(st);
    }
    pw.close();
}

我也借此机会修正了方法名称。永远不要以大写字母开头的方法名称。这是一个重大的风格错误。

我还修复了另外 2 个编译错误和一个错误。 (看看你能不能发现它......并找出它为什么是一个错误。)

最后,真的正确的写法是:

// Java 7
public void toFile() throws IOException {
    try (PrintWriter pw = new PrintWriter(new FileWriter(file, false))) {
        for (String st : items) { 
            pw.println(st);
        }
    }
}

// pre-Java 7
public void toFile() throws IOException {
    PrintWriter pw = new PrintWriter(new FileWriter(file, false))) {
    try {
        for (String st : items) { 
            pw.println(st);
        }
    } finally {
        pw.close();
    }
}

您需要这样做,以便FileWriter 始终关闭......即使在循环中引发了意外异常。


1 - 事实上,原来的catch (Exception exception) { } 真的很糟糕。首先,它只是狼吞虎咽地吃掉异常,什么也没说。其次,它是为Exception ... 这样做的,其中包括一大堆异常,例如NullPointerException,这很可能是一个错误的证据。不要压制异常,尤其不要尝试像那样“处理”Exception

【讨论】:

  • 正确关闭作者的好点。我没有回答这个问题,我很高兴它在你的回答中。
  • 我现在应该如何从 main 调用 ToFile?我创建并反对 MyObject ,在我写 MyObject.ToFile(); 之后它告诉我我没有处理异常。
  • @georgemano - 您需要处理main 中的异常...或将main 声明为throws IOExcption。但在您这样做之前,请阅读 Oracle 的 Java 教程。从长远来看,您将为自己节省大量时间和烦恼。
【解决方案3】:

异常的原因是在循环的第一次迭代之后,您正在关闭 PrintWriter 对象。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-26
    • 2020-03-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多