I’m working on GTK C++ Linux application. I wanted to know if any widget creation API can fail?
If yes, in what scenarios? And can we get some error code or something?
At a generic level, GObject creation via g_object_new()cannot fail. The constructor must return a valid GObject instance. GObject types that can fail at construction should implement the GInitable or GAsyncInitable interfaces.
Any wrapper around g_object_new(), like gtk_button_new(), is considered a C convenience API; those may fail through precondition checks on their arguments. Failures on precondition checks are considered programmer errors, and they will emit a critical warning; they are not recoverable by definition, so you won’t get an error code that you can catch.
See above: failures on precondition checks will emit a critical warning.
If a function has a recoverable error state, it will use GError in their arguments.