Support throw and try/catch for posix profile
In GObject profile error handling is implemented with GError struct. It uses common pattern for GLib-based applications where GError**
pointer is passed as the last argument of a function. Posix profile does not support this pattern in the same way as GObject profile does. The same approach might be used for Posix profile. While compliling a set of Vala files, compiler will generate additional header with all information regarding errors. It might produce something like
//error.h
//general definition
//Maybe it worth add project name of something to be able to distinguish this struct from the same struct in different project.
struct ValaError
{
int domain; //in GLib it is GQuark, for POSIX it might be hash of a namespace string or of a errordomain name. Unique value to identify the domain.
int code; //the same as in GLib
char* message; //the same as in GLib.
};
#define NAME_SPACE_DOMAIN_CODE 123
//public errordomain
typedef enum NameSpaceErrorDomainCodes {
FOO,
BAR,
FOO_BAR
}
//namespace_errors.h
//internal errordomains will be presented here
#include "errors.h"
#define NAME_SPACE_DOMAIN_CODE 456
//internal errordomain
typedef enum {
FOO,
BAR,
FOO_BAR
} NameSpaceErrorDomainCodes;
Handing of such error in C code will be very similar to the code for GLib. Such Vala code
try
{
func_with_error();
}
catch(FooError e) {
if(e is FooError.Bar) {
handle_bar(e);
}
handle_foo(e);
}
ValaError *err = NULL;
func_with_error(&err);
if(err != NULL) {
if(err->domain == NAME_SPACE_ERROR_DOMAIN_CODE) {
if(err->code == BAR) {
handle_bar(err);
}
handle_foo(err);
}
}
Edited by Vladyslav Stovmanenko