【问题标题】:Reading an URL when the button click unreported IO compilation error in Java按钮单击时读取 URL 未报告 Java 中的 IO 编译错误
【发布时间】:2012-10-23 10:59:05
【问题描述】:

我收到未报告的 IOException 错误尽管我构造了 try&catch 语句。

这个小程序试图调用一个 phpscript 。所以我必须在点击按钮时读取 URL。

我是java的新手。你能解释一下是什么问题吗

错误 44:IO 异常必须被捕获或声明被抛出

URLConnection myURLConnection = myURL.openConnection();

错误 46:IO 异常必须被捕获或声明为抛出

myURLConnection.connect();

import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;

public class RegisterUser extends Applet{


 //Applet components
TextField panel1 = new TextField(10);
TextField panel2 = new TextField(10);
TextField panel3 = new TextField(10);
Button    save   = new Button("Save");



public void init(){
   //There is two text fields and a button
    add(panel1);
    add(new Label("Name:"));
    addNewLine();
    add(panel2);
    add(new Label("Last:"));
    addNewLine();
    add(save);
    addNewLine();

    // Now tell the button  what it should do when it clicked

   save.addActionListener(new SaveListener());
  }

  class SaveListener implements ActionListener{

public void actionPerformed(ActionEvent event) {

                    try
          {

          URL myURL = new URL("http://www.myplace.com/save.php?name="+panel1.getText()+"&last_name="+panel2.getText()+"&email="+panel3.getText());

          URLConnection myURLConnection = myURL.openConnection();

          myURLConnection.connect();

          }

          }
          catch (MalformedURLException e){
          System.out.println(e.getMessage());
         }
    }
      }


      private void addHorizontalLine(Color c)
   {
   // Add a Canvas 10000 pixels wide but only 1 pixel high, which acts as
   // a horizontal line to separate one group of components from the next.
   Canvas line = new Canvas( );
  line.setSize(10000,1);
  line.setBackground(c);
  add(line);
  }


  private void addNewLine( )
 {
   // Add a horizontal line in the background color. The line itself is
   // invisible, but it serves to force the next Component onto a new line.
   addHorizontalLine(getBackground( ));
}

}

【问题讨论】:

  • 抱歉,您的代码中存在语法错误。更正它们并更新问题。

标签: java compiler-errors japplet


【解决方案1】:

你必须像这样抓住检查过的IOException

catch (IOException exc) {
  // handle exception
}

在您的 try catch 中,您仅捕获 MalformedURLException,这就是您收到编译错误的原因。

【讨论】:

  • 嘿嘿。回想起来,你的第一条评论几乎是喜剧。 ;) +1 的答案。
【解决方案2】:

您在try-block 之后的} 似乎太多了。

你还必须赶上IOException

这样写:

      try{
        URL myURL = new URL("http://www.myplace.com/save.php?name="+panel1.getText()+"&last_name="+panel2.getText()+"&email="+panel3.getText());
        URLConnection myURLConnection = myURL.openConnection();
        myURLConnection.connect();
      }catch (MalformedURLException e){
        System.out.println(e.getMessage());
      }catch (IOException e2){
        System.out.println(e2.getMessage());
      }

【讨论】:

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