assert()

Type Function
Library (globals)
Revision Release 2022.3683
Keywords assert, error
See also error()

Overview

Issues an error and aborts the program when the value of its first argument is false (nil or false). Otherwise, it returns all its arguments.

This function is useful for catching (and avoiding) unexpected situations and bugs.

Syntax

assert( v [, message] )
v (required)

Any. Issues an error when this value is false.

message (optional)

String. An error message. When absent, it defaults to "assertion failed!". The error message is displayed in the Corona Simulator Console.

Example

-- 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!" )