Accert 類中的斷言方法,

2021-09-07 18:32:03 字數 1317 閱讀 1042

1. notnull(object object)  

當 object 不為 null 時丟擲異常,notnull(object object, string message) 方法允許您通過 message 定製異常資訊。和 notnull() 方法斷言規則相反的方法是 isnull(object object)/isnull(object object, string message),它要求入參一定是 null;

2. istrue(boolean expression) / istrue(boolean expression, string message)  

當 expression 不為 true 丟擲異常; 

3. notempty(collection collection) / notempty(collection collection, string message) 

當集合未包含元素時丟擲異常。 

notempty(map map) / notempty(map map, string message) 和 notempty(object array, string message) / notempty(object array, string message) 分別對 map 和 object 型別的入參進行判斷;

4. haslength(string text) / haslength(string text, string message)  

當 text 為 null 或長度為 0 時丟擲異常;

5. hastext(string text) / hastext(string text, string message)  

text 不能為 null 且必須至少包含乙個非空格的字元,否則丟擲異常;

6. isinstanceof(class clazz, object obj) / isinstanceof(class type, object obj, string message)  

如果 obj 不能被正確造型為 clazz 指定的類將丟擲異常;

7. isassignable(class supertype, class subtype) / isassignable(class supertype, class subtype, string message)  

subtype 必須可以按型別匹配於 supertype,否則將丟擲異常;

使用 assert 斷言類可以簡化方法入參檢測的**,如 inputstream getdata(string file) 在應用 assert 斷言類後,其**可以簡化為以下的形式:

public inputstream getdata(string file)

Spring中的Assert斷言常用方法及作用

斷定某乙個實際的值就為自己預期想得到的,如果不一樣就丟擲異常。常用於server層處理業務邏輯時往外拋異常並提示異常原因。方法及作用 assert.notnull object object,object is required 物件非空 assert.istrue object object,ob...

Visual C 中的斷言

斷言語句指定在程式的某些特定點應為真的條件。如果該條件不為真,則斷言失敗,中斷程式的執行,並顯示 斷言失敗 對話方塊。visual c 支援基於下列構造的斷言語句 mfc 程式的 mfc 斷言。使用 atl 的程式的 atlassert。使用 c 執行時庫的程式的 crt 斷言。其他 c c 程式的...

Python中的斷言

在python中,做出斷言的語句是assert,assert後面跟任何合法的表示式,assert語句做出乙個判斷,如果結果為true,則該語句不做任何事情,如果結果為false,assert語句會丟擲異常assertionerror,這個丟擲異常很有作用,我們可以在assert語句後加上解釋性語句,...