萍聚社区-德国热线-德国实用信息网

 找回密码
 注册

微信登录

微信扫一扫,快速登录

萍聚头条

查看: 684|回复: 0

1-1-3-1-3 Exception?

[复制链接]
发表于 2003-2-4 22:28 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?注册 微信登录

x
发信人: rover (Eggplant), 信区: Java      
标  题: Re: thread programming please help, thank you
发信站: BBS 水木清华站 (Sat Sep  4 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[256];
:       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·[FROM: ab2-133.net.edu]
Die von den Nutzern eingestellten Information und Meinungen sind nicht eigene Informationen und Meinungen der DOLC GmbH.
您需要登录后才可以回帖 登录 | 注册 微信登录

本版积分规则

手机版|Archiver|AGB|Impressum|Datenschutzerklärung|萍聚社区-德国热线-德国实用信息网 |网站地图

GMT+2, 2024-5-21 15:32 , Processed in 0.050521 second(s), 16 queries , MemCached On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表