assert()

类型 函数
(全局)
修订 版本 2024.3703
关键词 assert,错误
另请参阅 error()

概述

当其第一个参数的值为 falsenilfalse)时,发出错误并中止程序。否则,它将返回其所有参数。

此函数对于捕获(并避免)意外情况和错误非常有用。

语法

assert( v [, message] )
v (必填)

任意。 当此值为 false 时发出错误。

message (可选)

字符串 错误消息。 缺省情况下,它默认为“断言失败!”。 错误消息将显示在 Corona Simulator 控制台中。

示例

-- Abort the program if the image is not found (in the Resource directory)
local img = display.newImage( "imageWithWrongName.png" )
assert( img, "Error: Image not found!" )