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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 奔跑的二叉树 中级黑马   /  2013-9-12 16:07  /  1452 人查看  /  13 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 奔跑的二叉树 于 2013-9-13 11:36 编辑
  1. class Demo10
  2. {
  3.         public static void main(String[] args)
  4.         {
  5.                 Demo ed=new Demo();        
  6.                 try{
  7.                         ed.add(10,-20);
  8.                 }catch(Exception e){
  9.                         System.out.println(e.getMessage());
  10.                 }
  11.         }
  12. }

  13. class Demo
  14. {        
  15.         public void add(int a,int b) throws Exception//这个方法要求两个数必须是正数.
  16.         {
  17.                 if(a<0||b<0)
  18.                 {
  19.                         throw new Fs("出现了负数");
  20.                 }
  21.                 int c=a+b;
  22.                 System.out.println(c);
  23.         }
  24. }

  25. class Fs extends Exception
  26. {
  27.         public Fs(String message)
  28.         {
  29.                 super(message);
  30.         }
  31. }


  32. */
复制代码
郁闷了,怎么他就编译不通过呢,找不到原因

评分

参与人数 1技术分 +1 收起 理由
黄兴旺 + 1

查看全部评分

13 个回复

倒序浏览
编译通过了啊。出现负数啊。
回复 使用道具 举报
我试了一下,可以正确编译啊??
回复 使用道具 举报

LooK,这是啥问题啊
回复 使用道具 举报

你把*/去掉吧。。。。

评分

参与人数 1技术分 +1 收起 理由
黄兴旺 + 1

查看全部评分

回复 使用道具 举报

  1. /*
  2. * 老师用电脑上课
  3. * 思考上课中出现的问题。
  4. *
  5. * 比如问题是
  6. * 电脑蓝屏
  7. * 电脑冒烟
  8. * 要对问题进行描述,封装成对象
  9. *
  10. * 出现了讲师的问题,课时计划完不成
  11. */
  12. public class ExceptionDemo {

  13.         public static void main(String[] args) {
  14.                 Teacher t=new Teacher("毕老师");
  15.                 try
  16.                 {
  17.                         t.prelect();
  18.                 }
  19.                 catch(NoPlanException e)
  20.                 {
  21.                         System.out.println(e.toString());
  22.                         System.out.println("换老师或者放假");
  23.                 }

  24.         }

  25. }
  26. class NoplanException extends Exception
  27. {
  28.         NoplanException(String message)
  29.         {
  30.                 super(message);
  31.         }
  32. }
  33. class LanPingException extends Exception
  34. {
  35.         LanPingException(String message)
  36.         {
  37.                 super(message);
  38.         }
  39. }
  40. class MaoYanException extends Exception
  41. {
  42.         MaoYanException(String message)
  43.         {
  44.                 super(message);
  45.         }
  46. }
  47. class Computer
  48. {
  49.         private int state=1;
  50.         public void run()throws LanPingException,MaoYanException
  51.         {
  52.                 if(state==2)
  53.                         throw new LanPingException("蓝屏了");
  54.                 if(state==3)
  55.                         throw new MaoYanException("冒烟了");
  56.                 System.out.print("电脑运行");
  57.         }
  58.         public void reset()
  59.         {
  60.                 state=1;
  61.                 System.out.println("电脑重启");
  62.         }
  63. }
  64. class Teacher
  65. {
  66.         private String name;
  67.         private Computer cmpt;
  68.         Teacher(String name)
  69.         {
  70.                 this.name=name;
  71.                 cmpt=new Computer();
  72.         }
  73.         public void prelect()
  74.         {
  75.                 try
  76.                 {
  77.                         cmpt.run();
  78.                        
  79.                 }
  80.                 catch(LanPingException e)
  81.                 {
  82.                         cmpt.reset();
  83.                 }
  84.                 catch(MaoYanException e)
  85.                 {
  86.                         test();
  87.                         throw new NoPlanException("课时无法继续"+e.getMessage());
  88.                        
  89.                 }
  90.                 System.out.print("讲课");
  91.                
  92.         }
  93.         public void test()
  94.         {
  95.                 System.out.println("练习");
  96.         }
  97. }

复制代码
这个也有好多提示的错误
回复 使用道具 举报
没有问题呀,
运行出来了呀,结果是出现了负数
回复 使用道具 举报
throw new NoPlanException("课时无法继续"+e.getMessage()),那里有个错误,是什么原因呢
回复 使用道具 举报
奔跑的二叉树 发表于 2013-9-12 16:39
throw new NoPlanException("课时无法继续"+e.getMessage()),那里有个错误,是什么原因呢 ...

应该要在函数那儿声明下
回复 使用道具 举报
黄炳期 发表于 2013-9-12 17:13
应该要在函数那儿声明下

怎么声明?catch那里不需要声明的吧
回复 使用道具 举报
Zinmm 中级黑马 2013-9-13 04:58:16
11#
36行有个注释符,去掉就ok了,或者在前面加上/**,虽然会成为垃圾代码

评分

参与人数 1技术分 +1 收起 理由
杨增坤 + 1

查看全部评分

回复 使用道具 举报
奔跑的二叉树 发表于 2013-9-12 17:22
怎么声明?catch那里不需要声明的吧

  • public void prelect() throw NoPlanException
  •         {
  •                 try
  •                 {
  •                         cmpt.run();
  •                 }
  •                 catch(LanPingException e)
  •                 {
  •                         cmpt.reset();
  •                 }
  •                 catch(MaoYanException e)
  •                 {
  •                         test();
  •                         throw new NoPlanException("课时无法继续"+e.getMessage());
  •                 }
  •                 System.out.print("讲课");
  •         }

回复 使用道具 举报
杨增坤 发表于 2013-9-13 08:41
  • public void prelect() throw NoPlanException
  •         {
  •                 try



  • 不对呀
    回复 使用道具 举报

    找到问题了,写错字了,我晕!
    回复 使用道具 举报
    您需要登录后才可以回帖 登录 | 加入黑马