[Vala] How to ignore exceptions?



Hi all,
I started using Vala recently and I'm confused by exception handling in
Vala.
I'm quite used to write C/GObject code and handles GError.
However, with exceptions, flow-control becomes quite different.
For example, I'm reading values from a KeyFile.
With C/glib, I can pass NULL GError* pointer to g_key_file_get_string() to
ignore any errors.
If the errors are not critical for my program, it's safe to ignore them all
like this.

str1 = g_key_file_get_string(kf, group, key, NULL);
str2 = g_key_file_get_string(kf, group, key, NULL);
str3 = g_key_file_get_string(kf, group, key, NULL);
if( !str1 && !str2 && !str3)
    return FALSE;

With Vala, I cannot do this. If I use try/catch, things are totally
different.

try {
    str1 = kf.get_string(group, key);
    str2 = kf.get_string(group, key);
    str3 = kf.get_string(group, key);
}
catch(KeyFile.Error error) {
    // If anyone of the preceding calls fails, we get here.
    // then I cannot continue getting the remaining strings from key file.
}
If get str1 fails, the excution jumps to catch() block, and I cannot
continue reading str2 and str3.

Of course I can do this:
try {
    str1 = kf.get_string(group, key);
}
catch(KeyFile.Error error) {
}
try {
    str2 = kf.get_string(group, key);
}
catch(KeyFile.Error error) {
}
try {
    str3 = kf.get_string(group, key);
}
catch(KeyFile.Error error) {
}

Apparently, this looks even worse then its C/glib counterpart.
So, how to ignore the GErrors/vala excaptions anyways?

Thank you all.


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]