MoonSharp generates a few types of exceptions:
all of these inherit from a common
InterpreterException type, which can be used to group these exceptions together in filters.
Of course, other types of exceptions could be generated, due to bugs in calling code or MoonSharp code, but not (in theory in at least) due to errors in script code.
They also support a
DecoratedMessage property which contains the error message, decorated with the reference in the source code which generated the error (if available).
A global option named
RethrowExceptionNested is available. If set to
false (the default), exceptions are rethrown preserving the stack, and the decorated message is available in the
DecoratedMessage property. If set to
true, a new exception is thrown with the older exception being in the
InnerException property, and the decorated message will be available both in the
DecoratedMessage and in the
Message property as well.
You can set
RethrowExceptionNested however it suites your tastes but generally it works better if set to
false when working with .NET and if set to
true when working with Mono, Xamarin and Unity.
InternalErrorException is thrown when the interpreter encounters an internal error.
There’s not really much which can be done, usually this should be considered a fatal error of the scripting engine.
Please report in the forums or discord if it happens, with all possible information to reproduce it, if possible.
SyntaxErrorException is thrown when the parser cannot parse the script code or the script code is invalid for some reason.
Note that this is thrown when calling one of the
Script methods (
RunString, etc.). If the load function of the standard Lua library is called on a broken script, a
ScriptRuntimeException is generated, wrapping the
This is the most common exception of all and, probably, the most important.
Everytime a Lua error is raised (through a call to error function, because of a runtime error, etc.) a
ScriptRuntimeException is raised. For example:
Doh! An error occured! chunk_1:(2,5-36): attempt to index a nil value
If we want to raise an error to Lua, we can just do the same, and raise a
This will return:
This is an exceptional message, no pun intended.
DynamicExpressionException is thrown when the interpreter encounters an error evaluating a dynamic expression. See the tutorial on dynamic expressions to
see what this means.