assert

From ComputerCraft
Jump to navigation Jump to search

assert
Function
Syntax
assert()
Returns nil
API Base globals
Source CC:Tweaked

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

Error creating thumbnail: Unable to save thumbnail to destination
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
Error creating thumbnail: Unable to save thumbnail to destination
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