黑马程序员技术交流社区

标题: 看完异常的视频,随着敲了下代码,出来问题不知道什么... [打印本页]

作者: NewOnce`    时间: 2014-3-21 10:02
标题: 看完异常的视频,随着敲了下代码,出来问题不知道什么...
本帖最后由 NewOnce` 于 2014-3-26 10:28 编辑

class Demo
{
        int div(int a,int b)throw Exception
        {
                return a/b;
        }
}        
class  ExceptionDemo1
{
        public static void main(String[] args)
        {
                Demo d= new Demo();
                try
                {
                        int x=d.div(4,0);
                System.out.println("x="+x);
                        
                }
                catch (Exception e)
                {
                        System.out.println("除零了"+e.toString());

                }
                System.out.println("Over");
        }
}




作者: chen_x    时间: 2014-3-21 10:11
int div(int a,int b)throw Exception
这句里应该用throws Exception, 不能用throw.

throws用在函数定义中函数名称后面,声明成员函数可能抛出的异常类,throw是用在函数体内,抛出具体的异常对象。
作者: zzkang0206    时间: 2014-3-21 10:12

class Demo
{
        int div(int a,int b)throw Exception
        {
                return a/b;
        }
}   
throw Exception 这里的throw缺少s,throw是定义在函数里面的,throws才是定义在函数上的。

你应该看了毕老师的视频了,在去看看!
作者: 蓝枫    时间: 2014-3-21 10:25
  1. class Demo
  2. {
  3.      int div(int a,int b)throws Exception
  4.      {
  5.         return a/b;
  6.      }
  7. }        
  8. class  ExceptionDemo1
  9. {
  10.     public static void main(String[] args)
  11.     {
  12.      Demo d= new Demo();
  13.       try
  14.       {
  15.         int x=d.div(4,0);
  16.         System.out.println("x="+x);
  17.                         
  18.       }
  19.         catch (Exception e)
  20.       {
  21.          System.out.println("除零了"+e.toString());

  22.       }
  23.       System.out.println("Over");
  24.     }
  25. }
复制代码



作者: NewOnce`    时间: 2014-3-21 10:29
chen_x 发表于 2014-3-21 10:11
int div(int a,int b)throw Exception
这句里应该用throws Exception, 不能用throw.

谢谢了  我粗心了 ,不过还学习到throw跟throws的区别了




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