断言是被用来检查非法情况而不是错误情况,即在该程序正常工作时绝不应该发生的非法情况,用来帮助开发人员对问题的快速定位。异常处理用于对程序发生异常情况的处理,增强程序的健壮性、容错性,减少程序使用中对用户不有好的行为,不让(通常也不必)用户知道发生了什么错误。

  实际开发中,我们通常将Assert与异常混淆, 不知道什么时候使用Assert,什么时候使用异常处理。或者不用Assert,将一切情况都归为异常。这样一来,就掩盖了问题,当问题发生的时候,很难进行定位,而这些问题本该是在开发的时候就解决掉的。同时,也增加了开销(在c#中,debug.Assert()编译成release版本时,不会产生任何代码,而try/catch在debug/release版本中都是有代码产生,运行时需要开销)。

 

  Assert类位于Microsoft.VisualStudio.TestTools.UnitTesting 命名空间下,该命名空间提供支持单元测试的类。此命名空间包含许多属性,这些属性为测试引擎标识有关数据源、方法执行顺序、程序管理、代理/主机信息以及部署数据的测试信息。Microsoft.VisualStudio.TestTools.UnitTesting 命名空间还包含自定义单元测试异常。

  MSDN中,有详细的Assert类的公共方法:http://msdn.microsoft.com/zh-cn/library/Microsoft.VisualStudio.TestTools.UnitTesting.Assert(v=vs.100).aspx

  本文简单归类一些简单的使用方法。掌握Assert类,一个BestPractice就是,了解Assert的几个主要方法,然后熟悉其重写方法即可,整理表格如下:

Name

Description

AreEqual(+18)

AreNotEqual(+18)

Verifies that specified values are equal(or not equal). The assertion fails if the values are not equal(or equal).

Displays a message if the assertion fails, and applies the specified formatting to it.(if available)

AreSame(+3)

Verifies that specified object variables refer to the same object. The assertion fails if they refer to different objects.

Displays a message if the assertion fails, and applies the specified formatting to it.(if available)

Equal

Determines whether two objects are equal.

Fail(+3)

Fails an assertion without checking any conditions.

Displays a message, and applies the specified formatting to it.(if available)

InConclusive(+3)

Indicates that an assertion cannot be proven true or false. Also used to indicate an assertion that has not yet been implemented.

Displays a message, and applies the specified formatting to it.(if available)

IsFalse(+3)

IsTrue(+3)

Verifies that a specified condition is false(or true). The assertion fails if the condition is true(not true).

Displays a message, and applies the specified formatting to it.(if available)

IsInstanceOfType(+3)

IsNotInstanceOfType(+3)

Verifies that a specified object is(or is not) an instance of the specified type. The assertion fails if the type is not(or is) found in the inheritance hierarchy of the object.

Displays a message, and applies the specified formatting to it.(if available)

IsNull(+3)

IsNotNull(+3)

Verifies that a specified object is null(or not null) . The assertion fails if it is not null(or is null).

Displays a message if the assertion fails, and applies the specified formatting to it.(if available)

ReplaceNullChars

In a string, replaces null characters ('\0') with "\\0".

  关于异常处理, 在编写代码的时候,应充分考虑各种具体异常,而不简单的catch到Exception,写出更健壮的代码。

  通常来说,能够用Assert的地方,都可以用try/catch处理 。但这不是好习惯。We need to "Writing Clean Code".

  文中相关扩展连接,  

  Microsoft.VisualStudio.TestTools.UnitTesting Namespace

  Assert Class

  A Unit Testing Walkthrough with Visual Studio Team Test

Lionden 2015年1月18日

E-mail:hsdlionden@hotmail.com

转载请注明原文地址和博客园Liondenhttp://www.cnblogs.com/lionden/

断言(Assert)与异常(Exception)的更多相关文章

  1. C# 断言 Assert

    重构-断言 现象:某一段代码需要对程序状态做出某种假设 做法:以断言明确表现这种假设 动机: 常常有这种一段代码:只有某个条件为真是,该改名才能正常运行. 通常假设这样的假设并没有代码中明确表现出来, ...

  2. C#中的断言(Assert)

    重构-断言 现象:某一段代码需要对程序状态做出某种假设 做法:以断言明确表现这种假设 动机: 常常有这种一段代码:只有某个条件为真是,该改名才能正常运行. 通常假设这样的假设并没有代码中明确表现出来, ...

  3. C++异常(exception)第一篇--综合讲解

    摘要:catch(exception &ex)是捕获所有标准库定义中的类std:exception;catch(...)则是捕获所有的异常. 1.简介 异常是由语言提供的运行时刻错误处理的一种 ...

  4. SpringBoot(20)---断言(Assert)

    SpringBoot(20)---断言(Assert) 我们在写单元测试的时候,除了接口直接抛异常而导致该单元测试失败外,还有种是业务上的错误也代表着该单元测试失败.好比我们在测试接口的时候, 该接口 ...

  5. 断言(assert)简介

    java中的断言assert的使用 一.assertion的意义和用法 J2SE 1.4在语言上提供了一个新特性,就是assertion功能,他是该版本再Java语言方面最大的革新. 从理论上来说,通 ...

  6. K:java 断言 assert 初步使用:断言开启、断言使用

    @转自天地悠悠的个人博客 主要总结一下在eclipse中如何使用断言. (一)首先明确: java断言Assert是jdk1.4引入的. jvm 断言默认是关闭的. 断言是可以局部开启的,如:父类禁止 ...

  7. 断言(assert)

    断言(assert):用来调试.测试代码. 格式: assert 布尔表达式: 字符串 (如果布尔表达式为false时,这个字符串才会显示). 注意: assert默认是关闭的,使用时需要使用&quo ...

  8. java断言assert初步使用:断言开启、断言使用

    1 说明 java断言assert是jdk1.4引入的. jvm断言默认是关闭的. 断言可以局部开启的,如:父类禁止断言,而子类开启断言,所以一般说“断言不具有继承性”. 断言只适用复杂的调式过程. ...

  9. 理解Python语言里的异常(Exception)

    Exception is as a sort of structured "super go to".异常是一种结构化的"超级goto". 作为一个数十年如一日 ...

随机推荐

  1. Java进击C#——语法之基础

    本章简言 上一章讲到关于项目工程开发常用的知识点,有了前面俩章的介绍之后.本章正式开始介绍关于C#的基础语法.我们都很清楚C#也是面向对象的计算机语言.而且他跟JAVA的相似度高达80%.所以很多语法 ...

  2. Moon.Orm常见问题问答FAQ

    有问题在评论,我看到邮件会尽快回复 1.重点了解Db里面的方法.这是核心. 2.关于查询语句MQL:http://www.cnblogs.com/humble/p/3380065.html 3.关于如 ...

  3. serviceStack.Redis 在PooledRedisClientManager 中设置密码

    ServiceStack.Redis 是一个C#访问Redis的客户端,可以说可以通过它实现所有需要Redis-Cli的功能.但是今天我在主Redis 实例设置了访问密码,而在slave 上没有设置, ...

  4. 【原创】SQL分页查询存储过程

    ------------------------------------- -----作者:张欣宇 -----时间:2013-06-28 -----简介:根据参数和条件分页查询 ----------- ...

  5. hostingEnvironment与宿主环境

    定义用来控制应用程序宿主环境的行为的配置设置. 配置如下 <hostingEnvironment idleTimeout="HH:MM:SS" shadowCopyBinAs ...

  6. deviceFilters与设备过滤

    本主题是ASP.NET在移动设备上展示的方面的内容 起初看起来deviceFilters与前面的browserCaps类似.同样也会关联到HttpBrowserCapabilities,而在这里用到的 ...

  7. 如何在windows下的Python开发工具IDLE里安装其他模块?

    以安装Httplib2模块为例 1 下载模块 到 “https://code.google.com/p/httplib2/” 下载一款适合你的压缩包“httplib2-0.4.0.zip” 2 解压下 ...

  8. No.026:Remove Duplicates from Sorted Array

    问题: Given a sorted array, remove the duplicates in place such that each element appear only once and ...

  9. Java实现Mysql数据库自动备份

    最近项目中有一个这样的需求,要求定时备份数据库.看了一些网上的资料,了解到主要思路是是使用java中的Runtime类的exec()方法,可以直接调用windows的cmd命令,参数就是Mysql的备 ...

  10. 自适应备忘录 demo

    <!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8&qu ...