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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

本帖最后由 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");
        }
}



4 个回复

正序浏览
chen_x 发表于 2014-3-21 10:11
int div(int a,int b)throw Exception
这句里应该用throws Exception, 不能用throw.

谢谢了  我粗心了 ,不过还学习到throw跟throws的区别了
回复 使用道具 举报
  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. }
复制代码


回复 使用道具 举报

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

你应该看了毕老师的视频了,在去看看!
回复 使用道具 举报
int div(int a,int b)throw Exception
这句里应该用throws Exception, 不能用throw.

throws用在函数定义中函数名称后面,声明成员函数可能抛出的异常类,throw是用在函数体内,抛出具体的异常对象。

评分

参与人数 1技术分 +1 收起 理由
zzkang0206 + 1

查看全部评分

回复 使用道具 举报 1 0
您需要登录后才可以回帖 登录 | 加入黑马