[Gdal-dev] Exception class(es) in SWIG C# wrapper

Tamas Szekeres szekerest at gmail.com
Mon Mar 5 17:28:37 EST 2007

2007/3/5, Richard Matsunaga <richard.matsunaga at waypointinfo.com>:
> If I remember correctly, many times in GDAL, a (C API) method will return
> null when something could not happen, like opening a dataset.
> In these cases, I would like to check for null and throw a GdalException or
> OgrException, which contains the extended error message, up my call stack.
> I could retrieve the error information and send that in any exception, like
> an InvalidOperationException, but it's a little cleaner if we can
> encapsulate the message details in new exception type.

Here's the typical SWIG generated error handling code that follows a
method invocation.

result = (GDALDatasetShadow *)Open((char const *)arg1,arg2);
    CPLErr eclass = CPLGetLastErrorType();
    if ( eclass == CE_Failure || eclass == CE_Fatal ) {
        SWIG_CSharpException(SWIG_RuntimeError, CPLGetLastErrorMsg());
return 0;

SWIG_CSharpException will create a new ApplicationException that will
include the string returned by CPLGetLastErrorMsg().  The exception is
thrown when the C# function returns.
Apparently this is a similar operation you have described above. In my
understanding the main difference is that you would want to get a
different exception type for the different modules.

Best regards,


More information about the Gdal-dev mailing list