Add support for specialized GErrorExceptions #363
No reviewers
Labels
No labels
bug
dependencies
documentation
duplicate
enhancement
github_actions
good first issue
help wanted
invalid
java
question
wontfix
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
java-gi/java-gi!363
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "specialized-gerror-exceptions"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Java methods generated by Java-GI throw a
GErrorExceptionwhen the native function sets a trailingGError**parameter. With this PR, the exception type will be specialized with subclasses extending fromGErrorExceptionand based on GLib error domains, such asIOException(forGIOError) orCssParserExceptionforGtkCssParserError.Because error domains are set at runtime and not introspectable from the GIR files, the Java method signatures will still throw
GErrorException. However, in thetry-catchclause we can catch the actual type. For example:Because GErrorException is a checked exception, the
catch (GErrorException)clause is still required by the Java compiler. It can be omitted in a Kotlin application.The functionality is implemented with a global static HashMap that maps error domains to exception constructors. (An Exception class is generated for each error domain.) The HashMap is filled during initialization of a module. For example, during initialization of the Gtk bindings, the following error domains are registered:
The generated exception classes are really small:
When a method raises an exception, it passes a pointer to the GError to
GErrorException.take()which will return a GErrorException instance that subsequently will be thrown.GErrorException.take()used to always create a GErrorException instance, but now it will instead read the error domain and create a specialized type, based on the registered mappings. When an error domain is not registered, a base GErrorException is created as a fallback: