openexr-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Openexr-devel] validity check on files


From: Paul Schneider
Subject: Re: [Openexr-devel] validity check on files
Date: Fri, 3 Oct 2003 13:01:14 -0700


The EXR library in general uses C++ exceptions as the preferred means of letting you know that something went wrong, so you really should be prepared to catch a exception from the library at any time.

In this case, the InputFile's constructor causes the file to be opened and the header information to be read. If the header isn't valid (not an EXR image, or an unsupported version), it will throw an Iex::InputExc. Calling the what() method of this exception will let you know what went wrong.

Since Iex::InputExc inherits from Iex::BaseExc, which inherits from std::exception, you can catch any exeption that might be thrown like this:

try
{
        InputFile file(fileName);
}
catch (const std::exception& ex)
{
cerr << "couldn't open input file " << fileName << ": " << ex.what() << endl;
}

This will also let you handle exceptions such as std::bad_alloc (out of memory), and Iex::ErrnoExc exceptions, which mean that the file couldn't be opened for some reason (permissions, maybe).

Note that you can put the image reading code in the try block as well, and you only have to worry about error handling in one place.

If you need more fine-grained error handling, you can catch specific exceptions:

try
{
        InputFile file(fileName);
}
catch (const EpermExc& ex)
{
        cerr << "don't have permission to open that file" << endl;
}
catch (const Iex::InputExc& ex)
{
        cerr << "file doesn't appear to really be an EXR file" << endl;
}
catch (const std::bad_alloc& ex)
{
        cerr << "out of memory opening file" << endl;
}
catch (const std::exception& ex)
{
        cerr << "unkown error" << endl;
}

This could let you silently ignore invalid EXR files, if you were just filtering them in an open file dialog for example. In general, you should probably pass the what() string along to the user in some way, as they tend to be pretty descriptive.

Hope that helps,
Paul


On Friday, October 3, 2003, at 12:36 PM, James McPhail wrote:

I hope this hasn't been touched upon in a previous thread.

Is there functionality in the toolkit for determining if an "InputFile" is bogus? Specifically, can the toolkit determine, when passed a file name, that the given file is not of the EXR format?

I had initially wanted to try

InputFile file(fileName);

then had hoped there would be some flag or function call on this 'file' variable or its associated header which would indicate that it was not a valid file, however this InputFile constructor throws an exception (which I couldn't manage to catch) and then crashes.

Any help would be appreciated.

James




_______________________________________________
Openexr-devel mailing list
address@hidden
http://mail.nongnu.org/mailman/listinfo/openexr-devel





reply via email to

[Prev in Thread] Current Thread [Next in Thread]