前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Java学习:assert(断言)的使用——测试程序和AssertionError错误事件

Java学习:assert(断言)的使用——测试程序和AssertionError错误事件

作者头像
全栈程序员站长
发布2022-10-29 10:30:58
1.6K0
发布2022-10-29 10:30:58
举报

大家好,又见面了,我是你们的朋友全栈君。

assert 是在 J2SE1.4 中引入的新特性, assertion 就是在代码中包括的布尔型状态,程序员认为这个状态是 true 。一般来说 assert 在开发的时候是检查程序的安全性的,在发布的时候通常都不使用 assert 。在 1.4 中添加了 assert 关键字和 java.lang.AssertError 类的支持。 首先,我们有必要从一个例子说起 assert

代码语言:javascript
复制
public class AssertTest 
{ 
 public static void main(String[] args) 
 { 
  AssertTest at = new AssertTest(); 
  at.assertMe(true); 
  at.assertMe(false); 
   
 } 
  
 private  void assertMe(boolean boo) 
 { 
  assert boo?true:false; 
  System.out.println("true condition"); 
 } 
  
} 

程序中包含了 assert 的话,你要用 javac -source 1.4 xxx.java 来编译,否则编译器会报错的。要想让 assert 得部分运行的话,要使用 java -ea xxx 来运行,否则包含 assert 得行会被忽略。下面我们运行 javac -source 1.4 AssertTest.java java -ea AssertTest 看看结果的输出是: true condition Exception in thread main java.lang.AssertionError at AssertTest.assertMe(AssertTest.java:13) at AssertTest.main(AssertTest.java:7) 当我们运行 at.assertMe(true) 得时候,由于 assert boo?true:false 相当于 assert true; 因此没有任何问题,程序往下执行打印出 true condition ,但是执行 at.assertMe(false) 的时候相当于 assert false ,这个时候解释器就会抛出 AssertionError 了,程序就终止了。大家必须清楚 AssertionError 是继承自 Error 得,因此你可以不再程序中 catch 它的,当然你也可以在程序中 catch 它然后程序可以继续执行。例如:

代码语言:javascript
复制
public class AssertTest 
{ 
 public static void main(String[] args) 
 { 
  AssertTest at = new AssertTest(); 
  try 
  { 
   at.assertMe(true); 
   at.assertMe(false); 
  } 
  catch(AssertionError ae) 
  { 
   System.out.println("AsseriontError catched"); 
  } 
  System.out.println("go on"); 
   
 } 
  
 private  void assertMe(boolean boo) 
 { 
  assert boo?true:false; 
  System.out.println("true condition"); 
 } 
  
} 

assert 还有另外一种表达的方式,就是 assert exp1:exp2; 其中 exp1 是个 boolean 返回值得表达式,而 exp2 可以是原始的数据类型或者对象都可以例如: boolean boo = true; String str = null; assert boo == false :str=”error”; 我们刚开始讲得 assert exp1 得形式,当 exp1 是 false 得时候, AssertionError 得默认构造器会被调用,但是 assert exp1:exp2 这样的形式,当 exp1 为 true 的时候后面 exp2 被或略,如果 false 的话,后面的表达式的结果会被计算出来并作为 AssertionError 得构造器参数。看下面的例子:

代码语言:javascript
复制
public class AssertTest 
{ 
 public static void main(String[] args) 
 { 
  AssertTest at = new AssertTest(); 
  at.assertMe(true); 
  at.assertMe(false); 
   
 } 
  
 private  void assertMe(boolean boo) 
 { 
  String s = null; 
  assert boo?true:false:s = "hello world"; 
  System.out.println("true condition"); 
 } 
  
} 

运行的时候会得到这样的结果 true condition Exception in thread main java.lang.AssertionError: hello world at AssertTest.assertMe(AssertTest.java:14) at AssertTest.main(AssertTest.java:7) Assert 最好不要滥用,原因是 assert 并不一定都是 enable 的,下面两种情况就不应该用 assert 1 不要再 public 的方法里面检查参数是不是为 null 之类的操作 例如 public int get(String s) { assert s != null; } 如果需要检查也最好通过 if s = null 抛出 NullPointerException 来检查 2 不要用 assert 来检查方法操作的返回值来判断方法操作的结果 例如 assert list.removeAll(); 这样看起来好像没有问题 但是想想如果 assert 被 disable 呢,那样他就不会被执行了 所以 removeAll() 操作就没有被执行 可以这样代替 boolean boo = list.removeAl(); assert boo;

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/196910.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022年9月5日 下,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档