简体   繁体   English

actionlistener上未报告的异常java.io.IOException

[英]unreported exception java.io.IOException at actionlistener

I am new to java and I am trying to make it to when a button is pushed, it will update the new information that was put in the table. 我是Java的新手,我试图使其在按下按钮时生效,它将更新表中放置的新信息。 I am getting this error: 我收到此错误:

unreported exception java.io.IOException; must be caught or declared to be thrown

Here the code I'm having trouble in: 这是我遇到麻烦的代码:

public static void updateAction(){

update.addActionListener(new ActionListener() {


 @Override
 public void actionPerformed(ActionEvent e) {
 BufferedWriter bfw = new BufferedWriter(new FileWriter(tmp));
 for(int i = 0 ; i < table.getColumnCount() ; i++)
 {
 bfw.write(table.getColumnName(i));
 bfw.write("\t");
 }

 for (int i = 0 ; i < table.getRowCount(); i++)
 {
 bfw.newLine();
 for(int j = 0 ; j < table.getColumnCount();j++)
 {
 bfw.write((String)(table.getValueAt(i,j)));
 bfw.write("\t");;
 }


 }
  bfw.close();

 }});     
}

Thanks for any help you can give me. 感谢你给与我的帮助。

The methods of BufferedWriter throw an IOException . BufferedWriter的方法抛出IOException You must either catch it in your method body or declare your method to throw it. 您必须将其捕获在方法主体中或声明您的方法以将其抛出。

Since you are using an anonymous implementation of ActionListener , you can't change the signature of actionPerformed . 由于您使用的是ActionListener的匿名实现,因此无法更改actionPerformed的签名。 So you must catch the IOException inside of actionPerformed . 因此,您必须在actionPerformed内部捕获IOException

You should catch like this 你应该这样抓

try {
   ....
} catch(IOException e) {

}

or throws IOException at 或将IOException抛出

public void actionPerformed(ActionEvent e) throws IOException

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

相关问题 未报告的异常java.io.IOException - unreported exception java.io.IOException 编译Java代码时出现未报告的异常java.io.IOException - Unreported exception java.io.IOException when compiling Java code Java类中的错误“必须捕获或声明抛出未报告的异常java.io.ioexception” - Error “unreported exception java.io.ioexception must be caught or declared to be thrown” in Java class 错误消息“未报告的异常 java.io.IOException;必须被捕获或声明为抛出” - Error message "unreported exception java.io.IOException; must be caught or declared to be thrown" 错误:未报告的异常java.io.IOException; 必须被抓住或宣布被抛出 - ERROR: unreported exception java.io.IOException; must be caught or declared to be thrown 未处理的异常java.io.iOException - Unhandled Exception java.io.iOException java.io.IOException java - java.io.IOException java 无法解决异常java.io.IOException:URL的403 - unable to resolve the exception java.io.IOException: 403 for URL java.io.IOException:使用 SpringBoot 写入 Multipart 时出现异常 - java.io.IOException: Exception writing Multipart with SpringBoot 线程“main”中的异常 java.io.IOException:作业失败! 在 mapreduce 中 - Exception in thread "main" java.io.IOException: Job failed! in mapreduce
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM