[Liblas-devel] Throwing out_of_range exceptions on read

Mateusz Loskot mateusz at loskot.net
Mon Apr 28 22:34:06 EDT 2008


Howard Butler wrote:
> On Apr 28, 2008, at 8:07 PM, Mateusz Loskot wrote:
>>
>> Or there is a good reason Validate throws?
> 
> How else are we to know why the point is not valid?  I'd rather throw, 
> like the rest of the library does, than have special return codes.  Or, 
> do you suggest having a ValidateAndTellMeWhy method too?

Right, seems we need to get it more precise.

First, LASPoint::Validate is public so user is able to check his
points himself and handle it as he prefers. Nothing to discuss here.

Second, we want to check it before writing and skip invalid points.
My point is that a user should be notified that a point has not been 
written but the writing process should not be aborted.
We do not need any special return codes, but we can use return flag 
already output from LASWriter::WritePoint.

for (0 to n)
{

    bool valid_and_written = writer.WritePoint(point);
    if (valid_and_written)
    {
        // 1. user has a chance to handle it as he likes
    }
    // 2. Writing process moves to next point and continues
}

The 1. is nice but not critical but 2. for me is very important.
If WritePoint throws on invalid point, then the execution exits for loop
and it hard to handle such cases gracefully for user.
And, user is forced to use try-catch like if-else statement to control 
execution:

for (0 to n)
{

    try
    {
       writer.WritePoint(point);
    }
    catch (invalid_point e)
    {
       // here user can do 1.
       continue;
    }
}

In fact, to make his client robust, user will have to put this try-catch 
every time he uses WritePoint call, even if he is not interested in any 
validity notifications. But with if-statement he can achieve it very easily:

for (0 to n)
{
    writer.WritePoint(point); // don't want to know about skipped points
}



Another point is that in most cases LASWriter throws if its internal 
state is invalid, and it does not make sense to continue calling writer.
But if a point is invalid, state of writer object is OK and we can 
safely continue using writer.

I hope my explanation makes sense :-)
What you think?

Cheers
-- 
Mateusz Loskot
http://mateusz.loskot.net



More information about the Liblas-devel mailing list