Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/lua/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Error handling 如何打印错误?_Error Handling_Lua_Compiler Errors_Roblox - Fatal编程技术网

Error handling 如何打印错误?

Error handling 如何打印错误?,error-handling,lua,compiler-errors,roblox,Error Handling,Lua,Compiler Errors,Roblox,在名为Roblox studio的软件中,我需要帮助制作一个脚本,每当游戏中发生错误时,它都会使用正常的打印功能再次打印错误,如以下示例所示: localerror=--无论发生什么错误 打印(错误)——所以它只是简单地将其打印出来 使用io库中的stderr流打印调试消息 ——基于打印定义调试函数 函数debug(…)io.stderr:write(table.concat({…},'\9')…\n')结束 --Debug充当常规的“print()”,但它会改为写入stderr 调试('[fi

在名为Roblox studio的软件中,我需要帮助制作一个脚本,每当游戏中发生错误时,它都会使用正常的打印功能再次打印错误,如以下示例所示:

localerror=--无论发生什么错误
打印(错误)——所以它只是简单地将其打印出来

使用
io
库中的stderr流打印调试消息

——基于打印定义调试函数
函数debug(…)io.stderr:write(table.concat({…},'\9')…\n')结束
--Debug充当常规的“print()”,但它会改为写入stderr
调试('[filename.lua]','debug')
使用“error()”函数打印错误

如果为true,则
错误(“不应该是真的!”)
结束
如果要捕获函数中的错误,请使用
xpcall()

——定义调试
函数debug(…)io.stderr:write(table.concat({…},'\9')…\n')结束
xpcall(
函数()
局部b={}
--这将抛出一个错误,因为您不能索引nil值
印刷品(a.b)
完,,
功能(err)
调试(“[xpcall]”,错误)
结束
)

xpcall()

如果希望在脚本中本地观察错误,并且知道特定代码块可能会抛出错误,则可以将其用作try-catch块

local success, result = pcall(function()
    error("this is a test error message")
end)
if not success then
    print("An error was thrown!")
    print(result) --"this is a test error message"
end
如果要在所有脚本中观察错误,可以将回调附加到。每当抛出任何错误时,此信号都会触发。它提供有关错误的信息,包括消息、调用堆栈和对引发错误的脚本的引用

警告
脚本上下文。错误
仅在注册它的脚本上下文中激发。脚本只会观察服务器脚本中抛出的错误,而在LocalScript中注册只会观察客户端上抛出的错误

local ScriptContext = game:GetService("ScriptContext")
ScriptContext.Error:Connect( function(message, stack, context)
    print("An error was thrown!")
    print("Message : ", message)
    print("Stack : ", stack)
    print("Context :", context:GetFullName())
end)
类似地,如果您只关心错误消息本身,还可以观察到它们正在使用打印到输出窗口。该信号在任何记录到输出的时间触发。这包括消息、警告和错误

local LogService = game:GetService("LogService")
LogService.MessageOut:Connect( function(message, messageType)
    if messageType == Enum.MessageType.MessageError then
        print("An error was thrown!")
        print(message)
    end
end)

您希望处理编译器错误还是运行时错误?