Java的assert语句可以允许您指定一条消息吗? [英] Can java's assert statement allow you to specify a message?

查看:107
本文介绍了Java的assert语句可以允许您指定一条消息吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

似乎喜欢在断言失败时让断言显示消息是很有用的.

Seems likes it might be useful to have the assert display a message when an assertion fails.

当前会抛出AssertionError,您能为其指定自定义消息吗?

Currently an AssertionError gets thrown, can you specify a custom message for it?

您能否展示一个用于执行此操作的示例机制(除了创建自己的异常类型并将其抛出之外)?

Can you show an example mechanism for doing this (other than creating your own exception type and throwing it)?

推荐答案

您当然可以:

assert x > 0 : "x must be greater than zero, but x = " + x;

有关更多信息,请参见使用断言进行编程信息.

See Programming with Assertions for more information.

这篇关于Java的assert语句可以允许您指定一条消息吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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