062_Apex使用Assert
2024-10-21 11:58:28
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 不相等
最新文章
- Bootstrap3系列:导航
- spring框架学习(二)依赖注入
- 利用jQueryRotate旋转插件开发大转盘抽奖
- list的使用命令 百度经验保存
- android 中int 和 String 互相转换的多种方法
- WCF开发教程资源收集
- SQLLite 简介
- 【Java】基本数据类型长度
- Javascript学习8 - 脚本化文档(Document对象)
- Sonya and Problem Wihtout a Legend
- APIcloud 移动端常用事件
- 敏捷测试(6)--基于story的敏捷基础知识
- 利用toggle实现背包
- Angular2入门:TypeScript的类 - 参数属性:定义和初始化类成员
- django操作数据库 ORM
- tf运作方式之共享变量
- JS中的兼容问题总结
- 《DSP using MATLAB》Problem 3.5
- 关于IIS Express,集成管道
- 增强型for和Iterator学习
热门文章
- Java程序(数组扩容的尝试)
- springboot外部部署官方文档说明复制版
- windows系统mysql8.0.20.0踩坑(-)
- B - Yet Another Palindrome Problem
- Quartz 2D CGGradient与CGShading实现渐变的绘制
- 基于注解的AOP
- [部署日记]Android Studio在安装完后,sdk依旧提示SDK emulator directory is missing
- 日記かな、自分のサーバ作りの?(01、try…catch…の問題)
- 读取数组树下的某值,并返回其父级下的任何值 vue
- 微信小程序JS遇到【object object 】怎么打印?js如何打印object对象