godot: GDScript error checking
I’ve been testing out the new multiplayer (I’m using the master branch), and as I was trying to make a basic server and client, I noticed that an invalid IP would cause an error to be printed in the Debugger. Issue is, that I really dont know how to detect the error, if it is even possible, and deal with it in code, as exceptions simply dont exist and the function returns 0 (which is the code for OK, if I’m not mistaken).
Seeing this problem, and a lot of others, maybe exceptions could be added in gdscript? I know that this has been addressed before, and rejected as godot aims to be difficult to crash. That is why I’m proposing the following:
- Have exceptions be non-fatal. That means that an exception would just go to the debugger and print out an error message if not caught, instead of crashing the program
- Exceptions should not travel back the call stack. This should:
- Make it easy for godot to tell whether or not to print a debug message
- Prevent unwanted behaviour, as the exception would not have to terminate the function, and keep doing so, until it reaches a try block.
- Make it easier for developers to anticipate exceptions, as they would only have to worry about the immediate context of the try block, instead of whatever functions the functions inside the try block call
- To throw exceptions, the throw command would be used, which would accept an exception object, or for ease of use, the following parameters in order:
- A brief description (string) - required
- Any clarifications that may be useful (string) - optional
- An error code (int) - optional The same parameters could be used to construct exception objects
func error_prone_function(): throw "Invalid IP", "<user-supplied IP here>", ERR_INVALID_IP - To catch exceptions (pretty standard):
try: error_prone_function() catch exception: print(exception) - Optionally, as exceptions do not travel back the call stack, a command that would rethrow exceptions if they occur could be useful:
Instead of# This would throw an exception with a description "Error!" func error_prone_function2(): attempt: error_prone_function()# This would throw an exception with a description "Error!" func error_prone_function2(): try: error_prone_function() catch exception: throw exception
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Reactions: 11
- Comments: 21 (11 by maintainers)
I’ve already seen #3516, and @reduz’s response, and that’s why I’m proposing this altered model for exceptions. The in point is that exceptions should not travel back the callstack, so as to ensure that godot would keep working even if errors occur. This way, the engine’s stability would be preserved and error checking would be made a hole lot easier for developers.
even though you say that, i don’t believe it’s the case
pcall in lua is simple enough, just add a function that executes another function by pointing to it
this function returns 0 or 1 (error codes, like they’ve already chosen)
i am pretty sure it’s a case of… they want it this way because they don’t like abusive programming patterns using error handling… but perhaps in the end we learn that, like perl, different users want different things
EXTRA: apparently godot is designed to keep going when you get errors but i find ANY script in the scene with one dang missing property or null reference crashes my entire game… not just the script like i had stuff set up in Unity
oh yeah, a lack of error handling does keep you on your toes! but it’s a woeful lack of understanding of dynamic programming patterns
this is going on and on… why can’t people who want exceptions just get them?
i’m not asking that reduz uses them… i want them, for MY program design that follow Pythons philosophy (duck typing, asking forgiveness instead of permission)
Currently i have to keep checking over and over is_instance_valid()… what you’re doing is forcing the creation of a new javascript by maintaining this stance. I constantly get crashes that would have been fine in the last world (script crashes, that’s okay, program continues)… but in this one, they kill the ENTIRE program (i can’t release a game like that!!!)
Designing complex systems under this restriction is just getting stressful… these guys just whip out the C++ oh yeah good for them… and keep us noobs using the Basic without having the features we want
Implementing exceptions would add a significant amount of complexity to the GDScript implementation. At the end of the day, some people have to maintain it, so keeping it easy to work on is important 🙂
Thank you for the response @bojidar-bg!
First I’d like to state that what I basically want to propose is something similar to what you suggested in the last part of you post, though my phrasing probably wasnt clear enough. Also, I probably shouldn’t have called it “exceptions”, but I really dont know how to call it… Let’s call it “errors” for now. So, I’ll try and rephrase my proposal, to make it clearer (Also with some new, and hopefully better ideas). If you agree, I’ll also update my original post:
raisecommand similar to the following might be usufull, for simplicity:error_prone_function()always raises an ERR_SOMETHING error. The output would be: So, the order of execution would be:test(), would yield the following output: And the debugger would log the error from the firstget_value()call (infoo())Hopefully I’ve made my ideas clearer, and I’m looking forward to hearing your opinion.
@alex-evelyn Maybe, but honestly I dropped this idea, since the only viable solution that we came up with were the C-errno errors, which I was never a fan of because you never know with absolute certainty where the error originated from. To me, the existing
Errorenum with custom types seem like a better option (maybe with aResultRust-like type that @bojidar-bg mentioned - the documentation would get all kinds of messed up without generics though, if a single type with a variant was introduced).But if you want to start up a discussion, maybe try opening up a new issue, since this diverges a fair bit from the original topic.
On a completely unrelated note (This really isn’t a snarky remark - by all means do open an issue if you think it’s worth implementing): Reading back, a huge thank you to everyone I talked with for their patience. Oh, did I get annoyed from my own persistence (This may not be the place, but I had to say it).
In general, Godot always has a ways to check if there was an error manually becore the error is printed to the console. If this is not possible, then we made a mistake
On Sat, Feb 18, 2017 at 11:22 AM, Bojidar Marinov notifications@github.com wrote:
Ok, let’s see if I can answer your points:
ERRconstant, justreturnit. Example: Currently the thing nearest point 3 isassertin GDScript (which does nearly the same)So, about 4… #7223 is one way that this might be done. Under that proposal one can only do
thing() else default_valuethough, so it is useless for try-catching, though we can return the same default value for rethrowing.One other way I was thinking about in the past is to have
That way we can make a simple try catch already, with something like this:
(I agree that this can benefit from syntax sugar, even if it doesn’t need it a lot) (Note that other functions are free to disregard your raises, and that it behaves exactly like the current C++ version of it)
Finally, I don’t think if there are others or more sensible ways of doing it, but I would be happy to hear about it if it is so.
See https://github.com/godotengine/godot-proposals/issues/432.