简体   繁体   English

我正在尝试通过按钮创建文件,但是我一直遇到错误

[英]I am trying to create a file via a button press but i keep running into an error

I am creating a checkbook and am unable to create a file to write to for each separate account. 我正在创建支票簿,并且无法为每个单独的帐户创建要写入的文件。 When I try to create the file I get the error "unreported exception IOException; must be caught or declared to be thrown". 当我尝试创建文件时,出现错误“未报告的异常IOException;必须捕获或声明为抛出”。 I try to declare that my action listener method throws an exception but that makes the action listener method no longer able to work. 我试图声明我的动作侦听器方法引发异常,但这会使动作侦听器方法不再起作用。 I then tried to create a separate method that creates the file and is called by the button press but i still run into the same error 然后,我尝试创建一个单独的方法来创建文件,并通过按按钮调用它,但是我仍然遇到相同的错误

Here is my code: 这是我的代码:

public void actionPerformed(ActionEvent e) {

    ...

    if (e.getSource() == create)  {
         creatNewAccount(name3.getText());
         BALANCE = Double.parseDouble(name2.getText());
    }
}
public void creatNewAccount(String s) throws IOException {
    FileWriter fw = new FileWriter(s + ".txt", false);
}

IOException is a checked exception. IOException是一个检查的异常。 Given that you're calling it within an ActionListener , rethrowing the exception is not an option so you need to catch it. 鉴于您是在ActionListener调用它,因此无法抛出异常,因此您需要捕获它。

try {
   creatNewAccount(name3.getText());
} catch (IOException e) {
   e.printStackTrace();
   // more exception handling
}

creatNewAccount is declared as possibly throwing an IOException . creatNewAccount被声明为可能抛出IOException IOException is not a RuntimeException , so you must catch it. IOException不是RuntimeException ,因此您必须捕获它。

if (e.getSource() == create)  {
     try {
         creatNewAccount(name3.getText());
     } catch (IOException ie) {
         ie.printStackTrace();
         // handle error
     }
     BALANCE = Double.parseDouble(name2.getText());
}

For more information, please read about The Catch or Specify Requirement and Catching and Handling Exceptions . 有关更多信息,请阅读有关捕获或指定需求以及捕获和处理异常的信息


A few other things I noticed: - The word you're looking for is create , not creat . 我注意到了其他一些事情:-您要查找的词是create ,而不是creat - You're assigning something to BALANCE . -您正在为BALANCE分配某些内容。 Uppercase names are generally reserved for constants. 大写名称通常为常量保留。 Consider renaming this variable balance . 考虑重命名此可变balance - Consider more descriptive names for your text fields. -为您的文本字段考虑更多描述性名称。 name2 and name3 don't really say much. name2name3并没有说太多。

In your actionPerformed() you need to put a try/catch block around the createNewAccount call. actionPerformed()您需要在createNewAccount调用周围放置try / catch块。 What you do with the exception once caught is up to you -- an easy thing to do is to wrap it in a RuntimeException which does not need to be caught (but might foul up your process until you do something more elaborate). 一旦捕获到异常,您要执行的操作就由您自己决定–一件容易的事是将其包装到不需要捕获的RuntimeException中(但可能会使您的过程RuntimeException ,直到您做一些更复杂的事情为止)。

public void actionPerformed(ActionEvent e) {

    ...

    if (e.getSource() == create)  {
         try {
             creatNewAccount(name3.getText());
         } catch( IOException ioe) {
             System.err.println("Whoops! " + ioe.getMessage());
             throw new RuntimeException("Unexpected exception", ioe);
         }
         BALANCE = Double.parseDouble(name2.getText());
    }
}

It's likely you'll just need to catch the exception inside the method: 您可能只需要在方法内部捕获异常:

public void creatNewAccount(String s) {
    try{
        FileWriter fw = new FileWriter(s + ".txt", false);
    } catch (IOException e){
        //TODO something to handle the error
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我在尝试创建 XML 文件时不断出错 - I keep getting error while trying to create an XML file 我试图在单击按钮时选择 PDF 文件,但出现错误 - I am trying to select PDF file on button click but I am getting an error 我正在尝试使用mulesoft通过Java代码将文件上传到文件夹,找不到404文件错误 - I am trying upload a file using mulesoft via Java code to a folder getting 404 file not found error 我正在创建一个2D游戏,我正在尝试导入一些字体,但我不断收到错误消息 - I am creating a 2d game, i am trying to import some fonts but i keep getting the error 当您使用Jquery按下按钮时,我试图使页面滚动速度变慢吗? - I am trying to make my page scroll slower when you press a button using Jquery? 按钮在我正在尝试创建的 android 基本 apo 中不起作用 - button not working in android basic apo I am trying to create 我正在尝试将一个值从 servlet 文件传递​​到 javascript 文件,但我一直为 null 我做错了什么? - I am trying to pass a value from servlet file to javascript file and i am keep getting null where am i doing wrong? 为什么当我按下“小程序”按钮时收到“抽象类”错误? - Why am I receiving an Abstract Class error when I press the Applet button? 我正在尝试创建一个秒表应用,但是在添加圈速按钮和继续按钮时遇到问题 - I am trying to create a Stopwatch app and am having problems adding a lap button and continue button that work 为什么在尝试将 Button 添加到 GridPane 时会收到错误消息? - Why am I receiving an error when trying to add a Button to a GridPane?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM