将IllegalArgumentException视为先决条件检查,并考虑设计原则:公共方法应该知道并公开记录其自身的前提条件。

我同意这个例子是正确的:

void setPercentage(int pct) {

if( pct < 0 || pct > 100) {

throw new IllegalArgumentException("bad percent");

}

}

If EmailUtil is opaque, meaning there's some reason the preconditions cannot be described to the end-user, then a checked exception is correct. The second version, corrected for this design:

import com.someoneelse.EmailUtil;

public void scanEmail(String emailStr, InputStream mime) throws ParseException {

EmailAddress parsedAddress = EmailUtil.parseAddress(emailStr);

}

If EmailUtil is transparent, for instance maybe it's a private method owned by the class under question, IllegalArgumentException is correct if and only if its preconditions can be described in the function documentation. This is a correct version as well:

/** @param String email An email with an address in the form abc@xyz.com

* with no nested comments, periods or other nonsense.

*/

public String scanEmail(String email)

if (!addressIsProperlyFormatted(email)) {

throw new IllegalArgumentException("invalid address");

}

return parseEmail(emailAddr);

}

private String parseEmail(String emailS) {

// Assumes email is valid

boolean parsesJustFine = true;

// Parse logic

if (!parsesJustFine) {

// As a private method it is an internal error if address is improperly

// formatted. This is an internal error to the class implementation.

throw new AssertError("Internal error");

}

}

这种设计可以采用两种方式。

If preconditions are expensive to describe, or if the class is intended to be used by clients who don't know whether their emails are valid, then use IllegalArgumentException. The top level method here is named scanEmail which hints the end user intends to send unstudied email through so this is likely correct.

If preconditions can be described in function documentation, and the class does not intent for invalid input and therefore programmer error is indicated, use IllegalArgumentException. Although not "checked" the "check" moves to the Javadoc documenting the function, which the client is expected to adhere to. IllegalArgumentException where the client can't tell their argument is illegal beforehand is wrong.

A note on IllegalStateException: This means "this object's internal state (private instance variables) is not able to perform this action." The end user cannot see private state so loosely speaking it takes precedence over IllegalArgumentException in the case where the client call has no way to know the object's state is inconsistent. I don't have a good explanation when it's preferred over checked exceptions, although things like initializing twice, or losing a database connection that isn't recovered, are examples.

java异常什么时候抛出异常,java - 什么时候应该抛出IllegalArgumentException?相关推荐

  1. java异常怎么输出到页面_怎么把抛出的异常信息输出到SWING 界面上

    展开全部 首先抛出异常e68a84e8a2ad3231313335323631343130323136353331333337623366,然后捕获异常,最后把异常信息输出到SWING界面组件 代码参 ...

  2. java异常——捕获异常+再次抛出异常与异常链

    java异常--捕获异常+再次抛出异常与异常链 参考文章: (1)java异常--捕获异常+再次抛出异常与异常链 (2)https://www.cnblogs.com/jpfss/p/9454592. ...

  3. java异常错误的是,java 异常 错误处理

    场景:thinking in java 学习笔记 12 通过错误处理异常 thinking in java 学习笔记 12 通过异常处理错误 第十二章 通过异常处理错误 在以前平时的编程中没有真正意思 ...

  4. python异常(概念、捕获、传递、抛出)

    python异常(概念.捕获.传递.抛出) 参考文章: (1)python异常(概念.捕获.传递.抛出) (2)https://www.cnblogs.com/xiangribai/p/8594392 ...

  5. java 异常对象_在java中的异常处理中的异常对象是什么

    展开全部 Exception类以及他的子类 的一个实例对象 比如32313133353236313431303231363533e58685e5aeb931333264633563 常见异常 1. j ...

  6. 简述java异常机制处理,简述Java中异常处理的机制

    Q1:JAVA中的异常处理机制的原理 Java中的异常处理机制的简单原理和应用 : java中Throwable这个类可以被作为异常抛出的类,继承它的分为异常Exception和错误Error. Ex ...

  7. java 文件删除异常 ut_【JAVA-UT】12、抛出了指定的异常吗?

    文|码术张 如何验证抛出的异常是指定的异常? 下面介绍两种方法. 方法一: public class Test1 { @Test(expected = IllegalArgumentException ...

  8. java employee list_JSP中List中的$ {employee.id}抛出java.lang.NumberFormat

    我有一个JSP页面,当List从下面的方法中显示时,它可以正常工作. @RequestMapping(value = { "getAllEmployees", "/&qu ...

  9. 10054 java,为什么Socket.Receive在远程主机断开连接时抛出SocketException(10054)?

    我以前用C编写套接字程序,无法理解为什么会发生这种情况 . 我的服务器在接收调用时阻塞,当它返回0时,我打破了while循环并关闭了线程 . public class MyServer { publi ...

最新文章

  1. iOS 提示更新 业务逻辑
  2. MSXML解析[转]
  3. mysql更新日志问题
  4. 经典C语言程序100例之十一
  5. Google 开源技术protobuf 简介与样例
  6. 【BZOJ - 4754】独特的树叶(树哈希)
  7. 你家的饮水机,到底可以有多脏?
  8. 【英语学习】【WOTD】resuscitate 释义/词源/示例
  9. 什么是IDS/IPS?
  10. 从ELK到EFK演进
  11. 3810.最长连续休息时间-AcWing题库
  12. 佳能g2810提示5b00_佳能G2810 3800系列出现5B00如何解决
  13. sqlmap的安装教程
  14. Python pip卸载包
  15. 战舰世界换服务器改什么文件,战舰世界国服更新账号继承并合区,直营服已更新092...
  16. win10打不开计算机配置似乎是正确的,Win10系统软件打不开提示“并行配置不正确”如何解决...
  17. 美国鲨鱼主机使用笔记
  18. Visio绘制论文文献技术路线图流程图
  19. 程序自动化 linux_自动化Windows子系统Linux安装程序
  20. 这可能是你看到最详细的NCF代码讲解(1)数据处理篇

热门文章

  1. 中两个数做减法_四年级数学下册 | 第1单元加、减法的意义和各部 分之间的关系(P13)...
  2. Cpp 对象模型探索 / 虚函数表和虚函数表指针的创建时机
  3. C语言操作符(又称运算符)(1)
  4. 乐鑫代理启明云端分享|ESP32系列教程之三: VS Code远程连接Linux
  5. c语言期末作业自己设计个小程序,帮看一个小程序吧,c语言期末考…求求各位大神了...
  6. TVS二极管电压6V,有哪些型号?
  7. html5语法参考图片,仿AS语法来写HTML5—第1章,显示一张图片
  8. java简易计算器考察什么_练习:用java写一个简易计算器
  9. 【Java】Struts2配置默认Action和配置Action的默认处理类
  10. python(五)模块