assert

Takes in a boolean value where, on the condition that it evaluates to false or nil an error is thrown with the specified message.

ExampleAsserting an error
Throws an error with specified error message due to the condition being false.
Code
assert(false, "example error message")
Output
program.lua:1: example error message
ExampleError checking in a function
Throws an error when something other than an even number is given
Code
function assertNumber(number)
  assert(number/2 == math.ceil(number/2), "this is odd")
end
assertNumber(4) --Passes by this since it is an even number
assertNumber('foo')
Output
program.lua:5: this is odd

assert
Function
Syntax
assert(
  • expression : boolean
  • message : string
)

Returns nil
API Base globals
Source CC:Tweaked
This article is issued from Computercraft. The text is licensed under Creative Commons - Attribution - Sharealike. Additional terms may apply for the media files.