Lua does not officially support exceptions. But, a similar mechanism can be built with pcalls, or protected calls. An exception halts the flow of code in case of an error, and returns control to the caller right away. Furthermore, some kind of error code is likely provided to the caller. This method of raising errors immediately should, in theory, allow callers to handle unsafe code gracefully.
Much like debugger.lua, third-party Lua modules for profiling already exist. Unsurprisingly, one of the best modules for profiling is profile.lua. You can learn more about profile.lua and download it here: https://bitbucket.org/itraykov/profile.lua