[Gdal-dev] SWIG Exceptions for C# Bindings

Ari Jolma ari.jolma at tkk.fi
Sat Mar 17 04:18:06 EDT 2007


I'd say we should add checks and calls for CPLError for null values like 
this into the bindings layer.

I did this for a number of methods in ogr.i, for example into 
OGRFeature::GetFieldDefnRef. The simple reason is that if the null gets 
into the scripting language layer, it will create a core dump, which is 
not acceptable behavior, and this is the last place where it can be done 
(at least in the case of Perl). I know there are still many cases in 
which this happens since I get core dumps if I'm careless in coding with 
the Perl bindings.

Could we adopt the policy of always throwing an exception in a case of 
errors in the bindings?

Regards,

Ari

Tamas Szekeres kirjoitti:
> It depends on the implementation in the gdal core whether the CPLError
> is invoked or not. Generally if we can retrieve the error by calling
> CPLGetLastErrorType() the error will be propagated to C# as an
> exception.
> As far as I can see the various providers are not calling CPLError in
> DataSource.GetLayerByIndex. Here is a common example of it:
>
> OGRLayer *OGRPGDataSource::GetLayer( int iLayer )
> {
>    if( iLayer < 0 || iLayer >= nLayers )
>        return NULL;
>    else
>        return papoLayers[iLayer];
> }
>
> Therefore you should always check the return value in this case.
>
> Best regards,
>
> Tamas
>
>
> 2007/3/16, Simon Perkins <sy at perkins.net>:
>> Can someone remind me how error reporting works with the GDAL and OGR C#
>> bindings? Do errors typically throw exceptions, or return status codes,
>> or nulls, or both? There seems to be code to throw exceptions in most of
>> the method definitions, but the code in swig/csharp/apps often appears
>> to be checking for return null values instead. Which is correct?
>>
>> For instance, if OGR.DataSource.GetLayerByIndex() is invoked with an
>> invalid index, or if something else goes wrong, do we always/sometimes
>> get an exception or do we just get a null back? Is there a general 
>> policy?
>>
>> Cheers,
>>
>> Simon
>>
>> _______________________________________________
>> Gdal-dev mailing list
>> Gdal-dev at lists.maptools.org
>> http://lists.maptools.org/mailman/listinfo/gdal-dev
>>
> _______________________________________________
> Gdal-dev mailing list
> Gdal-dev at lists.maptools.org
> http://lists.maptools.org/mailman/listinfo/gdal-dev


-- 
Prof. Ari Jolma
Geoinformaatio- ja paikannustekniikka
Geoinformation and positioning technology
Teknillinen Korkeakoulu / Helsinki University of Technology
POBox 1200, 02015 TKK, Finland
Email: ari.jolma at tkk.fi URL: http://www.tkk.fi/~jolma




More information about the Gdal-dev mailing list