文选流氓 发表于 2003-2-4 21:28

1-1-3-1-3 Exception?

发信人: rover (Eggplant), 信区: Java      
标题: Re: thread programming please help, thank you
发信站: BBS 水木清华站 (Sat Sep4 15:40:50 1999)

【 在 waterbird (爱来不来) 的大作中提到: 】
:I want to use datagramsocket in a thread, the foliowing is client code,
: It can't pass compiling, reports the following error information:
: Exception java.net.SocketException must be caught, or it must be declared in
:the throws clause of this method.
:   Please help me to check the mistakes, thank you very much!
      这个错误的意思就是说Java要求你把所有可能抛出Exception 的语句都
要放在
      try
      {
      ....(这里就是那些可能抛出Exception的语句)
      }
      catch (Exception e)
      {
      ....(这里就写上那些对Exception的处理,如果你是新手可以先不管这里)
      }
上面这样的结构里,否则Java编译器是不会放过你的。

你的程序里面那些和网络操作相关的都将抛出异常,你看看文档和出错提示就
可以知道哪些是抛出异常的语句了。

: *****code******
:public class SendingThread_Client extends Thread {
:    protected DatagramSocket ssock_client = null;
:    protected String clientname = "bird";
:    protected String server_address = null;
:    protected InetAddress server_ip = null;
:    public SendingThread_Client(String addrs) throws IOException {
:       server_address = addrs;
:       start();
:    };
:    public void run() {
:       System.out.println("Sending Thread on server side starts...");
:       System.out.println("Client's name is " + this.clientname);
:       System.out.println("Server is " + this.server_address);
:       ssock_client = new DatagramSocket();
:       byte[] buf = new byte;
:       buf = clientname.getBytes();
:       InetAddress server_ip = InetAddress.getByName(this.server_address);
:       DatagramPacket packet = new DatagramPacket
:                                        (buf, buf.length, server_ip, 6666);
:       ssock_client.send(packet);
:    }
: }


--
※ 来源:·BBS 水木清华站 bbs.net.tsinghua.edu.cn·
页: [1]
查看完整版本: 1-1-3-1-3 Exception?