Error Handling
Last update: 16.07.2025Catch and invoke errors at runtime.
-- version checks (we use them to ensure that the script is working as expected)
-- simple version check:
sos.requireMinVersion(3,3,4) -- error if SoS version < 3.3.4
sos.requireMaxVersion(30,0,0) -- error if SoS version >= 30.0.0
-- custom version check:
local major,minor,maint = sos.getVersion() -- get version numbers
major = math.floor(major) -- for better printing: convert floating point numbers to integer (Since Lua 5.3!)
minor = math.floor(minor)
maint = math.floor(maint)
if (major < 3 or (major == 3 and minor < 3) or (major == 3 and minor == 3 and maint < 4) ) then
error("We need at least version 3.3.4. " ..
"Your SoS has version " .. major .. "." .. minor .. "." .. maint .. "! " ..
"Aborting with error!")
end
print("This is SoS version " .. major .. "." .. minor .. "." .. maint .. ". ")
-- how to create and catch errors ?
-- use the pcall function and get the error code
local success_code,err_info = pcall( function() -- this line is required!
-- here you write your own code
-- in this example we create a runtime error
error("Create an intended runtime error. This message is intended and should be catched by the script.")
end ) -- this line is required (marks the end of the "pcall" call)
print("An error appeared during script execution. " ..
"The error was intended and successfully catched.")
else -- there was no error -> here we invoke a true error since this may not happen!
error("Although intended there was no runtime error identified. " ..
"This is not supposed to happen, aborting script!")
end
-- how to get name of a function if you do not know how it works ? -> function info(...)
-- you can pass any object to the info function and it will list its members
print("\nInformation on object success_code:")
info(success_code)
print("\nInformation on all functions and parameters of class CreateFMOP:")
settings = sos.CreateFMOP()
info(settings,1)
print("\nInformation on all functions and parameters in node data object container:")
info(sos.database():data():filterType(sos.NODE_DATA),1)
-- warning: You should not call info(sos) in GUI mode for performance reasons
print("Terminate script with success.")