简体   繁体   中英

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". 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. Given that you're calling it within an ActionListener , rethrowing the exception is not an option so you need to catch it.

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

creatNewAccount is declared as possibly throwing an IOException . IOException is not a RuntimeException , so you must catch it.

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 . - You're assigning something to BALANCE . Uppercase names are generally reserved for constants. Consider renaming this variable balance . - Consider more descriptive names for your text fields. name2 and name3 don't really say much.

In your actionPerformed() you need to put a try/catch block around the createNewAccount call. 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).

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
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM