当前位置: 首页 > news >正文

java throw throws_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 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 要是执行了则一定抛出了某种异常且后面的代码都不执行。

相关文章:

  • 2007年智能手机系统大战在即
  • java输入输出流课程_java day14第十四课 IO(输入、输出)流和JAVA目录操作
  • [领域]javascript hacking guide 第7部分
  • 浅谈Acegi配置
  • java valgrind_意外的内存泄漏[Valgrind]
  • 国内比较经典的图库资源网站
  • jstack 脚本 自动日志_Shell脚本实战:日志关键字监控+自动告警
  • 漫谈创业和管理-程序员5大思维障碍
  • mysql版本号字段比较大小_sqlserver中软件版本号进行字符串对比比较大小
  • 选择的自由
  • DSA and RBackupWindow
  • 慢连接 java_java nio 如何处理慢速的连接
  • 臭毛病从何而来
  • graphpad7.04多组比较p值_手把手教你用Graphpad做单因素方差分析
  • java查询sqlserver_JAVA实现对SQLServer增,删,改,查
  • [数据结构]链表的实现在PHP中
  • 3.7、@ResponseBody 和 @RestController
  • 5分钟即可掌握的前端高效利器:JavaScript 策略模式
  • Android系统模拟器绘制实现概述
  • ES2017异步函数现已正式可用
  • IDEA常用插件整理
  • js作用域和this的理解
  • macOS 中 shell 创建文件夹及文件并 VS Code 打开
  • miniui datagrid 的客户端分页解决方案 - CS结合
  • TCP拥塞控制
  • 前端之React实战:创建跨平台的项目架构
  • 使用iElevator.js模拟segmentfault的文章标题导航
  • JavaScript 新语法详解:Class 的私有属性与私有方法 ...
  • 昨天1024程序员节,我故意写了个死循环~
  • # 学号 2017-2018-20172309 《程序设计与数据结构》实验三报告
  • #Z0458. 树的中心2
  • #免费 苹果M系芯片Macbook电脑MacOS使用Bash脚本写入(读写)NTFS硬盘教程
  • #我与Java虚拟机的故事#连载10: 如何在阿里、腾讯、百度、及字节跳动等公司面试中脱颖而出...
  • (cos^2 X)的定积分,求积分 ∫sin^2(x) dx
  • (C语言)编写程序将一个4×4的数组进行顺时针旋转90度后输出。
  • (html转换)StringEscapeUtils类的转义与反转义方法
  • (Java实习生)每日10道面试题打卡——JavaWeb篇
  • (带教程)商业版SEO关键词按天计费系统:关键词排名优化、代理服务、手机自适应及搭建教程
  • (亲测有效)解决windows11无法使用1500000波特率的问题
  • (学习日记)2024.04.10:UCOSIII第三十八节:事件实验
  • (一)spring cloud微服务分布式云架构 - Spring Cloud简介
  • (一一四)第九章编程练习
  • (转)【Hibernate总结系列】使用举例
  • (转)创业的注意事项
  • (自适应手机端)响应式新闻博客知识类pbootcms网站模板 自媒体运营博客网站源码下载
  • .NET Framework Client Profile - a Subset of the .NET Framework Redistribution
  • .net oracle 连接超时_Mysql连接数据库异常汇总【必收藏】
  • .net 桌面开发 运行一阵子就自动关闭_聊城旋转门家用价格大约是多少,全自动旋转门,期待合作...
  • .net连接MySQL的方法
  • ??myeclipse+tomcat
  • @serverendpoint注解_SpringBoot 使用WebSocket打造在线聊天室(基于注解)
  • [2013][note]通过石墨烯调谐用于开关、传感的动态可重构Fano超——
  • [Android Pro] Notification的使用
  • [Android] 修改设备访问权限
  • [AutoSar NVM] 存储架构