黑马程序员技术交流社区

标题: 代码运行结果是什么?? [打印本页]

作者: 黑马李文    时间: 2013-5-29 09:52
标题: 代码运行结果是什么??
本帖最后由 黑马李文 于 2013-5-30 08:50 编辑

代码运行结果是什么??
  1. import java.io.*;

  2. public class MyThread extends Thread{
  3.         public static void main(String args[]){
  4.                 MyThread mt = new MyThread();
  5.                 mt.start();
  6.         }

  7.         public void run(){
  8.                 try {
  9.                              sleep(200);
  10.                              System.out.println
  11. ("Printing from thread run() method");
  12.                 } catch ( IOException e) {
  13.                         
  14.                 }
  15.         }
  16. }
复制代码

作者: 黄丽军    时间: 2013-5-29 10:10
应该是输出"Printing from thread run() method",可是不会抛出IOException异常吧,是不是该报"IO"去掉。
作者: 娄田田    时间: 2013-5-29 10:40
这段代码编译期是不会通过的,首先不会抛出IOException,会提示去掉这个异常,其次用到sleep方法时,需要捕获InterruptedException异常,要不然就抛出异常。
作者: 廖志强    时间: 2013-5-29 10:51
public class MyThread extends Thread{
        public static void main(String args[]){
                MyThread mt = new MyThread();
                mt.start();
        }

        public void run(){
                try {
                             sleep(200);
                             System.out.println
("Printing from thread run() method");
                } catch (InterruptedException e) {
                                e.printStackTrace();
                        }
        }
}
帮你修改了下,在catch这里有误
   



                        

                }

        }

}



作者: 刘勇强    时间: 2013-5-29 10:55
同楼上
编译同不过的
用到sleep方法,需要处理InterruptedException异常;
不会抛出IOException,你却catch了




把catch ( IOException e) 里的io异常换成  InterruptedException e 就可以了

输出就是:Printing from thread run() method
作者: 殇_心。    时间: 2013-5-29 16:44
如果问题已解决,请及时修改分类,否则继续提问,谢谢合作!




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2