黑马程序员技术交流社区

标题: 哪里出错了,总是找不到符号呢 [打印本页]

作者: gaigai7    时间: 2014-5-2 16:09
标题: 哪里出错了,总是找不到符号呢
class FuShuIndexException extends Exception
{
}
class FuArray
{
        public int method(int[] arr , int index) throws NullPointerException,FuShuIndexException
        {
                if(arr == null)
                        throw new NullPointerException();
                if(index < 0)
                        throw new FuShuIndexExcepion();
                return arr[index];
        }
}
class ExceptionDemo
{
        public static void main(String[] args)
        {
                int[] arr = new int[3];
                FuArray fy = new FuArray();
                try
                {
                        int num = fy.method(null,-1);
                        System.out.println("num = " + num);
                }
                catch(NullPointerException e)
                {
                        System.out.println(e.toString());
                        System.out.println("has a problem");
                }
                catch(FuShuIndexException e)
                {
                        System.out.println("string:" + e.toSring());
                        System.out.println("Fushuindexexception");
                }
        }
}

作者: XinWen    时间: 2014-5-2 16:26
哪里 找不到 符号 , 把错误 贴上来看看
作者: gaigai7    时间: 2014-5-2 16:52
XinWen 发表于 2014-5-2 16:26
哪里 找不到 符号 , 把错误 贴上来看看



作者: hong云梦    时间: 2014-5-2 17:14
FuShuIndexException 这个是你自己定义的Exception  要把它引入进来
作者: skill20    时间: 2014-5-2 17:15
本帖最后由 skill20 于 2014-5-2 17:16 编辑
  1. class FuShuIndexException extends Exception
  2. {
  3. }
  4. class FuArray
  5. {
  6.         public int method(int[] arr , int index) throws NullPointerException,FuShuIndexException
  7.         {
  8.                 if(arr == null)
  9.                         throw new NullPointerException();
  10.                 if(index < 0)
  11.                         throw new FuShuIndexExcepion();
  12.                 return arr[index];
  13.         }
  14. }
  15. class ExceptionDemo
  16. {
  17.         public static void main(String[] args)
  18.         {
  19.                 int[] arr = new int[3];
  20.                 FuArray fy = new FuArray();
  21.                 try
  22.                 {
  23.                         int num = fy.method(null,-1);
  24.                         System.out.println("num = " + num);
  25.                 }
  26.                 catch(NullPointerException e)
  27.                 {
  28.                         System.out.println(e.toString());
  29.                         System.out.println("has a problem");
  30.                 }
  31.                 catch(FuShuIndexException e)
  32.                 {
  33.                         System.out.println("string:" + e.toSring()); //e.toString( );
  34.                         System.out.println("Fushuindexexception");
  35.                 }
  36.         }
  37. }
复制代码

作者: 张然龙    时间: 2014-5-2 17:58
两处错误直接给你指出来了 ,你自己不认真看而已,一般这种情况都是你自己代码写错了。。看注释
代码如下↓

  1. class FuShuIndexException extends Exception
  2. {
  3. }
  4. class FuArray
  5. {
  6.         public int method(int[] arr , int index) throws NullPointerException,FuShuIndexException
  7.         {
  8.                 if(arr == null)
  9.                         throw new NullPointerException();
  10.                 if(index < 0)
  11.                         throw new FuShuIndexException();//你发上来的代码这个位置少写个t,所以他认为这个类没有被定义过
  12.                 return arr[index];
  13.         }
  14. }
  15. class ExceptionDemo
  16. {
  17.         public static void main(String[] args)
  18.         {
  19.                 int[] arr = new int[3];
  20.                 FuArray fy = new FuArray();
  21.                 try
  22.                 {
  23.                         int num = fy.method(null,-1);
  24.                         System.out.println("num = " + num);
  25.                 }
  26.                 catch(NullPointerException e)
  27.                 {
  28.                         System.out.println(e.toString());
  29.                         System.out.println("has a problem");
  30.                 }
  31.                 catch(FuShuIndexException e)
  32.                 {
  33.                         System.out.println("string:" + e.toString());//你发上来的代码这里少写个t,
  34.                         System.out.println("Fushuindexexception");
  35.                 }
  36.         }
  37. }
复制代码


作者: gaigai7    时间: 2014-5-2 18:04
张然龙 发表于 2014-5-2 17:58
两处错误直接给你指出来了 ,你自己不认真看而已,一般这种情况都是你自己代码写错了。。看注释
代码如下↓ ...

囧啊  我自己也看了好几遍  就是没看到
作者: 张然龙    时间: 2014-5-2 19:30
gaigai7 发表于 2014-5-2 18:04
囧啊  我自己也看了好几遍  就是没看到

O(∩_∩)O哈哈~
作者: 小周务商    时间: 2014-5-2 19:58
哈哈。我以前也经常犯这种错误的。
作者: 心?=忐§忑]    时间: 2014-5-2 20:47
  1. @SuppressWarnings("serial")   //压制警告
  2. class FuShuIndexException extends Exception
  3. {
  4. }
  5. class FuArray
  6. {
  7.          public int method(int[] arr , int index) throws NullPointerException,FuShuIndexException
  8.          {
  9.                  if(arr == null)
  10.                          throw new NullPointerException();
  11.                  if(index < 0)
  12.                          throw new FuShuIndexException();  //类名写错了
  13.                  return arr[index];
  14.          }
  15. }
  16. public class Demo10
  17. {
  18.          public static void main(String[] args)
  19.          {
  20.                  int[] arr = new int[3];
  21.                  FuArray fy = new FuArray();
  22.                  try
  23.                  {
  24.                          int num = fy.method(null,-1);
  25.                          System.out.println("num = " + num);
  26.                  }
  27.                  catch(NullPointerException e)
  28.                  {
  29.                          System.out.println(e.toString());
  30.                          System.out.println("has a problem");
  31.                  }
  32.                  catch(FuShuIndexException e)
  33.                  {
  34.                          System.out.println("string:" + e.toString());  //toString方法写错了
  35.                          System.out.println("Fushuindexexception");
  36.                  }
  37.          }
  38. }
复制代码

作者: 362688114    时间: 2014-5-2 21:27
在注释中已经给你标出来了,都是发放名或类名写错了。
这些都是小错误,仔细看就能发现了。

  1. class FuShuIndexException extends Exception
  2. {
  3. }


  4. class FuArray
  5. {
  6.          public int method(int[] arr , int index) throws NullPointerException,FuShuIndexException
  7.          {
  8.                  if(arr == null)
  9.                          throw new NullPointerException();
  10.                  if(index < 0)
  11.                          throw new FuShuIndexException(); //这个类名少写一个‘t’;
  12.                  return arr[index];
  13.          }
  14. }


  15. class ExceptionDemo
  16. {
  17.          public static void main(String[] args)
  18.          {
  19.                  int[] arr = new int[3];
  20.                  FuArray fy = new FuArray();
  21.                  try
  22.                  {
  23.                          int num = fy.method(null,-1);
  24.                          System.out.println("num = " + num);
  25.                  }
  26.                  catch(NullPointerException e)
  27.                  {
  28.                          System.out.println(e.toString());
  29.                          System.out.println("has a problem");
  30.                  }
  31.                  catch(FuShuIndexException e)
  32.                  {
  33.                          System.out.println("string:" + e.toString()); //这里又是少写个‘t’,方法名写错;
  34.                          System.out.println("Fushuindexexception");
  35.                  }
  36.          }
  37. }
复制代码





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