How to acknowledge an exception in Godot ?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Lertsenem
:warning: Old Version Published before Godot 3 was released.

Hi !

I understand that there is no exceptions in gdscript. However, I find myself in an odd situation.

During the processing of my game, I may or may not have created a node at runtime, with the name created_node. Now I want to delete it if it exists.

To find the node, I do a var node_to_delete = get_node("created_node"). Now, if the node exists, node_to_delete is now pointing to it, and I can do node_to_delete.free() or whatever. However, if it does not exists, node_to_delete now contains null. So I have to check for that before going further. The final code snippet is thus :

var node_to_delete = get_node( "created_node" )
  
if node_to_delete == null:
    return
else:
    node_to_delete.free()

This code runs as intended, however while testing it I noticed an error in the debugger, stating: Node not found: created_node

So, am I writing godotscript the way it was intended? What should I do with that error? Just ignore it? Or should I check that created_node exists before doing my get_node()? But won’t that slow the process down for no good reason?

Please, enlighten me! (and as a bonus, convince me there is a good reason I can’t do this with a simple try ... except ... block :] )

:bust_in_silhouette: Reply From: avencherus

Another solution I can think of for checking against dynamically created nodes, is storing them in an array or some container as they’re created.

Then validating them by looping through that array or iterating your preferred data structure. An empty array will tell you if they’re existing or not. Then if there are nodes, while iterating a loop you should be able to carry on with the desired logic from there on each item.

:bust_in_silhouette: Reply From: GlaDOSik

Do you expect null reference often? If the probability of null is high, then it’s not an exception. Well, there is no try except/catch in GDScript so you can’t do it. Also, exception handling often impacts your performance more than basic null check (at least in Java) so even if Godot had exception system, simple check has_node(nodePath) would probably be better.

In lack of a better answer I select that one for the alternative solution of using has_node().

But this does not explain why Godot prompts me an error but does not crash, and if using has_node() to prevent the error from happening is the correct way of doing things™, or if I should just leave it alone and stop worrying. :]

Lertsenem | 2016-10-08 14:40

Well, if you’re not sure the node exists, has_node() is the correct way of doing things. Godot is built in a way to not crash every time there is a problem.

GlaDOSik | 2016-10-08 16:12

:bust_in_silhouette: Reply From: qaptoR

I know this is years later, but there is a proper solution, at least for this specific problem.

instead of using get_node(), use get_node_or_null(), which does the same task without logging an error if null.

This is available in Godot 3.5 and 4.0
enter link description here

I can’t say for certain that all functions that log errors like get_node have an *_or_null variant, but at the very least we can avoid it in this instance.