assert关键字用法简单,但是使用assert往往会让你陷入越来越深的陷阱中。应避免使用。总结了以下原因:
 
1、用assert代替if是陷阱之二。assert的判断和if语句差不多,但两者的作用有着本质的区别:assert关键字本意上是为测试 调试程序时使用的,但如果不小心用assert来控制了程序的业务流程,那在测试调试结束后去掉assert关键字就意味着修改了程序的正常的逻辑。
 
2、assert断言失败将面临程序的退出。这在一个生产环境下的应用是绝不能容忍的。一般都是通过异常处理来解决程序中潜在的错误。但是使用断言就很危险,一旦失败系统就挂了。
 
如果<boolean表达式>为true,则程序继续执行。
如果为false,则程序抛出AssertionError,并终止执行。
 
Map<Integer, String> m = new Map<Integer, String>(); // Define a new map
m.put(1, 'First entry'); // Insert a new key-value pair in the map
m.put(2, 'Second entry'); // Insert a new key-value pair in the map
System.assert(m.containsKey(3)); // Assert that the map contains a key
String value = m.get(2); // Retrieve a value, given a particular key
System.assertEquals('Second entry1', value);
Set<Integer> s = m.keySet(); // Return a set that contains all of the keys in the map

 System.assert(m.containsKey(3));


==>System.AssertException: Assertion Failed 因为3 不在key范围内,因此出错,即程序中断
 System.assertEquals('Second entry1', value);
 
 ==>System.AssertException: Assertion Failed: Expected: Second entry1, Actual: Second entry  因为value 和 Secong entry1 不相等

 
 

 

 

最新文章

  1. Bootstrap3系列:导航
  2. spring框架学习(二)依赖注入
  3. 利用jQueryRotate旋转插件开发大转盘抽奖
  4. list的使用命令 百度经验保存
  5. android 中int 和 String 互相转换的多种方法
  6. WCF开发教程资源收集
  7. SQLLite 简介
  8. 【Java】基本数据类型长度
  9. Javascript学习8 - 脚本化文档(Document对象)
  10. Sonya and Problem Wihtout a Legend
  11. APIcloud 移动端常用事件
  12. 敏捷测试(6)--基于story的敏捷基础知识
  13. 利用toggle实现背包
  14. Angular2入门:TypeScript的类 - 参数属性:定义和初始化类成员
  15. django操作数据库 ORM
  16. tf运作方式之共享变量
  17. JS中的兼容问题总结
  18. 《DSP using MATLAB》Problem 3.5
  19. 关于IIS Express,集成管道
  20. 增强型for和Iterator学习

热门文章

  1. Java程序(数组扩容的尝试)
  2. springboot外部部署官方文档说明复制版
  3. windows系统mysql8.0.20.0踩坑(-)
  4. B - Yet Another Palindrome Problem
  5. Quartz 2D CGGradient与CGShading实现渐变的绘制
  6. 基于注解的AOP
  7. [部署日记]Android Studio在安装完后,sdk依旧提示SDK emulator directory is missing
  8. 日記かな、自分のサーバ作りの?(01、try…catch…の問題)
  9. 读取数组树下的某值,并返回其父级下的任何值 vue
  10. 微信小程序JS遇到【object object 】怎么打印?js如何打印object对象