18045

How to handle errors while using Glib.Settings in Vala?

I am using Glib.Settings in my Vala application. And I want to make sure that my program will work okay even when the schema or key is not available. So I've added a try/catch block, but if I'm using the key that doesn't exist, the program segfaults. As I understood, it doesn't even reach the catch statement. Here is the function that uses settings:

GLib.Settings settings; string token = ""; try { settings = new GLib.Settings (my_scheme); token = settings.get_string("token1"); } catch (Error e) { print("error"); token = ""; } return token;

And the program output is:

(main:27194): GLib-GIO-ERROR **: Settings schema 'my_scheme' does not contain a key named 'token1' Trace/breakpoint trap (core dumped)

(of course I'm using my real scheme string instead of my_scheme) So can you suggest me where I'm wrong?

Answer1:

The methods in GLib.Settings, including get_string do not throw exceptions, they call abort inside the library. This is not an ideal design, but there isn't anything you can do about it.

In this case, the correct thing to do is fix your schema, install into /usr/share/glib-2.0/schemas and run glib-compile-schemas on that directory (as root).

Vala only has checked exceptions, so, unlike C#, a method must declare that it will throw, or it is not possible to do so. You can always double check the Valadoc or the VAPI to see.

Recommend

  • If next item in looping array exists
  • Regex to extract value between a single quote and parenthesis using boost token iterator
  • strtok when process two strings at same time
  • remove whitespaces tail from string (char*)
  • What third-party options are there for working with strings in C?
  • Glib memory allocation error
  • not able to build mod_tile on openSUSE
  • Post Data Getting Lost during Ajax Post Request in Razor
  • Compiling from Netbeans fails, but from terminal works
  • PHP reindexing an array? [duplicate]
  • PInvoking with StringBuilder in a Unit Test
  • Having trouble creating a list based on function arguments
  • Codename one separate class along with statemachine class
  • Install different versions of nuget packages inside one solution file with two projects
  • Configuring Ninject
  • Syntax error near unexpected token 'elif'
  • Search function not doing anything
  • Search files(key) in s3 bucket takes longer time
  • How to name a 'group' check box in Adobe Reader when wanting to fill form by FDF / XFDF
  • Creating a setup.exe in InstallShield in Visual Studio
  • Thread synchronization with syncwarp
  • read values from form post in jquery or javascript
  • What does “t” refer to in this SQL?
  • CERN ROOT exporting data to plain text
  • Django simple Captcha “No module named fields” error
  • Why does access(2) check for real and not effective UID?
  • Google cloud sdk not working when python points python3
  • align graphs with different xlab
  • Return words with double consecutive letters
  • AT Commands to Send SMS not working in Windows 8.1
  • Is there a mandatory requirement to switch app.yaml?
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • Hits per day in Google Big Query
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • Linking SubReports Without LinkChild/LinkMaster
  • apache spark aggregate function using min value
  • XCode 8, some methods disappeared ? ex: layoutAttributesClass() -> AnyClass
  • Sorting a 2D array using the second column C++
  • Reading document lines to the user (python)
  • Python/Django TangoWithDjango Models and Databases