黑马程序员技术交流社区

标题: 自定义异常的问题 [打印本页]

作者: 奔跑的二叉树    时间: 2013-9-12 16:07
标题: 自定义异常的问题
本帖最后由 奔跑的二叉树 于 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. */
复制代码
郁闷了,怎么他就编译不通过呢,找不到原因

作者: 一碗小米周    时间: 2013-9-12 16:22
编译通过了啊。出现负数啊。
作者: Yuan先生    时间: 2013-9-12 16:23
我试了一下,可以正确编译啊??
作者: 奔跑的二叉树    时间: 2013-9-12 16:28

LooK,这是啥问题啊

作者: 一碗小米周    时间: 2013-9-12 16:29
奔跑的二叉树 发表于 2013-9-12 16:28
LooK,这是啥问题啊

你把*/去掉吧。。。。

作者: 奔跑的二叉树    时间: 2013-9-12 16:32

  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. }

复制代码
这个也有好多提示的错误

作者: 代文娟    时间: 2013-9-12 16:34
没有问题呀,
运行出来了呀,结果是出现了负数
作者: 奔跑的二叉树    时间: 2013-9-12 16:39
throw new NoPlanException("课时无法继续"+e.getMessage()),那里有个错误,是什么原因呢
作者: 黄炳期    时间: 2013-9-12 17:13
奔跑的二叉树 发表于 2013-9-12 16:39
throw new NoPlanException("课时无法继续"+e.getMessage()),那里有个错误,是什么原因呢 ...

应该要在函数那儿声明下
作者: 奔跑的二叉树    时间: 2013-9-12 17:22
黄炳期 发表于 2013-9-12 17:13
应该要在函数那儿声明下

怎么声明?catch那里不需要声明的吧

作者: Zinmm    时间: 2013-9-13 04:58
36行有个注释符,去掉就ok了,或者在前面加上/**,虽然会成为垃圾代码
作者: 杨增坤    时间: 2013-9-13 08:41
奔跑的二叉树 发表于 2013-9-12 17:22
怎么声明?catch那里不需要声明的吧



作者: 奔跑的二叉树    时间: 2013-9-13 11:29
杨增坤 发表于 2013-9-13 08:41
  • public void prelect() throw NoPlanException
  •         {
  •                 try



  • 不对呀

    作者: 奔跑的二叉树    时间: 2013-9-13 11:34
    奔跑的二叉树 发表于 2013-9-13 11:29
    不对呀

    找到问题了,写错字了,我晕!




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