在java中使用Assert关键字 [英] Assert keyword in java

查看:129
本文介绍了在java中使用Assert关键字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您是否使用此关键字或抛出一些验证运行时异常?它给了你什么好处,或者为什么你认为这不值得使用?

解决方案

Assert会抛出一个运行时错误(<如果其条件为假,则href =http://docs.oracle.com/javase/6/docs/api/java/lang/AssertionError.html =nofollow noreferrer> AssertionError )。断言为您提供了简化的方式来记录,检查和执行代码的正确性标准。好处是用于定义和操纵这些正确性条件的语言级钩子。如果您希望启用或禁用它们(有关这是否是一个好主意),您可以从JVM命令行执行此操作。下面的一些评论者注意到断言是默认禁用,除非运行在调试模式;我的做法是随时在我的包装器脚本中添加-ea(启用断言)。即使在性能敏感的代码中,对于我来说,权衡权衡我从断言得到的安全性/正确性信心。 在Oracle上的断言 AssertionError的API说明



请注意预期或意外故障(异常)之间的区别,这可能在您的控制之外,断言失败 - 断言失败文档程序员假设,并指示不正确的程序,而不是意外的外部条件或预期特殊情况。 如果发生断言失败,解释是程序员误解或错误地表达程序,而不是其他错误或失败的来源。



在实践中,我使用它来记录我制作的明显或非明显的假设,以及我制作(特别是私人/内部)代码时要执行的不变量,我自己和他人为什么要做这些假设,他们在哪里做出来,以及他们是否被验证。比同样的效果好于评论。这是迈向按合同进行设计的一个(小)步骤



有效的Java项目#38检查参数有效性( Google图书 Amazon.com )提供了参数检查和适当使用断言之间区别的有用介绍。



与SO相关:(启用netbeans中的断言),(断言与异常),(ñ耳朵重复,请求示例),(名称不正确,但非常相似的内容


Do you use this keyword or throw some validation runtime exception? What benefits does it give to you or why you think it's not worth to use?

解决方案

Assert will throw a runtime error (AssertionError) if its condition is false. Asserts give you a streamlined way of documenting, checking, and enforcing correctness criteria for your code. The benefits are a language-level hook for defining and manipulating these correctness conditions. To the extent that you wish to enable or disable them (there are arguments about whether or not this is a good idea) you can do so from the JVM command-line. Some commenters below note that assertions are disabled by default unless running in debug mode; my practice is to add "-ea" (enable assertions) in my wrapper scripts at all times. Even in performance sensitive code, for me the tradeoff weighs in favor of the security/correctness confidence I get from assertions. Assertions at Oracle and API Description for AssertionError

Note the distinction between expected or unexpected failures (exceptions), which may be outside your control, and assertion failures -- assertion failures document programmer assumptions, and indicate an incorrect program rather than an unexpected external condition or expected exceptional condition. If an assertion failure occurs, the interpretation is that the programmer has misunderstood or incorrectly expressed the program, rather than other sources of error or failure.

In practice, I use it to document obvious or non-obvious assumptions I make and invariants which I want to enforce as I produce (particularly private/internal) code, making it clear to myself and others why these assumptions are made, where they are made, and whether or not they are validated. Much better than comments to the same effect. This is a (small) step toward Design by Contract.

Effective Java item #38 "Check Parameters for Validity" (Google Books, Amazon.com) provides a useful presentation of the distinction between parameter checking and appropriate use of assertions.

Related on SO: (Enabling assertions in netbeans), (Assertions vs. Exceptions), (Near duplicate, asking for examples), (Badly named, but very similar content)

这篇关于在java中使用Assert关键字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆