[英]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. name2
和name3
并没有说太多。
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.