Java面试题 (2) Java中 throw 和 throws 的区别?

Java面试题 :throw 和 throws 的区别 ?

Java 中抛出异常有3种方式,分别是 throw throws 系统自动抛出。

系统自动抛出:

当开发人员自己编写代码出现异常(主义错误,逻辑错误,转义错误),该异常如果自己没捕捉,则系统会自动抛出 。系统会把异常所在的位置,异常的名称,异常的原因 等 信息打印在 控制台中,并终止程序往下执行。


throw :

throw是语句中抛出异常,一般都是在代码块中,当程序中某种逻辑错误时由开发人员主动抛出自己指定类型异常。创建的是一个异常对象,确定某种异常才能使用,定义在方法体内,必须搭配 thy / catch 或者 throws 一起使用。

栗子:

搭配 thy / catch 使用

try{
   int effectedNum = productCategoryDao.batchInsertProductCategory(productCategoryList);
   if (effectedNum <=0){
       throw  new ProductCategoryOperationException("店铺类别创建失败");
    }else {
       return new ProductCategoryExecution(ProductCategoryStateEnum.SUCCESS);
     }
  }catch (Exception e){
         hrow new ProductCategoryOperationException("batchInsertProductCategory error"+e.getMessage());
  }

搭配 throws 使用

public ProductCategoryExecution deleteProductCategory(long productCategoryId, long shopId) throws ProductCategoryOperationException {
        try{
            int effectedNum = productDao.updateProductCategoryToNull(productCategoryId);
            if (effectedNum < 0){
                throw new RuntimeException("商品类别更新失败");
            }
        }catch (Exception e){
            throw new RuntimeException("deleteProductCategory error:"+e.getMessage());
        }
        //将此商品类别下的商品的类别id置空
        try{
            int effectedNum = productCategoryDao.deleteProductCategory(productCategoryId,shopId);
            if (effectedNum <=0){
                throw  new ProductCategoryOperationException("商品类别删除失败");
            }else {
                return new ProductCategoryExecution(ProductCategoryStateEnum.SUCCESS);
            }
        }catch (Exception e){
            throw new ProductCategoryOperationException("deleteProductCategory error:"+e.getMessage());
        }
    }

throws :

在方法参数后(方法头的位置),throws 可以 跟着 多个异常名,多个抛出的异常用逗号隔开。
表示在调用该方法的类中抛出异常(可以理解往上抛),不在该类中解决,预计这个方法可能出现的异常。

栗子:

public ProductCategoryExecution batchInsertProductCategory(List<ProductCategory> productCategoryList) throws ProductCategoryOperationException,RuntimeException {
        if (productCategoryList!=null && productCategoryList.size()>0){
            try{
                int effectedNum = productCategoryDao.batchInsertProductCategory(productCategoryList);
                if (effectedNum <=0){
                    throw  new ProductCategoryOperationException("店铺类别创建失败");
                }else {
                    return new ProductCategoryExecution(ProductCategoryStateEnum.SUCCESS);
                }
            }catch (Exception e){
                throw new ProductCategoryOperationException("batchInsertProductCategory error"+e.getMessage());
            }
        }else {
            return new ProductCategoryExecution(ProductCategoryStateEnum.EMPTY_LIST);
        }
    }

附:

1)throws 用在方法声明后,跟在后面的是异常类名 (Execution ),throw 用在方法体内,跟的是异常的对象名 (new Execution)。
2)throws 后可以跟多个异常类名,用逗号隔开;而 throw 只能抛出一个异常对象名。
3)throws 表示抛出的异常由该方法调用者处理,throw表示抛出的异常由自己处理(定义方法体内)
4)throws 表示会出现异常的一种可能性,不一定会发生该异常;throw 要是执行了则一定抛出了某种异常且后面的代码都不执行。

  • 8
    点赞
  • 32
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

不像程序猿的程序员

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值