A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 陈浩 黑马帝   /  2011-12-24 15:45  /  1838 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 love风之吻浩 于 2012-1-9 19:27 编辑

以下是我写管道流的代码:
public static void main(String []args)throws IOException
        {
                PipedInputStream in=new PipedInputStream();
                PipedOutputStream out=new PipedOutputStream();
                in.connect(out);
                write w=new write(out);
                Read  r=new Read(in);
                new Thread(w).start();
                new Thread(r).start();
               
        }
我想不直接通过抛出异常的方式解决异常问题,我尝试着写了以下的代码:
      public static void main(String []args)
        {
                PipedOutputStream out=null;
                PipedInputStream in=null;
               
                try{
                        out=new PipedOutputStream();
                        in=new PipedInputStream();
                        in.connect(out);
                        Xie1 x=new Xie1(out);
                        Du1 d=new Du1(in);
                        new Thread(x).start();
                        new Thread(d).start();
                }
                catch(IOException e){
                        throw new RuntimeException("读写失败");
                }
                finally
                {
                        try{
                               
                                if(in!=null)
                                        in.close();
                        }
                        catch(IOException e){
                                throw new RuntimeException("关闭失败!");
                        }
                                          try{
                               
                                if(out!=null)
                                        out.close();
                        }
                        catch(IOException e){
                                throw new RuntimeException("关闭失败!");
                        }
                }
        }
结果运行不了,老是报错,不知是何故,请诸位帮忙解答?

评分

参与人数 1技术分 +1 收起 理由
admin + 1

查看全部评分

2 个回复

倒序浏览
哪位兄弟可以帮帮忙啊!
回复 使用道具 举报
好吧,我的问题还是我的老师替我解决的,我处理方式不正确,真正处理异常的方法比较繁杂,不过我基本思路是对的
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马