You want to use an errordomain, not instantiate your own GLib.Error. As you
said earlier, this is a non-GLib library. It probably shouldn't be throwing
GLib errors.
I've dealt with this when writing code with a non-GLib library (Sqlite),
trapping its error codes, and then throwing exceptions named for it. Sample
code:
errordomain MyError {
FROTZ,
NITFOL
}
void try_run(int code) throws Error {
switch (code) {
case 0:
return;
case 1:
throw new MyError.FROTZ("Frotz");
default:
throw new MyError.NITFOL("Nitfol");
}
}
int try_success() {
return 0;
}
int try_frotz() {
return 1;
}
int try_nitfol() {
return 2;
}
void try_catch(int code) {
try {
try_run(code);
stdout.printf("Success\n");
} catch (Error err) {
stdout.printf("Failure: %s\n", err.message);
}
}
void main() {
try_catch(try_success());
try_catch(try_frotz());
try_catch(try_nitfol());
}
Note that try_run() throws Error, not MyError. As I understand it, all
exceptions declared in an errordomain are Errors, which is why this works.
I think of Error as an abstract base class and errordomain declarations as
implementations (but I wouldn't carry the analogy any further than that).
-- Jim
On Thu, Feb 11, 2010 at 12:49 PM, Julian Andres Klode <[email protected]>wrote:
> Am Donnerstag, den 11.02.2010, 21:46 +0100 schrieb Julian Andres Klode:
> > Am Donnerstag, den 11.02.2010, 12:49 -0600 schrieb Sandino Flores
> > Moreno:
> > > Hello.
> > >
> > > Is there a way to construct a throwable error instance from an error
> code?
> > >
> > > I want to implement something like this:
> > >
> > > void try_run(int err_code) throws GLib.Error {
> > > if (err_code != 0)
> > > throw new GLib.Error (Quark.from_string ("My Error"),
> > > err_code, err_desc (err_code));
> > > }
> >
> > Try to assign the error to a variable and you get:
> >
> > a.vala:3.18-3.81: error: Assignment: Cannot convert from `GLib.Error' to
> > `GLib.Error'
> > Error e = new Error (Quark.from_string ("My Error"),
> > err_code, "TEST");
> >
> > ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
> >
> > So I think there is a bug in Vala.
> >
> It actually works if you cast it but it looks stupid, i.e.
>
> throw (Error)new Error (Quark.from_string ("My Error"), err_code, "T");
>
> works.
> --
> Julian Andres Klode - Debian Developer, Ubuntu Member
>
> See http://wiki.debian.org/JulianAndresKlode and http://jak-linux.org/.
>
>
> _______________________________________________
> Vala-list mailing list
> [email protected]
> http://mail.gnome.org/mailman/listinfo/vala-list
>
_______________________________________________
Vala-list mailing list
[email protected]
http://mail.gnome.org/mailman/listinfo/vala-list